Class: MemoryPersister::MemoryContainer

Inherits:
Object
  • Object
show all
Defined in:
lib/persisters/memory_persister.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass_list, options = {}) ⇒ MemoryContainer

Returns a new instance of MemoryContainer.



45
46
47
48
49
# File 'lib/persisters/memory_persister.rb', line 45

def initialize(klass_list, options = {})
  klass_list.each {|klass| make_index_getters klass }
  @capacity = options[:capacity] || 65535
  clear
end

Instance Method Details

#absorb(instance) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/persisters/memory_persister.rb', line 51

def absorb(instance)
  raise MemoryPersister::Full if self.capacity_left <= 0
  klass = instance.class
  klass.get_instance_indices.each do |index|
    instances[klass.to_s]["__#{index}"][instance.send(index)] = instance
  end
  @capacity_used += 1
end

#capacity_leftObject



60
61
62
# File 'lib/persisters/memory_persister.rb', line 60

def capacity_left
  @capacity - @capacity_used
end

#clearObject



64
65
66
67
# File 'lib/persisters/memory_persister.rb', line 64

def clear
  @instances = new_instances_hash
  @capacity_used = 0
end