Method: Formant::FormObject.parse
- Defined in:
- lib/formant.rb
.parse(field_name, options = {}) ⇒ Object
Directive to add a parse rule for the specified attribute. Parse rules let you apply any special parsing/transformation logic on the form’s attributes upon input. The parsing rules are applied automatically prior to validation.
Usage example:
class MyForm < FormObject
...
parse :appointment_time, as: :datetime
parse :phone, as: :phone_number
parse :price, as: :currency
parse :email, to: :strip_whitespace
...
end
The above example specifies that the appointment_time attribute should be parsed with the parse_datetime method (which converts the datetime string into an ActiveSupport::TimeWithZone object), and that the # phone attribute should be parsed with the parse_phone_number method (which normalizes the phone number into a standard format).
59 60 61 62 63 64 |
# File 'lib/formant.rb', line 59 def self.parse(field_name, ={}) self.parse_fields ||= [] parse_type = [:as] || [:to] raise "no parse type provided" if parse_type.blank? self.parse_fields << [ field_name, "parse_#{parse_type}", ] end |