Class: Moneta::Builder

### Home Classes Methods

### In Files

- lib/moneta/builder.rb

### Parent

Object

### Methods

- ::new
- #adapter
- #build
- #check_arity
- #new_proxy
- #use

### Files

- LICENSE

### Class/Module Index

Quicksearch

- Moneta
- Moneta::Adapters
- Moneta::Adapters::ActiveRecord
- Moneta::Adapters::Cassandra
- Moneta::Adapters::Client
- Moneta::Adapters::Cookie
- Moneta::Adapters::Couch
- Moneta::Adapters::DBM
- Moneta::Adapters::DataMapper
- Moneta::Adapters::DataMapper::Store
- Moneta::Adapters::Daybreak
- Moneta::Adapters::File
- Moneta::Adapters::Fog
- Moneta::Adapters::GDBM
- Moneta::Adapters::HBase
- Moneta::Adapters::KyotoCabinet
- Moneta::Adapters::LMDB
- Moneta::Adapters::LRUHash
- Moneta::Adapters::LRUHash::Entry
- Moneta::Adapters::LevelDB
- Moneta::Adapters::LocalMemCache
- Moneta::Adapters::MemcachedDalli
- Moneta::Adapters::MemcachedNative
- Moneta::Adapters::Memory
- Moneta::Adapters::MongoBase
- Moneta::Adapters::MongoMoped
- Moneta::Adapters::MongoOfficial
- Moneta::Adapters::Null
- Moneta::Adapters::PStore
- Moneta::Adapters::Redis
- Moneta::Adapters::RestClient
- Moneta::Adapters::Riak
- Moneta::Adapters::SDBM
- Moneta::Adapters::Sequel
- Moneta::Adapters::Sqlite
- Moneta::Adapters::TDB
- Moneta::Adapters::TokyoCabinet
- Moneta::Adapters::TokyoTyrant
- Moneta::Adapters::YAML
- Moneta::Builder
- Moneta::Cache
- Moneta::Cache::DSL
- Moneta::CreateSupport
- Moneta::Defaults
- Moneta::Defaults::ClassMethods
- Moneta::Expires
- Moneta::ExpiresSupport
- Moneta::HashAdapter
- Moneta::IncrementSupport
- Moneta::Lock
- Moneta::Logger
- Moneta::Logger::Format
- Moneta::Mutex
- Moneta::OptionMerger
- Moneta::OptionSupport
- Moneta::Pool
- Moneta::Proxy
- Moneta::Semaphore
- Moneta::Server
- Moneta::Shared
- Moneta::Stack
- Moneta::Stack::DSL
- Moneta::SynchronizePrimitive
- Moneta::Transformer
- Moneta::Transformer::Helper
- Moneta::Utils
- Moneta::WeakCreate
- Moneta::WeakIncrement
- Moneta::Wrapper
- Rack
- Rack::Cache
- Rack::Cache::EntityStore
- Rack::Cache::EntityStore::MONETA
- Rack::Cache::EntityStore::Moneta
- Rack::Cache::MetaStore
- Rack::Cache::MetaStore::MONETA
- Rack::Cache::MetaStore::Moneta
- Rack::Cache::MonetaResolver
- Rack::MonetaCookies
- Rack::MonetaRest
- Rack::MonetaStore
- Rack::Session
- Rack::Session::Moneta
- ActionDispatch
- ActionDispatch::Session
- ActionDispatch::Session::MonetaStore
- ActiveSupport
- ActiveSupport::Cache
- ActiveSupport::Cache::MonetaStore

No matching classes.

# Moneta::Builder

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

### Public Class Methods

new(&block) click to toggle source

@yieldparam Builder dsl code block

    # File lib/moneta/builder.rb, line 6
    def initialize(&block)
      raise ArgumentError, 'No block given' unless block_given?
      @proxies = []
      instance_eval(&block)
    end

### Public Instance Methods

adapter(adapter, options = {}, &block) click to toggle source

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

    # 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

build() click to toggle source

Build proxy stack

@return [Object] Generated Moneta proxy stack @api public

    # File lib/moneta/builder.rb, line 16
    def build
      adapter = @proxies.first
      if Array === adapter
        klass, options, block = adapter
        adapter = new_proxy(klass, options, &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, &block)
        check_arity(klass, proxy, 2)
        result << proxy
      end
    end

use(proxy, options = {}, &block) click to toggle source

Add proxy to stack

@param [Symbol/Class] proxy Name of proxy class or proxy class @param [Hash] options Options hash @api public

    # 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

### Protected Instance Methods

check_arity(klass, proxy, expected) click to toggle source

    # File lib/moneta/builder.rb, line 71
        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

new_proxy(klass, *args, &block) click to toggle source

    # File lib/moneta/builder.rb, line 64
    def new_proxy(klass, *args, &block)
      klass.new(*args, &block)
    rescue ArgumentError => ex
      check_arity(klass, klass.allocate, args.size)
      raise
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.