Class: Flipper::AdapterBuilder
- Inherits:
-
Object
- Object
- Flipper::AdapterBuilder
- Defined in:
- lib/flipper/adapter_builder.rb
Overview
Instance Method Summary collapse
-
#initialize(&block) ⇒ AdapterBuilder
constructor
A new instance of AdapterBuilder.
- #store(adapter, *args, &block) ⇒ Object
- #to_adapter ⇒ Object
- #use(klass, *args, &block) ⇒ Object
Constructor Details
#initialize(&block) ⇒ AdapterBuilder
Returns a new instance of AdapterBuilder.
11 12 13 14 15 16 17 18 |
# File 'lib/flipper/adapter_builder.rb', line 11 def initialize(&block) @stack = [] # Default to memory adapter store Flipper::Adapters::Memory block.arity == 0 ? instance_eval(&block) : block.call(self) if block end |
Instance Method Details
#store(adapter, *args, &block) ⇒ Object
31 32 33 |
# File 'lib/flipper/adapter_builder.rb', line 31 def store(adapter, *args, **kwargs, &block) @store = adapter.respond_to?(:call) ? adapter : -> { adapter.new(*args, **kwargs, &block) } end |
#to_adapter ⇒ Object
40 41 42 |
# File 'lib/flipper/adapter_builder.rb', line 40 def to_adapter @stack.reverse.inject(@store.call) { |adapter, wrapper| wrapper.call(adapter) } end |
#use(klass, *args, &block) ⇒ Object
21 22 23 |
# File 'lib/flipper/adapter_builder.rb', line 21 def use(klass, *args, **kwargs, &block) @stack.push ->(adapter) { klass.new(adapter, *args, **kwargs, &block) } end |