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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Generic

#array?, #optional, #optional?

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

Class Method Details

.optionalObject



29
30
31
# File 'lib/easy_params/types/collection.rb', line 29

def self.optional
  self.class.new(@title, @default, @normalize_proc, of: @of_type, &@coerce_proc)
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)
  value = @normalize_proc.call(Array(value)) if @normalize_proc
  self.class.new(@title, Array(value).map do |v|
                           @of_type.coerce(v)
                         end, @normalize_proc, of: @of_type, &@coerce_proc)
end

#default(value) ⇒ Object



33
34
35
# File 'lib/easy_params/types/collection.rb', line 33

def default(value)
  self.class.new(@title, value, @normalize_proc, of: @of_type, &@coerce_proc)
end

#each(&block) ⇒ Object



37
38
39
# File 'lib/easy_params/types/collection.rb', line 37

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