Sha256: 781400d304d227890b16b45e9a642a5aa422dff6fd7a7ff52b7fcf486e34bdba

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 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

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

      def initialize(adapter, handler = nil, &block)
        @name = :strict
        @adapter = adapter
        @handler = block || 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)
        return if @adapter.features.include?(feature.key)

        case handler
        when Proc then handler.call(feature)
        when :warn then warn NotFound.new(feature.key).message
        when :noop, false, nil
         # noop
        else # truthy or :raise
         raise NotFound.new(feature.key)
        end
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
flipper-1.2.2 lib/flipper/adapters/strict.rb
flipper-1.2.1 lib/flipper/adapters/strict.rb
flipper-1.2.0 lib/flipper/adapters/strict.rb