Class: Dinamo::Model::Caster

Inherits:
Object
  • Object
show all
Defined in:
lib/dinamo/model/caster.rb

Defined Under Namespace

Classes: Any

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCaster

Returns a new instance of Caster.



8
9
10
# File 'lib/dinamo/model/caster.rb', line 8

def initialize
  @types = {}
end

Instance Attribute Details

#typesObject (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

Returns:

  • (Boolean)


27
28
29
# File 'lib/dinamo/model/caster.rb', line 27

def supported_type?(type)
  supported_types.include?(type)
end

#supported_typesObject



31
32
33
# File 'lib/dinamo/model/caster.rb', line 31

def supported_types
  @types.keys
end