class Fend::Plugins::Coercions::Coercer

Attributes

coerce[R]

Public Class Methods

new(coerce) click to toggle source
# File lib/fend/plugins/coercions.rb, line 244
def initialize(coerce)
  @coerce = coerce
end

Public Instance Methods

call(data, schema) click to toggle source
# File lib/fend/plugins/coercions.rb, line 248
def call(data, schema)
  data.each_with_object({}) do |(name, value), result|
    type = schema[name]

    result[name] = coerce_value(type, value)
  end
end

Private Instance Methods

coerce_value(type, value) click to toggle source
# File lib/fend/plugins/coercions.rb, line 258
def coerce_value(type, value)
  case type
  when NilClass then value
  when Hash     then process_hash(value, type)
  when Array    then process_array(value, type.first)
  else
    coerce.to(type, value)
  end
end
process_array(input, member_schema) click to toggle source
# File lib/fend/plugins/coercions.rb, line 276
def process_array(input, member_schema)
  coerced_value = coerce_value(:array, input)

  return coerced_value unless coerced_value.is_a?(Array)

  coerced_value.each_with_object([]) do |member, result|
    value = member
    type  = member_schema.is_a?(Array) ? member_schema.first : member_schema

    coerced_member_value = coerce_value(type, value)

    next if coerced_member_value.nil?

    result << coerced_member_value
  end
end
process_hash(input, schema) click to toggle source
# File lib/fend/plugins/coercions.rb, line 268
def process_hash(input, schema)
  coerced_value = coerce_value(:hash, input)

  return coerced_value unless coerced_value.is_a?(Hash)

  call(coerced_value, schema)
end