class BPS::Coercer

Constants

TRUE_VALUES

Attributes

schema[R]

Public Class Methods

new(schema) click to toggle source
# File lib/bps/coercer.rb, line 9
def initialize(schema)
  validate!(schema)
  @schema = schema
end

Public Instance Methods

coerce(hash) click to toggle source
# File lib/bps/coercer.rb, line 14
def coerce(hash)
  coerce_with(@schema, hash)
end

Private Instance Methods

coerce_value(type, val) click to toggle source
# File lib/bps/coercer.rb, line 45
def coerce_value(type, val)
  case type
  when :string
    val&.to_s
  when :symbol
    val&.to_sym
  when :int
    val&.to_i
  when :float
    val&.to_f
  when :bool
    val.nil? ? nil : TRUE_VALUES.include?(val)
  when Hash
    val.is_a?(Hash) ? coerce_with(type, val) : nil
  when Array
    Array(val).map {|v| coerce_value(type[0], v) }
  end
end
coerce_with(schema, hash) click to toggle source
# File lib/bps/coercer.rb, line 35
def coerce_with(schema, hash)
  clone = {}
  schema.each do |key, type|
    next unless hash.key?(key)

    clone[key] = coerce_value(type, hash[key])
  end
  clone
end
validate!(schema) click to toggle source
# File lib/bps/coercer.rb, line 20
def validate!(schema)
  schema.each do |key, type|
    case type
    when :string, :symbol, :int, :float, :bool
      # OK
    when Hash
      validate!(type)
    when Array
      raise ArgumentError, "Array types must have exactly one entry, but was (#{key} => #{type.inspect})" unless type.size == 1
    else
      raise ArgumentError, "Unknown type #{type.inspect}"
    end
  end
end