Class: ValueSemantics::ArrayCoercer

Inherits:
Object
  • Object
show all
Defined in:
lib/value_semantics/array_coercer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element_coercer = nil) ⇒ ArrayCoercer

Returns a new instance of ArrayCoercer.



5
6
7
8
# File 'lib/value_semantics/array_coercer.rb', line 5

def initialize(element_coercer = nil)
  @element_coercer = element_coercer
  freeze
end

Instance Attribute Details

#element_coercerObject (readonly)

Returns the value of attribute element_coercer.



3
4
5
# File 'lib/value_semantics/array_coercer.rb', line 3

def element_coercer
  @element_coercer
end

Instance Method Details

#call(obj) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/value_semantics/array_coercer.rb', line 10

def call(obj)
  if obj.respond_to?(:to_a)
    array = obj.to_a
    if element_coercer
      array.map { |element| element_coercer.call(element) }
    else
      array
    end
  else
    obj
  end
end