class Mova::Storage::Chain
Allows to wrap several storages and treat them as one. All methods are called on each storage in order defined in the initializer.
@since 0.1.0
Attributes
storages[R]
Public Class Methods
new(*storages)
click to toggle source
# File lib/mova/storage/chain.rb, line 10 def initialize(*storages) @storages = storages # Performance optimizations: # * replace loop with OR operator in places where we know beforehand # all iterated elements (storages) # * avoid reading from the next storage if possible instance_eval <<-EOM, __FILE__, __LINE__ + 1 def read(key) #{ calls_to_each_storage = storages.map.each_with_index do |s, i| "Mova.presence(storages[#{i}].read(key))" end calls_to_each_storage.join(" || ") } end def read_multi(*keys) #{ initialize_results = storages.map.each_with_index do |s, i| "results#{i} = nil" end initialize_results.join("\n") } keys.each_with_object({}) do |key, memo| result = \ #{ calls_to_each_storage = storages.map.each_with_index do |s, i| "Mova.presence((results#{i} ||= storages[#{i}].read_multi(*keys))[key])" end calls_to_each_storage.join(" || ") } memo[key] = result if result end end EOM end
Public Instance Methods
clear()
click to toggle source
@return [void]
@note Each storage will receive clear
. Use {Readonly} if you wish to protect
certain storages.
# File lib/mova/storage/chain.rb, line 108 def clear storages.each { |s| s.clear } end
exist?(key)
click to toggle source
@return [Boolean] @param key [String]
# File lib/mova/storage/chain.rb, line 114 def exist?(key) storages.any? { |s| s.exist?(key) } end
inspect()
click to toggle source
@private
# File lib/mova/storage/chain.rb, line 119 def inspect "<##{self.class.name} storages=#{storages.inspect}>" end
write(key, value)
click to toggle source
@return [void] @param key [String] @param value [String, nil]
@note Each storage will receive write
. Use {Readonly} if you wish to protect
certain storages.
# File lib/mova/storage/chain.rb, line 100 def write(key, value) storages.each { |s| s.write(key, value) } end