Class: EasyParams::Types::Collection
- Includes:
- Enumerable
- Defined in:
- lib/easy_params/types/collection.rb
Overview
base interface for array type
Direct Known Subclasses
Instance Method Summary collapse
- #[](index) ⇒ Object
- #coerce(value) ⇒ Object
- #default(value) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(*attrs, of: nil) ⇒ Collection
constructor
A new instance of Collection.
- #normalize(&block) ⇒ Object
- #of(of_type) ⇒ Object
Methods inherited from Generic
Constructor Details
#initialize(*attrs, of: nil) ⇒ Collection
Returns a new instance of Collection.
9 10 11 12 |
# File 'lib/easy_params/types/collection.rb', line 9 def initialize(*attrs, of: nil) super(*attrs) @of_type = of end |
Instance Method Details
#[](index) ⇒ Object
37 38 39 |
# File 'lib/easy_params/types/collection.rb', line 37 def [](index) @default[index] end |
#coerce(value) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/easy_params/types/collection.rb', line 18 def coerce(value) input = value || @default input = @normalize_proc.call(Array(input)) if @normalize_proc coerced = Array(input).map { |v| @of_type.coerce(v) } self.class.new(@title, coerced, @normalize_proc, of: @of_type, &@coerce_proc) end |
#default(value) ⇒ Object
29 30 31 |
# File 'lib/easy_params/types/collection.rb', line 29 def default(value) self.class.new(@title, value, @normalize_proc, of: @of_type, &@coerce_proc) end |
#each(&block) ⇒ Object
33 34 35 |
# File 'lib/easy_params/types/collection.rb', line 33 def each(&block) @default.each(&block) end |
#normalize(&block) ⇒ Object
25 26 27 |
# File 'lib/easy_params/types/collection.rb', line 25 def normalize(&block) self.class.new(@title, @default, block, of: @of_type, &@coerce_proc) end |
#of(of_type) ⇒ Object
14 15 16 |
# File 'lib/easy_params/types/collection.rb', line 14 def of(of_type) self.class.new(@title, @default, @normalize_proc, of: of_type, &@coerce_proc) end |