-
Notifications
You must be signed in to change notification settings - Fork 184
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
Proposal to add coersion or proc types #250
Comments
Would you be open to opening up a PR with an idea of how that would work? |
Hi @gaorlov, I don't work with JsonApiClient anymore. Code which does the job is all included in this particular issue. I used coercion mainly to create ruby range objects and arrays. |
@senid231 what do you think about a |
@gaorlov If we talk about object with one method I've prefer |
@senid231 I guess my question is more: does this belong in the client gem or is this a higher level concern? This feels like parser logic rather than a cast/field validation. Should this be a separate option? Like a property :names, parser: (data) -> { data.to_s.split('#,') }
property :value, type: :double, parser: ValueParser
property :range, parser_name: "RangeParser" Where the parcer classes look like class Parser
attr_reader :field
def initialize( field )
@field = field
end
end
def ArrayParser < Parser
def parse
field.split( ", " )
end
end
def RangeParser < Parser
def parse
start, end = field.split( "-to-" )
( start ... end )
end
end But again, I'm not sure if this belong in this gem. What do you think? |
@gaorlov I think we should keep it simple class OptionsType
def self.call(value)
OpenStruct.new(value) if value
end
end
class Person < JsonApiClient::Resource
property :names, type: ->(value) { value.split(',') }
property :options, type: OptionsType
end |
At the moment I use following patch to support custom coersion:
Used as:
Do you have any plans to have something similar?
The text was updated successfully, but these errors were encountered: