Class: EasyParams::Types::Collection

Inherits:
Generic
  • Object
show all
Includes:
Enumerable
Defined in:
lib/easy_params/types/collection.rb

Overview

base interface for array type

Direct Known Subclasses

StructsCollection

Instance Method Summary collapse

Methods inherited from Generic

#array?

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

#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