Module: ActiveEntity::Typecasting
- Included in:
- Coercion
- Defined in:
- lib/active_entity/typecasting.rb
Instance Method Summary collapse
-
#cast ⇒ nil
Try to cast attribute values.
-
#cast! ⇒ nil
Try to cast attribute values.
Instance Method Details
#cast ⇒ nil
Try to cast attribute values. When fails to cast, ignores error and left attribute value as original one.
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/active_entity/typecasting.rb', line 23 def cast defined_attributes.each do |name, attr| value = public_send(name) next if value.nil? begin casted = attr.cast!(value) public_send("#{name}=", casted) rescue ActiveEntity::CastError end end nil end |
#cast! ⇒ nil
Try to cast attribute values. When fails to cast, raises the error and rollbacks all values.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/active_entity/typecasting.rb', line 8 def cast! casted_values = defined_attributes.map do |name, attr| value = public_send(name) next [name, nil] if value.nil? [name, attr.cast!(value)] end casted_values.each {|name, casted| public_send("#{name}=", casted) } nil end |