Class: Mova::Storage::Chain
- Inherits:
-
Object
- Object
- Mova::Storage::Chain
- Defined in:
- lib/mova/storage/chain.rb
Overview
Allows to wrap several storages and treat them as one. All methods are called on each storage in order defined in the initializer.
Instance Attribute Summary collapse
- #storages ⇒ Object readonly
Instance Method Summary collapse
- #clear ⇒ void
- #exist?(key) ⇒ Boolean
-
#initialize(*storages) ⇒ Chain
constructor
A new instance of Chain.
-
#read(key) ⇒ String?
First non-empty value while trying each storage in order defined in the initializer.
-
#read_multi(*keys) ⇒ Hash{String => String}
Composed result of all non-empty values.
- #write(key, value) ⇒ void
Constructor Details
#initialize(*storages) ⇒ Chain
Returns a new instance of Chain.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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 " def read(key)\n \#{\n calls_to_each_storage = storages.map.each_with_index do |s, i|\n \"Mova.presence(storages[\#{i}].read(key))\"\n end\n calls_to_each_storage.join(\" || \")\n }\n end\n\n def read_multi(*keys)\n \#{\n initialize_results = storages.map.each_with_index do |s, i|\n \"results\#{i} = nil\"\n end\n initialize_results.join(\"\\n\")\n }\n keys.each_with_object({}) do |key, memo|\n result = \\\n \#{\n calls_to_each_storage = storages.map.each_with_index do |s, i|\n \"Mova.presence((results\#{i} ||= storages[\#{i}].read_multi(*keys))[key])\"\n end\n calls_to_each_storage.join(\" || \")\n }\n memo[key] = result if result\n end\n end\n EOM\nend\n", __FILE__, __LINE__ + 1 |
Instance Attribute Details
#storages ⇒ Object (readonly)
8 9 10 |
# File 'lib/mova/storage/chain.rb', line 8 def storages @storages end |
Instance Method Details
#clear ⇒ void
Each storage will receive #clear. Use Readonly if you wish to protect certain storages.
This method returns an undefined value.
108 109 110 |
# File 'lib/mova/storage/chain.rb', line 108 def clear storages.each { |s| s.clear } end |
#exist?(key) ⇒ Boolean
114 115 116 |
# File 'lib/mova/storage/chain.rb', line 114 def exist?(key) storages.any? { |s| s.exist?(key) } end |
#read(key) ⇒ String?
Returns first non-empty value while trying each storage in order defined in the initializer.
48 49 50 |
# File 'lib/mova/storage/chain.rb', line 48 def read(key) Mova.presence(storages[0].read(key)) || Mova.presence(storages[1].read(key)) || etc end |
#read_multi(*keys) ⇒ Hash{String => String}
Returns composed result of all non-empty values. Hashes are merged backwards, so results from a first storage win over results from next one. However, non-empty value wins over empty one.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/mova/storage/chain.rb', line 66 def read_multi(*keys) results0 = nil results1 = nil etc keys.each_with_object({}) do |key, memo| result = Mova.presence((results0 ||= storages[0].read_multi(*keys))[key]) || Mova.presence((results1 ||= storages[1].read_multi(*keys))[key]) || etc memo[key] = result if result end end |
#write(key, value) ⇒ void
Each storage will receive #write. Use Readonly if you wish to protect certain storages.
This method returns an undefined value.
100 101 102 |
# File 'lib/mova/storage/chain.rb', line 100 def write(key, value) storages.each { |s| s.write(key, value) } end |