Sha256: ba4658a330a32ce6d4211fee769fbcfdefbd767bd3d10044d68b2a4c0699ddf4

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

module Flipper
  module Adapters
    # An adapter that ensures a feature exists before checking it.
    class Strict
      extend Forwardable
      include ::Flipper::Adapter
      attr_reader :name, :adapter, :handler

      class NotFound < ::Flipper::Error
        def initialize(name)
          super "Could not find feature #{name.inspect}. Call `Flipper.add(#{name.inspect})` to create it."
        end
      end

      HANDLERS = {
        raise: ->(feature) { raise NotFound.new(feature.key) },
        warn: ->(feature) { warn NotFound.new(feature.key).message },
        noop: ->(_) { },
      }

      def_delegators :@adapter, :features, :get_all, :add, :remove, :clear, :enable, :disable

      def initialize(adapter, handler = nil, &block)
        @name = :strict
        @adapter = adapter
        @handler = block || HANDLERS.fetch(handler)
      end

      def get(feature)
        assert_feature_exists(feature)
        @adapter.get(feature)
      end

      def get_multi(features)
        features.each { |feature| assert_feature_exists(feature) }
        @adapter.get_multi(features)
      end

      private

      def assert_feature_exists(feature)
        @handler.call(feature) unless @adapter.features.include?(feature.key)
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
flipper-1.1.2 lib/flipper/adapters/strict.rb
flipper-1.1.1 lib/flipper/adapters/strict.rb
flipper-1.1.0 lib/flipper/adapters/strict.rb