Skip to content

Allow initialization of Object subclasses with models #25

Open
@scottfister

Description

@scottfister

Is your feature request related to a problem? Please describe.
Model's of Object subclasses require empty init methods:

class User: Object, DataRepresentable {
    var data: Model?
    
    struct Model: Modelable, Codable {
        let id: String
        let firstName: String
        let lastName: String
        
        init() {
            self.id = ""
            self.firstName = ""
            self.lastName = ""
        }
        
        init(id: String, firstName: String, lastName: String) {
            self.id = id
            self.firstName = firstName
            self.lastName = lastName
        }
    }
}

And when used require doing a little dance with initializing separately and assigning:

let user = User()
let model = User.Model(id: userId,
                           firstName: firstName,
                           lastName: lastName)
user.data = model
user.save()

This is un-ideal as you need to write so much repetitive boilerplate for every Object subclass, and you can forget to assign the model to object.data.

Describe the solution you'd like
When initializing an instance of a subclass of Object it would be great if we required the data model to be provided along with it:

let model = User.Model(id: userId,
                           firstName: firstName,
                           lastName: lastName)
let user = User(data: model)
user.save()

Which would enable us to simplify the model blueprint to:

class User: Object, DataRepresentable {
    var data: Model?
    
    struct Model: Modelable, Codable {
        let id: String
        let firstName: String
        let lastName: String
    }
}

Describe alternatives you've considered
I listed the alternative route I'm using above.

Additional context
I'd be happy to help develop this if you think it would be a worthwhile feature.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions