Class: Dinamo::Model::Caster
- Inherits:
-
Object
- Object
- Dinamo::Model::Caster
- Defined in:
- lib/dinamo/model/caster.rb
Defined Under Namespace
Classes: Any
Instance Attribute Summary collapse
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Instance Method Summary collapse
- #associate(key, type) ⇒ Object
- #cast(attributes) ⇒ Object
-
#initialize ⇒ Caster
constructor
A new instance of Caster.
- #register(type, &block) ⇒ Object
- #supported_type?(type) ⇒ Boolean
- #supported_types ⇒ Object
Constructor Details
#initialize ⇒ Caster
Returns a new instance of Caster.
8 9 10 |
# File 'lib/dinamo/model/caster.rb', line 8 def initialize @types = {} end |
Instance Attribute Details
#types ⇒ Object (readonly)
Returns the value of attribute types.
6 7 8 |
# File 'lib/dinamo/model/caster.rb', line 6 def types @types end |
Instance Method Details
#associate(key, type) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/dinamo/model/caster.rb', line 35 def associate(key, type) fail Exceptions::UnsupportedTypeError, "%p type is not supported" % type unless @types.keys.include?(type) current = @types[type] current.support(key) end |
#cast(attributes) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/dinamo/model/caster.rb', line 16 def cast(attributes) attributes.each_with_object({}) do |(key, value), casted| casted[key] = if found = @types.find { |_, any| any.supported_key?(key) } found.pop.cast(value) else value end end end |
#register(type, &block) ⇒ Object
12 13 14 |
# File 'lib/dinamo/model/caster.rb', line 12 def register(type, &block) @types[type] = Any.new(type, &block) end |
#supported_type?(type) ⇒ Boolean
27 28 29 |
# File 'lib/dinamo/model/caster.rb', line 27 def supported_type?(type) supported_types.include?(type) end |
#supported_types ⇒ Object
31 32 33 |
# File 'lib/dinamo/model/caster.rb', line 31 def supported_types @types.keys end |