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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Generic.



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

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

Instance Attribute Details

#normalize_procObject (readonly)

Returns the value of attribute normalize_proc.



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

def normalize_proc
  @normalize_proc
end

Instance Method Details

#array?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/easy_params/types/generic.rb', line 16

def array?
  @title == :array
end

#coerce(value) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/easy_params/types/generic.rb', line 28

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



20
21
22
# File 'lib/easy_params/types/generic.rb', line 20

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

#normalize(&block) ⇒ Object



24
25
26
# File 'lib/easy_params/types/generic.rb', line 24

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