Class: Edr::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/edr/registry.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Registry

Returns a new instance of Registry.



17
18
19
20
21
# File 'lib/edr/registry.rb', line 17

def initialize(&block)
  @data_to_model_map = {}
  @model_to_data_map = {}
  self.instance_eval &block
end

Class Method Details

.data_class_for(model_class) ⇒ Object



9
10
11
# File 'lib/edr/registry.rb', line 9

def self.data_class_for(model_class)
  @@instance.data_class_for(model_class)
end

.define(&block) ⇒ Object



5
6
7
# File 'lib/edr/registry.rb', line 5

def self.define(&block)
  @@instance = self.new(&block)
end

.model_class_for(data_class) ⇒ Object



13
14
15
# File 'lib/edr/registry.rb', line 13

def self.model_class_for(data_class)
  @@instance.model_class_for(data_class)
end

Instance Method Details

#data_class_for(model_class) ⇒ Object



30
31
32
33
34
35
# File 'lib/edr/registry.rb', line 30

def data_class_for model_class
  data_class = @model_to_data_map[model_class.to_s]
  return OpenStruct unless data_class

  data_class.constantize
end

#map(model_class, data_class) ⇒ Object

the mapping is done by the class names, to avoid class-reloading issues in Rails dev mode



25
26
27
28
# File 'lib/edr/registry.rb', line 25

def map model_class, data_class
  @model_to_data_map[model_class.to_s] = data_class.to_s
  @data_to_model_map[data_class.to_s] = model_class.to_s
end

#model_class_for(data_class) ⇒ Object



37
38
39
40
41
42
# File 'lib/edr/registry.rb', line 37

def model_class_for data_class
  model_class = @data_to_model_map[data_class.to_s]
  raise "No model class for #{data_class.to_s}" unless model_class

  model_class.constantize
end