Module: ActiveEntity::Typecasting
- Included in:
- Coercion
- Defined in:
- lib/active_entity/typecasting.rb
Overview
Offers explicit type conversion. This depends ‘ActiveEntity::Accessor#defined_attributes`.
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.
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/active_entity/typecasting.rb', line 25 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.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/active_entity/typecasting.rb', line 10 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 |