File: stack.rb

package info (click to toggle)
ruby-moneta 1.6.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,776 kB
  • sloc: ruby: 13,201; sh: 178; makefile: 7
file content (106 lines) | stat: -rw-r--r-- 2,442 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
module Moneta
  # Combines multiple stores. Reads return the result from the first store,
  # writes go to all stores.
  #
  # @example Add `Moneta::Stack` to proxy stack
  #   Moneta.build do
  #     use(:Stack) do
  #       add { adapter :Redis }
  #       add { adapter :File, dir: 'data' }
  #       add { adapter :File, dir: 'replicate' }
  #     end
  #   end
  #
  # @api public
  class Stack
    include Defaults

    # @api private
    class DSL
      def initialize(stack, &block)
        @stack = stack
        instance_eval(&block)
      end

      # @api public
      def add(store = nil, &block)
        raise ArgumentError, 'Only argument or block allowed' if store && block
        @stack << (store || Moneta.build(&block))
        nil
      end
    end

    attr_reader :stack

    # @param [Hash] options Options hash
    # @option options [Array] :stack Array of Moneta stores
    # @yieldparam Builder block
    def initialize(options = {}, &block)
      @stack = options[:stack].to_a
      DSL.new(@stack, &block) if block_given?
    end

    # (see Proxy#key?)
    def key?(key, options = {})
      @stack.any? { |s| s.key?(key, options) }
    end

    # (see Proxy#load)
    def load(key, options = {})
      @stack.each do |s|
        value = s.load(key, options)
        return value if value != nil
      end
      nil
    end

    # (see Proxy#store)
    def store(key, value, options = {})
      @stack.each { |s| s.store(key, value, options) }
      value
    end

    # (see Proxy#increment)
    def increment(key, amount = 1, options = {})
      last = nil
      @stack.each { |s| last = s.increment(key, amount, options) }
      last
    end

    # (see Proxy#create)
    def create(key, value, options = {})
      last = false
      @stack.each { |s| last = s.create(key, value, options) }
      last
    end

    # (see Proxy#delete)
    def delete(key, options = {})
      @stack.inject(nil) do |value, s|
        v = s.delete(key, options)
        value || v
      end
    end

    # (see Proxy#clear)
    def clear(options = {})
      @stack.each { |s| s.clear(options) }
      self
    end

    # (see Proxy#close)
    def close
      @stack.each { |s| s.close }
      nil
    end

    # (see Proxy#features)
    def features
      @features ||=
        begin
          features = @stack.map(&:features)
          (features.inject(features.first, &:&) - [:each_key]).freeze
        end
    end
  end
end