~ubuntu-branches/ubuntu/trusty/ruby-moneta/trusty

« back to all changes in this revision

Viewing changes to lib/moneta/optionmerger.rb

  • Committer: Package Import Robot
  • Author(s): Jérémy Bobbio
  • Date: 2013-05-14 09:40:52 UTC
  • mfrom: (1.2.1) (3.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20130514094052-p8i17yyswpx6c1zc
Tags: 0.7.16-1
* Team upload.
* New upstream version.
* Remove transitional packages.
* Add Vcs-* fields.
* Fix a typo in debian/copyright.
* Add a Breaks for chef given the API has changed between 0.6 and 0.7.
* Refresh patches:
  - remove 0002-Mark-concurrent-increment-example-as-pending.patch:
    fixed upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
module Moneta
 
2
  # @api private
 
3
  class OptionMerger < Wrapper
 
4
    METHODS = [:key?, :load, :store, :create, :delete, :increment, :clear].freeze
 
5
 
 
6
    attr_reader :default_options
 
7
 
 
8
    # @param [Moneta store] adapter underlying adapter
 
9
    # @param [Hash] options
 
10
    def initialize(adapter, options = {})
 
11
      super(adapter, options)
 
12
 
 
13
      @default_options = adapter.respond_to?(:default_options) ? adapter.default_options.dup : {}
 
14
 
 
15
      if options.include?(:only)
 
16
        raise ArgumentError, 'Either :only or :except is allowed' if options.include?(:except)
 
17
        methods = [options.delete(:only)].compact.flatten
 
18
      elsif options.include?(:except)
 
19
        methods = METHODS - [options.delete(:except)].compact.flatten
 
20
      else
 
21
        methods = METHODS
 
22
      end
 
23
 
 
24
      methods.each do |method|
 
25
        if oldopts = @default_options[method]
 
26
          newopts = (@default_options[method] = oldopts.merge(options))
 
27
          newopts[:prefix] = "#{oldopts[:prefix]}#{options[:prefix]}" if oldopts[:prefix] || options[:prefix]
 
28
        else
 
29
          @default_options[method] = options
 
30
        end
 
31
      end
 
32
    end
 
33
 
 
34
    protected
 
35
 
 
36
    def wrap(method, *args)
 
37
      options = args.last
 
38
      options.merge!(@default_options[method]) if Hash === options && @default_options.include?(method)
 
39
      yield
 
40
    end
 
41
  end
 
42
end
 
43