Class: Mapping::Model

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

Direct Known Subclasses

ObjectModel

Constant Summary collapse

PREFIX =
'map_'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.map(klass, &block) ⇒ Object



34
35
36
37
# File 'lib/mapping/model.rb', line 34

def self.map(klass, &block)
  method_name = self.method_for_mapping(klass)
  define_method(method_name, &block)
end

.method_for_mapping(klass) ⇒ Object

This function generates mapping names like map_Array and map_Hash which while a bit non-standard are perfectly fine for our purposes and this never really needs to leak.



26
27
28
# File 'lib/mapping/model.rb', line 26

def self.method_for_mapping(klass)
  PREFIX + klass.name.gsub(/::/, '_')
end

Instance Method Details

#map(root, *args) ⇒ Object



39
40
41
42
43
# File 'lib/mapping/model.rb', line 39

def map(root, *args)
  method_name = self.method_for_mapping(root)

  self.send(method_name, root, *args)
end

#method_for_mapping(object) ⇒ Object



30
31
32
# File 'lib/mapping/model.rb', line 30

def method_for_mapping(object)
  self.class.method_for_mapping(object.class)
end