class Primalize::Single::Object

Public Class Methods

new(types, &coercion) click to toggle source
# File lib/primalize/single.rb, line 285
def initialize types, &coercion
  @types = types
  @coercion = coercion
end

Public Instance Methods

===(value) click to toggle source
# File lib/primalize/single.rb, line 290
def === value
  return false unless ::Hash === value

  @types.all? do |attr, type|
    type === value[attr]
  end
end
coerce(hash) click to toggle source
# File lib/primalize/single.rb, line 298
def coerce hash
  if @coercion
    return @coercion.call(hash)
  end

  if hash.respond_to? :each_with_object
    hash.each_with_object({}) do |(key, value), h|
      type = @types[key]

      h[key] = if type.respond_to? :coerce
                 type.coerce(value)
               else
                 value
               end
    end
  else
    hash
  end
end
inspect() click to toggle source
# File lib/primalize/single.rb, line 318
def inspect
  "object(#{@types.map { |attr, type| "#{attr}: #{type.inspect}" }.join(', ')})"
end