Class: Parameters::Types::Array
- Defined in:
- lib/parameters/types/array.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#element_type ⇒ Object
readonly
The type to coerce all Array elements with.
Class Method Summary collapse
-
.coerce(value) ⇒ ::Array
Coerces a value into an Array.
Instance Method Summary collapse
-
#===(value) ⇒ ::Boolean
Determines if the value is an Array.
-
#coerce(value) ⇒ ::Array
Coerces a value into an Array, and coerces the elements of the Array.
-
#initialize(element_type) ⇒ Array
constructor
Initializes the Array type.
-
#to_ruby ⇒ Array<Class>
The Ruby Type for the Array Type instance.
Methods inherited from Object
Methods inherited from Type
Constructor Details
#initialize(element_type) ⇒ Array
Initializes the Array type.
16 17 18 |
# File 'lib/parameters/types/array.rb', line 16 def initialize(element_type) @element_type = element_type end |
Instance Attribute Details
#element_type ⇒ Object (readonly)
The type to coerce all Array elements with
8 9 10 |
# File 'lib/parameters/types/array.rb', line 8 def element_type @element_type end |
Class Method Details
.coerce(value) ⇒ ::Array
Coerces a value into an Array.
29 30 31 32 33 34 35 36 37 |
# File 'lib/parameters/types/array.rb', line 29 def self.coerce(value) if value.respond_to?(:to_a) value.to_a elsif value.respond_to?(:to_ary) value.to_ary else [value] end end |
Instance Method Details
#===(value) ⇒ ::Boolean
Determines if the value is an Array.
57 58 59 60 61 |
# File 'lib/parameters/types/array.rb', line 57 def ===(value) (self.class === value) && value.all? { |element| @element_type === element } end |
#coerce(value) ⇒ ::Array
Coerces a value into an Array, and coerces the elements of the Array.
74 75 76 77 78 79 |
# File 'lib/parameters/types/array.rb', line 74 def coerce(value) array = super(value) array.map! { |element| @element_type.coerce(element) } return array end |