Method: ActiveModel::Datastore::PropertyValues#format_property_value
- Defined in:
- lib/active_model/datastore/property_values.rb
#format_property_value(attr, type) ⇒ Object
Converts the type of the property.
Example:
format_property_value :weight, :float
is equivalent to:
self.weight = weight.to_f if weight.present?
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/active_model/datastore/property_values.rb', line 39 def format_property_value(attr, type) return unless send(attr.to_sym).present? case type.to_sym when :integer send("#{attr.to_sym}=", send(attr.to_sym).to_i) when :float send("#{attr.to_sym}=", send(attr.to_sym).to_f) when :boolean send("#{attr.to_sym}=", ActiveModel::Type::Boolean.new.cast(send(attr.to_sym))) else raise ArgumentError, 'Supported types are :boolean, :integer, :float' end end |