Skip to content

Instantly share code, notes, and snippets.

@aflatter
Created March 13, 2015 15:09
Show Gist options
  • Save aflatter/a8951c484c839bd0afd4 to your computer and use it in GitHub Desktop.
Save aflatter/a8951c484c839bd0afd4 to your computer and use it in GitHub Desktop.
# An empty hash does not result in creating a new instance of the type.
# Required because that's what we get back from the relation.
class OptionalCoercer
def initialize(type)
@type = type
end
def call(value)
return unless value
value.values.any? ? type.new(value) : nil
end
def success?(primitive, value)
value.is_a?(primitive)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment