Class: GOM::Object::Mapping

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

Overview

Provides a mapping between objects and ids

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMapping

Returns a new instance of Mapping.



5
6
7
# File 'lib/gom/object/mapping.rb', line 5

def initialize
  @map = { }
end

Class Method Details

.method_missing(method_name, *arguments, &block) ⇒ Object



48
49
50
# File 'lib/gom/object/mapping.rb', line 48

def self.method_missing(method_name, *arguments, &block)
  self.singleton.send method_name, *arguments, &block
end

.singletonObject



44
45
46
# File 'lib/gom/object/mapping.rb', line 44

def self.singleton
  @mapping ||= self.new
end

Instance Method Details

#clear!Object



34
35
36
# File 'lib/gom/object/mapping.rb', line 34

def clear!
  @map.clear
end

#id_by_object(object) ⇒ Object



18
19
20
# File 'lib/gom/object/mapping.rb', line 18

def id_by_object(object)
  @map[object]
end

#inspectObject



38
39
40
41
42
# File 'lib/gom/object/mapping.rb', line 38

def inspect
  output = ""
  @map.each{ |object, id| output += object.inspect + " => " + id.to_s + "\n" }
  output
end

#object_by_id(id) ⇒ Object



14
15
16
# File 'lib/gom/object/mapping.rb', line 14

def object_by_id(id)
  @map.respond_to?(:key) ? @map.key(id) : @map.index(id)
end

#put(object, id) ⇒ Object



9
10
11
12
# File 'lib/gom/object/mapping.rb', line 9

def put(object, id)
  return if object.nil? || id.nil?
  @map[object] = id
end

#remove_by_id(id) ⇒ Object



22
23
24
# File 'lib/gom/object/mapping.rb', line 22

def remove_by_id(id)
  @map.delete object_by_id(id)
end

#remove_by_object(object) ⇒ Object



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

def remove_by_object(object)
  @map.delete object
end

#sizeObject



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

def size
  @map.size
end