Sha256: 15128afd3aa37c34d21cc1d400fce4e631ce73f1674eb7c7eb0dca9de83c0218

Contents?: true

Size: 1.22 KB

Versions: 8

Compression:

Stored size: 1.22 KB

Contents

module Flipper
  # Builds an adapter from a stack of adapters.
  #
  #   adapter = Flipper::AdapterBuilder.new do
  #     use Flipper::Adapters::Strict
  #     use Flipper::Adapters::Memoizer
  #     store Flipper::Adapters::Memory
  #   end.to_adapter
  #
  class AdapterBuilder
    def initialize(&block)
      @stack = []

      # Default to memory adapter
      store Flipper::Adapters::Memory

      block.arity == 0 ? instance_eval(&block) : block.call(self) if block
    end

    if RUBY_VERSION >= '3.0'
      def use(klass, *args, **kwargs, &block)
        @stack.push ->(adapter) { klass.new(adapter, *args, **kwargs, &block) }
      end
    else
      def use(klass, *args, &block)
        @stack.push ->(adapter) { klass.new(adapter, *args, &block) }
      end
    end

    if RUBY_VERSION >= '3.0'
      def store(adapter, *args, **kwargs, &block)
        @store = adapter.respond_to?(:call) ? adapter : -> { adapter.new(*args, **kwargs, &block) }
      end
    else
      def store(adapter, *args, &block)
        @store = adapter.respond_to?(:call) ? adapter : -> { adapter.new(*args, &block) }
      end
    end

    def to_adapter
      @stack.reverse.inject(@store.call) { |adapter, wrapper| wrapper.call(adapter) }
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
flipper-1.3.0 lib/flipper/adapter_builder.rb
flipper-1.3.0.pre lib/flipper/adapter_builder.rb
flipper-1.2.2 lib/flipper/adapter_builder.rb
flipper-1.2.1 lib/flipper/adapter_builder.rb
flipper-1.2.0 lib/flipper/adapter_builder.rb
flipper-1.1.2 lib/flipper/adapter_builder.rb
flipper-1.1.1 lib/flipper/adapter_builder.rb
flipper-1.1.0 lib/flipper/adapter_builder.rb