Eno::Loaders
enorb provides a set of core loaders for important types that are available out of the box (in addition to the possiblity to define your own custom loaders, which can be bootstrapped and made accessible directly through the core loader API or be passed as arguments, e.g. as lambdas, to all accessors).
The loaders are exposed through the API as drop in replacements to the standard accessors:
Eno::Field
#value
=>#[loader_name]
Eno::Fieldset
#entry
=>#[loader_name]
Eno::List
#items
=>#[loader_name]_items
Eno::Section
#field
=>#[loader_name]
#list
=>#[loader_name]_list
So for instance, instead of calling ...
document.field('done', required: true) #=> 'yes'
document.list('visitor_counts', required: true) #=> [ '476', '213', '330', ... ]
... you can just replace field
or augment list
with the loader name ...
document.boolean('done', required: true) #=> true
document.integer_list('visitor_counts', required: true) #=> [ 476, 213, 330, ... ]
... and the method signature stays exactly the same as for the original accessor (except you can't provide a loader as an argument anymore).
Here's another full example:
require 'enorb'
doc = parse(
<<~DOC
publish: yes
location: 36.987094, -25.091719
contact: contact@faulty
DOC
)
doc.boolean('publish')
#=> true
doc.lat_lng('location')
#=>
# {
# lat: 36.987094,
# lng: -25.091719
# }
doc.email('contact')
# raises an error: 'contact' must contain a valid email address, for instance 'jane.doe@eno-lang.org'.
Note that some loaders only perform validation and return their input unaltered
as string (e.g. color
, email
), while others both validate and transform the
value into a new type (e.g. float
, boolean
) or even object (e.g. lat_lng
).
Subpages
#boolean#color
#comma_separated
#date
#datetime
#float
#integer
#json
#lat_lng
#number
#string
#url