Sha256: 9b485205061aa6d7dd89b3a4ccf475dee56b37893fa3203d2fbf733a91eede79

Contents?: true

Size: 1.61 KB

Versions: 3

Compression:

Stored size: 1.61 KB

Contents

module FlipFab
  class ContextualFeature
    attr_reader :feature, :context

    def initialize feature, context
      @feature = feature
      @context = context
      if overridden?
        @state = override
        persist
      end
    end

    def enabled?
      state == :enabled
    end

    def disabled?
      !enabled?
    end

    def enable
      self.state = :enabled
    end

    def disable
      self.state = :disabled
    end

    def state= value
      raise "Invalid state provided: `#{value}`, possible states are :enabled, :disabled" unless %i(enabled disabled).include? value
      unless overridden?
        @state = value
        persist
      end
    end

    def persist
      persistence_adapters.each{ |adapter| adapter.write state }
    end

    private

    def persistence_adapters
      @persistence_adapters ||= feature.persistence_adapters.map { |adapter_class| adapter_class.new feature.name, context }
    end

    def state
      @state ||= if state_in_context?
        state_from_context
      else
        default_state
      end
    end

    def state_in_context?
      !first_adapter_with_state.nil?
    end

    def state_from_context
      first_adapter_with_state.read
    end

    def first_adapter_with_state
      persistence_adapters.detect{|adapter| !adapter.read.nil?}
    end

    def default_state
      feature.default
    end

    def override
      return unless context.respond_to? :params
      override = context.params[feature.name.to_s]
      return unless %w(enabled disabled).include? override
      override.to_sym
    end

    def overridden?
      !override.nil?
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
flip_fab-1.0.1 lib/flip_fab/contextual_feature.rb
flip_fab-1.0.0 lib/flip_fab/contextual_feature.rb
flip_fab-0.0.1 lib/flip_fab/contextual_feature.rb