File: builder.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 (82 lines) | stat: -rw-r--r-- 2,535 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
module Moneta
  # Builder implements the DSL to build a stack of Moneta store proxies
  # @api private
  class Builder
    # @yieldparam Builder dsl code block
    def initialize(&block)
      raise ArgumentError, 'No block given' unless block_given?
      @proxies = []
      instance_eval(&block)
    end

    # Build proxy stack
    #
    # @return [Object] Generated Moneta proxy stack
    # @api public
    def build
      adapter = @proxies.first
      if Array === adapter
        klass, options, block = adapter
        adapter = new_proxy(klass, options.dup, &block)
        check_arity(klass, adapter, 1)
      end
      @proxies[1..-1].inject([adapter]) do |result, proxy|
        klass, options, block = proxy
        proxy = new_proxy(klass, result.last, options.dup, &block)
        check_arity(klass, proxy, 2)
        result << proxy
      end
    end

    # Add proxy to stack
    #
    # @param [Symbol/Class] proxy Name of proxy class or proxy class
    # @param [Hash] options Options hash
    # @api public
    def use(proxy, options = {}, &block)
      proxy = Moneta.const_get(proxy) if Symbol === proxy
      raise ArgumentError, 'You must give a Class or a Symbol' unless Class === proxy
      @proxies.unshift [proxy, options, block]
      nil
    end

    # Add adapter to stack
    #
    # @param [Symbol/Class/Moneta store] adapter Name of adapter class, adapter class or Moneta store
    # @param [Hash] options Options hash
    # @api public
    def adapter(adapter, options = {}, &block)
      case adapter
      when Symbol
        use(Adapters.const_get(adapter), options, &block)
      when Class
        use(adapter, options, &block)
      else
        raise ArgumentError, 'Adapter must be a Moneta store' unless adapter.respond_to?(:load) && adapter.respond_to?(:store)
        raise ArgumentError, 'No options allowed' unless options.empty?
        @proxies.unshift adapter
        nil
      end
    end

    protected

    def new_proxy(klass, *args, &block)
      klass.new(*args, &block)
    rescue ArgumentError
      check_arity(klass, klass.allocate, args.size)
      raise
    end

    def check_arity(klass, proxy, expected)
      args = proxy.method(:initialize).arity.abs
      raise(ArgumentError, %{#{klass.name}#new accepts wrong number of arguments (#{args} accepted, #{expected} expected)

Please check your Moneta builder block:
  * Proxies must be used before the adapter
  * Only one adapter is allowed
  * The adapter must be used last
}) if args != expected
    end
  end
end