Class: Strict::Coercers::Array

Inherits:
Object
  • Object
show all
Defined in:
lib/strict/coercers/array.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element_coercer = nil) ⇒ Array

Returns a new instance of Array.



8
9
10
# File 'lib/strict/coercers/array.rb', line 8

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

Instance Attribute Details

#element_coercerObject (readonly)

Returns the value of attribute element_coercer.



6
7
8
# File 'lib/strict/coercers/array.rb', line 6

def element_coercer
  @element_coercer
end

Instance Method Details

#call(value) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/strict/coercers/array.rb', line 12

def call(value)
  return value if value.nil? || !value.respond_to?(:to_a)

  array = value.to_a
  return array unless element_coercer

  array.map { |element| element_coercer.call(element) }
end