Class: DriveTime::ClassNameMap

Inherits:
Object
  • Object
show all
Defined in:
lib/drive_time/class_name_map.rb

Overview

This is a fluid two-way map. In both directions it will return a mapping if it exists, otherwise it will return the value passed to it. It does not raise an exception if a mapping is not found.

Instance Method Summary collapse

Constructor Details

#initializeClassNameMap

Returns a new instance of ClassNameMap.



7
8
9
# File 'lib/drive_time/class_name_map.rb', line 7

def initialize
  @map = BiDirectionalHash.new
end

Instance Method Details

#resolve_mapped_from_original(className) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/drive_time/class_name_map.rb', line 20

def resolve_mapped_from_original(className)
  if @map.has_value_for_key className
    @map.value_for_key className
  else
    className
  end
end

#resolve_original_from_mapped(className) ⇒ Object

Check for mapped class



12
13
14
15
16
17
18
# File 'lib/drive_time/class_name_map.rb', line 12

def resolve_original_from_mapped(className)
  if @map.has_key_for_value className
    @map.key_for_value className
  else
    className
  end
end

#save_mapping(class_name, mapped_class_name = nil) ⇒ Object

Accepts String versions of class names eg. ExampleClass



29
30
31
32
33
34
35
36
37
# File 'lib/drive_time/class_name_map.rb', line 29

def save_mapping(class_name, mapped_class_name=nil)
  if mapped_class_name
    # Save mapping so we can look it up from both directions
    @map.insert(class_name, mapped_class_name)
    mapped_class_name
  else
    class_name
  end
end