class NestedStruct::Coercer
Attributes
target[R]
type[R]
Public Class Methods
new(expression)
click to toggle source
# File lib/nested_struct/coercer.rb, line 5 def initialize(expression) if expression.is_a?(Class) @type = :single @target = expression elsif expression.is_a?(Array) raise Errors::InvalidCoercerExpression if expression.size != 1 @type = :multiple @target = expression.first else raise Errors::InvalidCoercerExpression end raise Errors::InvalidCoercerTarget if !valid_target? end
Public Instance Methods
coerce(value)
click to toggle source
# File lib/nested_struct/coercer.rb, line 20 def coerce(value) if multiple? raise Errors::InvalidCoercerValue if !value.is_a?(Array) value.map { |e| single_coercion(e) } else single_coercion(value) end end
multiple?()
click to toggle source
# File lib/nested_struct/coercer.rb, line 29 def multiple? type == :multiple end
Private Instance Methods
single_coercion(hash)
click to toggle source
# File lib/nested_struct/coercer.rb, line 39 def single_coercion(hash) raise Errors::InvalidCoercerValue if !hash.is_a?(Hash) target.new(hash) end
valid_target?()
click to toggle source
# File lib/nested_struct/coercer.rb, line 35 def valid_target? target.ancestors.include?(NestedStruct::Interface) end