Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow initialization of Object subclasses with models #25

Open
scottfister opened this issue May 8, 2020 · 0 comments
Open

Allow initialization of Object subclasses with models #25

scottfister opened this issue May 8, 2020 · 0 comments
Assignees

Comments

@scottfister
Copy link

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.

@1amageek 1amageek self-assigned this Jun 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants