Class: YADM::IdentityMap

Inherits:
Object
  • Object
show all
Defined in:
lib/yadm/identity_map.rb

Instance Method Summary collapse

Constructor Details

#initialize(data_source) ⇒ IdentityMap

Returns a new instance of IdentityMap.



6
7
8
9
10
11
12
# File 'lib/yadm/identity_map.rb', line 6

def initialize(data_source)
  @data_source = data_source
  
  @map = Hash.new do |map, collection|
    map[collection] = {}
  end
end

Instance Method Details

#add(collection, attributes) ⇒ Object



18
19
20
21
22
# File 'lib/yadm/identity_map.rb', line 18

def add(collection, attributes)
  data_source.add(collection, attributes).tap do |id|
    map[collection][id] = attributes.merge(id: id)
  end
end

#change(collection, id, new_attributes) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/yadm/identity_map.rb', line 24

def change(collection, id, new_attributes)
  data_source.change(collection, id, new_attributes)
  
  if attributes = map[collection][id]
    attributes.update(new_attributes)
  else
    map[collection][id] = new_attributes.merge(id: id)
  end
end

#count(collection) ⇒ Object



39
40
41
# File 'lib/yadm/identity_map.rb', line 39

def count(collection)
  data_source.count(collection)
end

#get(collection, id) ⇒ Object



14
15
16
# File 'lib/yadm/identity_map.rb', line 14

def get(collection, id)
  map[collection][id] ||= data_source.get(collection, id).dup
end

#migrate(block) ⇒ Object



47
48
49
# File 'lib/yadm/identity_map.rb', line 47

def migrate(block)
  data_source.migrate(block)
end

#remove(collection, id) ⇒ Object



34
35
36
37
# File 'lib/yadm/identity_map.rb', line 34

def remove(collection, id)
  data_source.remove(collection, id)
  map[collection].delete(id)
end

#send_query(collection, query) ⇒ Object



43
44
45
# File 'lib/yadm/identity_map.rb', line 43

def send_query(collection, query)
  data_source.send_query(collection, query)
end