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.
-
.element_type ⇒ Object
The element type of the Array type.
Instance Method Summary collapse
-
#==(other) ⇒ ::Boolean
Compares the instance type with another type.
-
#===(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.
41 42 43 44 45 46 47 48 49 |
# File 'lib/parameters/types/array.rb', line 41 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
#==(other) ⇒ ::Boolean
Compares the instance type with another type.
73 74 75 |
# File 'lib/parameters/types/array.rb', line 73 def ==(other) super(other) && (@element_type == other.element_type) end |
#===(value) ⇒ ::Boolean
Determines if the value is an Array.
85 86 87 88 89 |
# File 'lib/parameters/types/array.rb', line 85 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.
102 103 104 105 106 107 |
# File 'lib/parameters/types/array.rb', line 102 def coerce(value) array = super(value) array.map! { |element| @element_type.coerce(element) } return array end |