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

Add to_model generator #17

Open
Crisfole opened this issue Aug 27, 2018 · 1 comment
Open

Add to_model generator #17

Crisfole opened this issue Aug 27, 2018 · 1 comment
Assignees
Milestone

Comments

@Crisfole
Copy link
Contributor

From here: apsislabs/slayer#50

It should be easy to write a class method that you can call to generate a to_<model> method.

@wkirby wkirby added this to the 0.4.0 milestone Nov 15, 2018
@wkirby
Copy link
Contributor

wkirby commented Nov 15, 2018

I'm open to this, but I want to sniff out exactly the use case here. I see two ways this can go: either in the form definition, you declare a model that this Form can be translated into:

class PersonForm < Slayer::Form
  models Person
  attribute :name, String
end

form = PersonForm.new(name: 'Joe')
form.to_model # => essentially: Person.new(name: 'Joe')

Or, we could use some magic with method_missing to try to inflect the class you intend to create.

class PersonForm < Slayer::Form
  attribute :name, String
end

form = PersonForm.new(name: 'Joe')
form.to_person # => inflect Person from `to_person`
form.to_contact # => inflect Contact f
form.to_car_dealership # => inflect CarDealership from `to_person`

# The above could all shorthand to a call to a #to_model method, like below
form.to_model(Namespaced::Person) # => bypass inflection, attempt to convert to Namespaced::Person

The second version here is more flexible, since it lets us convert a Form into any model, and any model that has a name attribute will get the value from the Form, but it also is more "magic," and as such inherently less trustworthy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants