Class: Mova::Storage::Chain

Inherits:
Object
  • Object
show all
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.

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*storages) ⇒ Chain

Returns a new instance of Chain.

Since:

  • 0.1.0



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

#storagesObject (readonly)

Since:

  • 0.1.0



8
9
10
# File 'lib/mova/storage/chain.rb', line 8

def storages
  @storages
end

Instance Method Details

#clearvoid

Note:

Each storage will receive #clear. Use Readonly if you wish to protect certain storages.

This method returns an undefined value.

Since:

  • 0.1.0



108
109
110
# File 'lib/mova/storage/chain.rb', line 108

def clear
  storages.each { |s| s.clear }
end

#exist?(key) ⇒ Boolean

Parameters:

  • key (String)

Returns:

  • (Boolean)

Since:

  • 0.1.0



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.

Examples:

storage1.write("hello", "ruby")
storage2.write("hello", "world")
storage2.write("bye", "war")
chain = Mova::Storage::Chain.new(storage1, storage2)
chain.read("hello") #=> "ruby"
chain.read("bye") #=> "war"

Parameters:

  • key (String)

Returns:

  • (String, nil)

    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.

Examples:

storage1.write("hello", "ruby")
storage1.write("empty", "")
storage2.write("hello", "world")
storage2.write("empty", "not so much")
storage2.write("bye", "war")
chain = Mova::Storage::Chain.new(storage1, storage2)
chain.read_multi("hello", "bye", "empty") #=> {"hello" => "ruby", "bye" => "war", "empty" => "not so much"}

Parameters:

  • keys (*Array<String>)

Returns:

  • (Hash{String => String})

    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

Note:

Each storage will receive #write. Use Readonly if you wish to protect certain storages.

This method returns an undefined value.

Parameters:

  • key (String)
  • value (String, nil)

Since:

  • 0.1.0



100
101
102
# File 'lib/mova/storage/chain.rb', line 100

def write(key, value)
  storages.each { |s| s.write(key, value) }
end