Sha256: 869eff7a753af1e0ea425792831cd917326ba394a1b72e7d6cafee338b15fea9

Contents?: true

Size: 1.8 KB

Versions: 10

Compression:

Stored size: 1.8 KB

Contents

require "flipper/expression"

module Flipper
  module Gates
    class Expression < Gate
      # Internal: The name of the gate. Used for instrumentation, etc.
      def name
        :expression
      end

      # Internal: Name converted to value safe for adapter.
      def key
        :expression
      end

      def data_type
        :json
      end

      def enabled?(value)
        !value.nil? && !value.empty?
      end

      # Internal: Checks if the gate is open for a thing.
      #
      # Returns true if gate open for thing, false if not.
      def open?(context)
        data = context.values.expression
        return false if data.nil? || data.empty?
        expression = Flipper::Expression.build(data)

        if context.actors.nil? || context.actors.empty?
          !!expression.evaluate(feature_name: context.feature_name, properties: DEFAULT_PROPERTIES)
        else
          context.actors.any? do |actor|
            !!expression.evaluate(feature_name: context.feature_name, properties: properties(actor))
          end
        end
      end

      def protects?(thing)
        thing.is_a?(Flipper::Expression) || thing.is_a?(Hash)
      end

      def wrap(thing)
        Flipper::Expression.build(thing)
      end

      private

      # Internal
      DEFAULT_PROPERTIES = {}.freeze

      def properties(actor)
        return DEFAULT_PROPERTIES if actor.nil?

        properties = {}

        if actor.respond_to?(:flipper_properties)
          properties.update(actor.flipper_properties)
        else
          warn "#{actor.inspect} does not respond to `flipper_properties` but should."
        end

        properties.transform_keys!(&:to_s)

        if actor.respond_to?(:flipper_id)
          properties["flipper_id".freeze] = actor.flipper_id
        end

        properties
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
flipper-1.3.2 lib/flipper/gates/expression.rb
flipper-1.3.1 lib/flipper/gates/expression.rb
flipper-1.3.0 lib/flipper/gates/expression.rb
flipper-1.3.0.pre lib/flipper/gates/expression.rb
flipper-1.2.2 lib/flipper/gates/expression.rb
flipper-1.2.1 lib/flipper/gates/expression.rb
flipper-1.2.0 lib/flipper/gates/expression.rb
flipper-1.1.2 lib/flipper/gates/expression.rb
flipper-1.1.1 lib/flipper/gates/expression.rb
flipper-1.1.0 lib/flipper/gates/expression.rb