Module: StrokeDB::ChainableStorage
- Included in:
- ChunkStorage
- Defined in:
- lib/stores/chainable_storage.rb
Instance Method Summary collapse
- #add_chained_storage!(storage) ⇒ Object
- #has_chained_storage?(storage) ⇒ Boolean
- #remove_chained_storage!(storage) ⇒ Object
- #save_with_chained_storages!(chunk, source = nil) ⇒ Object
- #save_without_chained_storages!(chunk, source = nil) ⇒ Object (also: #save!)
- #sync_chained_storage!(storage) ⇒ Object
- #sync_chained_storages!(origin = nil) ⇒ Object
Instance Method Details
#add_chained_storage!(storage) ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/stores/chainable_storage.rb', line 3 def add_chained_storage!(storage) @chained_storages ||= {} @chained_storages[storage] = [] class <<self alias :save! :save_with_chained_storages! end storage.add_chained_storage!(self) unless storage.has_chained_storage?(self) end |
#has_chained_storage?(storage) ⇒ Boolean
22 23 24 |
# File 'lib/stores/chainable_storage.rb', line 22 def has_chained_storage?(storage) @chained_storages.nil? ? false : !!@chained_storages[storage] end |
#remove_chained_storage!(storage) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/stores/chainable_storage.rb', line 12 def remove_chained_storage!(storage) @chained_storages.delete(storage) storage.remove_chained_storage!(self) if storage.has_chained_storage?(self) if @chained_storages.keys.empty? class <<self alias :save! :save_without_chained_storages! end end end |
#save_with_chained_storages!(chunk, source = nil) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/stores/chainable_storage.rb', line 48 def save_with_chained_storages!(chunk, source=nil) perform_save!(chunk) (@chained_storages||{}).each_pair do |storage, savings| savings << chunk unless storage == source || savings.include?(chunk) # TODO: here we had a bug (storage == document), spec it end end |
#save_without_chained_storages!(chunk, source = nil) ⇒ Object Also known as: save!
44 45 46 |
# File 'lib/stores/chainable_storage.rb', line 44 def save_without_chained_storages!(chunk, source=nil) perform_save!(chunk) end |
#sync_chained_storage!(storage) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/stores/chainable_storage.rb', line 36 def sync_chained_storage!(storage) return unless @chained_storages.is_a?(Hash) (@chained_storages[storage]||[]).each do |saving| storage.save!(saving, self) end @chained_storages[storage] = [] end |
#sync_chained_storages!(origin = nil) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/stores/chainable_storage.rb', line 26 def sync_chained_storages!(origin=nil) return unless @chained_storages.is_a?(Hash) @chained_storages.each_pair do |storage, savings| next if storage == origin savings.each {|saving| storage.save!(saving, self)} storage.sync_chained_storages!(self) @chained_storages[storage] = [] end end |