Class: Moneta::Builder Private

Inherits:
Object
  • Object
show all
Defined in:
lib/moneta/builder.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Builder implements the DSL to build a stack of Moneta store proxies

Instance Method Summary collapse

Constructor Details

#initialize {|Builder| ... } ⇒ Builder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Builder.

Yield Parameters:

  • Builder

    dsl code block

Raises:

  • (ArgumentError)


6
7
8
9
10
# File 'lib/moneta/builder.rb', line 6

def initialize(&block)
  raise ArgumentError, 'No block given' unless block_given?
  @proxies = []
  instance_eval(&block)
end

Instance Method Details

#adapter(adapter, options = {}, &block) ⇒ Object

Add adapter to stack

Parameters:

  • adapter (Symbol/Class/Moneta store)

    Name of adapter class, adapter class or Moneta store

  • options (Hash) (defaults to: {})

    Options hash



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/moneta/builder.rb', line 48

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

#buildObject

Build proxy stack

Returns:

  • (Object)

    Generated Moneta proxy stack



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/moneta/builder.rb', line 16

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

#use(proxy, options = {}, &block) ⇒ Object

Add proxy to stack

Parameters:

  • proxy (Symbol/Class)

    Name of proxy class or proxy class

  • options (Hash) (defaults to: {})

    Options hash

Raises:

  • (ArgumentError)


36
37
38
39
40
41
# File 'lib/moneta/builder.rb', line 36

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