Class: EasyParams::Types::Generic

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_params/types/generic.rb

Overview

base interface for simple types

Direct Known Subclasses

Collection

Instance Method Summary collapse

Constructor Details

#initialize(title, default = nil, normalize_proc = nil, &coerce_proc) ⇒ Generic

Returns a new instance of Generic.



11
12
13
14
15
16
# File 'lib/easy_params/types/generic.rb', line 11

def initialize(title, default = nil, normalize_proc = nil, &coerce_proc)
  @title = title
  @default = default
  @coerce_proc = coerce_proc
  @normalize_proc = normalize_proc
end

Instance Method Details

#array?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/easy_params/types/generic.rb', line 7

def array?
  @title == :array
end

#coerce(value) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/easy_params/types/generic.rb', line 34

def coerce(value)
  value = @normalize_proc.call(value) if @normalize_proc
  return @default if value.nil?

  @coerce_proc.call(value)
rescue StandardError
  @default
end

#default(value) ⇒ Object



18
19
20
# File 'lib/easy_params/types/generic.rb', line 18

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

#normalize(&block) ⇒ Object



30
31
32
# File 'lib/easy_params/types/generic.rb', line 30

def normalize(&block)
  self.class.new(@title, @default, block, &@coerce_proc)
end

#optionalObject



22
23
24
# File 'lib/easy_params/types/generic.rb', line 22

def optional
  self.class.new(@title, @default, @normalize_proc, &@coerce_proc)
end

#optional?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/easy_params/types/generic.rb', line 26

def optional?
  @optional
end