Eno::Section » #field
field(name) → value or nil
field(name, options) → object/value or nil
field(name, loader_proc, options) → value or nil
field(name, options) { |name, value| loader_block } → object/value or nil
Retrieve a field's value from the section, optionally supplying a loader to validate and/or transform the value, and/or an options object.
enocolor: blue
sound:
rubysection.field('color') #=> 'blue'
section.field('sound') #=> nil
section.field('sound', required: true) # raises an error
section.field('sound', enforce_value: true) # raises an error
section.field('sound', enforce_element: true) #=> nil
section.field('texture', enforce_element: true) # raises an error
section.field('color') { |name, value| value.upcase } #=> 'BLUE'
section.field('color') do |name, value|
raise 'Only green is allowed!' if value != 'green' # raises an error
value
end
section.field('color', with_element: true)
#=> [ #<Eno::Field name="color" value="blue">, 'blue' ]
Parameters
nameA string representing the name of the field to return.
loader_block or loader_procA block or Proc
returning the transformed/validated value or raising an error.
enforce_element
Whether the field must be present in the document (true
or false
, defaults to false
)
enforce_value
Whether there must be a value or the field is allowed to be empty (defaults to false
)
required
Alias for enforce_value
(this exists on many methods and depending on context refers to either element or value)
with_element
Whether to return an array of the form [element, value]
instead of just the value (default false
)
Return value
The value of the field, or nil
if empty, or an array of the form [element, value]
when the option with_element
is specified.