Class: DataMapper::TypeMap

Inherits:
Object show all
Defined in:
lib/gems/dm-core-0.9.9/lib/dm-core/type_map.rb

Defined Under Namespace

Classes: Error, TypeChain

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil, &blk) ⇒ TypeMap

Returns a new instance of TypeMap.



8
9
10
11
12
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/type_map.rb', line 8

def initialize(parent = nil, &blk)
  @parent, @chains = parent, {}

  blk.call(self) unless blk.nil?
end

Instance Attribute Details

#chainsObject

Returns the value of attribute chains.



6
7
8
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/type_map.rb', line 6

def chains
  @chains
end

#parentObject

Returns the value of attribute parent.



6
7
8
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/type_map.rb', line 6

def parent
  @parent
end

Instance Method Details

#lookup(type) ⇒ Object Also known as: []



18
19
20
21
22
23
24
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/type_map.rb', line 18

def lookup(type)
  if type_mapped?(type)
    lookup_from_map(type)
  else
    lookup_by_type(type)
  end
end

#lookup_by_type(type) ⇒ Object

Raises:



39
40
41
42
43
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/type_map.rb', line 39

def lookup_by_type(type)
  raise DataMapper::TypeMap::Error.new(type) unless type.respond_to?(:primitive) && !type.primitive.nil?

  lookup(type.primitive).merge(Type::PROPERTY_OPTIONS.inject({}) {|h, k| h[k] = type.send(k); h})
end

#lookup_from_map(type) ⇒ Object



26
27
28
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/type_map.rb', line 26

def lookup_from_map(type)
  lookup_from_parent(type).merge(map(type).translate)
end

#lookup_from_parent(type) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/type_map.rb', line 30

def lookup_from_parent(type)
  if !@parent.nil? && @parent.type_mapped?(type)
    @parent[type]
  else
    {}
  end
end

#map(type) ⇒ Object



14
15
16
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/type_map.rb', line 14

def map(type)
  @chains[type] ||= TypeChain.new
end

#type_mapped?(type) ⇒ Boolean

Returns:



47
48
49
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/type_map.rb', line 47

def type_mapped?(type)
  @chains.has_key?(type) || (@parent.nil? ? false : @parent.type_mapped?(type))
end