class Shrink::Wrap::Property::Coercion::Enumerable

Attributes

enumerable[RW]

Public Class Methods

new(enumerable) click to toggle source
# File lib/shrink/wrap/property/coercion/enumerable.rb, line 12
def initialize(enumerable)
  ensure_type!(::Enumerable, enumerable)
  self.enumerable = wrap(enumerable.first)
end

Public Instance Methods

coerce(data) click to toggle source
# File lib/shrink/wrap/property/coercion/enumerable.rb, line 17
def coerce(data)
  return coerce_hash(data) if enumerable.size > 1

  coerce_enumerable(data)
end

Private Instance Methods

coerce_enumerable(data) click to toggle source
# File lib/shrink/wrap/property/coercion/enumerable.rb, line 35
def coerce_enumerable(data)
  ensure_type!(::Enumerable, data)
  klass = enumerable.first
  data.map do |element|
    Class.new(klass).coerce(element)
  end
end
coerce_hash(data) click to toggle source
# File lib/shrink/wrap/property/coercion/enumerable.rb, line 25
def coerce_hash(data)
  ensure_type!(Hash, data)
  key_klass, value_klass = enumerable
  data.each_with_object({}) do |(key, value), memo|
    coerced_key = Class.new(key_klass).coerce(key)
    coerced_value = Class.new(value_klass).coerce(value)
    memo[coerced_key] = coerced_value
  end
end
wrap(element) click to toggle source
# File lib/shrink/wrap/property/coercion/enumerable.rb, line 43
def wrap(element)
  [element].flatten
end