Class: NestedStruct::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/nested_struct/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, coercer_expression = nil) ⇒ Field

Returns a new instance of Field.



5
6
7
8
# File 'lib/nested_struct/field.rb', line 5

def initialize(name, coercer_expression = nil)
  @name = name
  @coercer = Coercer.new(coercer_expression) if coercer_expression
end

Instance Attribute Details

#coercerObject (readonly)

Returns the value of attribute coercer.



3
4
5
# File 'lib/nested_struct/field.rb', line 3

def coercer
  @coercer
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/nested_struct/field.rb', line 3

def name
  @name
end

Instance Method Details

#coerced?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/nested_struct/field.rb', line 16

def coerced?
  !coercer.nil?
end

#with(value) ⇒ Object



10
11
12
13
14
# File 'lib/nested_struct/field.rb', line 10

def with(value)
  return value if value.nil?
  return value if !coerced?
  coercer.coerce(value)
end