Class: GraphMediator::Mediator::IndexedHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/graph_mediator/mediator.rb

Direct Known Subclasses

ChangesHash

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ IndexedHash



13
14
15
16
# File 'lib/graph_mediator/mediator.rb', line 13

def initialize(*args, &block)
  @index = {}
  super
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



10
11
12
# File 'lib/graph_mediator/mediator.rb', line 10

def index
  @index
end

#klassObject (readonly)

Returns the value of attribute klass.



11
12
13
# File 'lib/graph_mediator/mediator.rb', line 11

def klass
  @klass
end

Instance Method Details

#<<(ar_instance, klass, changes) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/graph_mediator/mediator.rb', line 18

def <<(ar_instance, klass, changes)
  add_to_index(changes)
  case
    when ar_instance.new_record? then
      created_array = self[:_created] ||= []
      created_array << changes
    when ar_instance.destroyed? then
      destroyed_array = self[:_destroyed] ||= []
      destroyed_array << ar_instance.id
    else self[ar_instance.id] = changes
  end 
end

#add_to_index(changes) ⇒ Object



31
32
33
# File 'lib/graph_mediator/mediator.rb', line 31

def add_to_index(changes)
  index.merge!(changes)
end