<commit_before>module OpenFoodNetwork
  # This feature toggles implementation provides two mechanisms to conditionally enable features.
  #
  # You can provide an ENV var with the prefix `OFN_FEATURE_` and query it using the
  # `ApplicationHelper#feature?` helper method. For instance, providing the ENV var
  # `OFN_FEATURE_NEW_SHINNY_FEATURE` you could then query it from view as follows:
  #
  #   - if feature? :new_shiny_feature
  #     = render "new_shiny_feature"
  #
  # Alternatively, you can choose which users have the feature toggled on. To do that you need to
  # register the feature and its users from an initializer like:
  #
  #   require 'open_food_network/feature_toggle'
  #   OpenFoodNetwork::FeatureToggle.enable(:new_shiny_feature, ['<EMAIL>'])
  #
  # Note, however, that it'd be better to read the user emails from an ENV var provisioned with
  # ofn-install:
  #
  #   require 'open_food_network/feature_toggle'
  #   OpenFoodNetwork::FeatureToggle.enable(:new_shiny_feature, ENV['PRODUCT_TEAM'])
  #
  # You can then check it from a view like:
  #
  #   - if feature? :new_shiny_feature, spree_current_user
  #     = render "new_shiny_feature"
  #
  class FeatureToggle
    def self.enabled?(feature_name, user = nil)
      new.enabled?(feature_name, user)
    end

    def self.enable(feature_name, &block)
      Thread.current[:features] ||= {}
      Thread.current[:features][feature_name] = Feature.new(block)
    end

    def initialize
      @features = Thread.current[:features] || {}
    end

    def enabled?(feature_name, user)
      if Flipper[feature_name].exist?
        Flipper.enabled?(feature_name, user)
      else
        feature = features.fetch(feature_name, DefaultFeature.new(feature_name))
        feature.enabled?(user)
      end
    end

    private

    attr_reader :features
  end

  class Feature
    def initialize(block)
      @block = block
    end

    def enabled?(user)
      block.call(user)
    end

    private

    attr_reader :block
  end

  class DefaultFeature
    attr_reader :feature_name

    def initialize(feature_name)
      @feature_name = feature_name
    end

    def enabled?(_user)
      true?(env_variable_value(feature_name))
    end

    private

    def env_variable_value(feature_name)
      ENV.fetch(env_variable_name(feature_name), nil)
    end

    def env_variable_name(feature_name)
      "OFN_FEATURE_#{feature_name.to_s.upcase}"
    end

    def true?(value)
      value.to_s.casecmp("true").zero?
    end
  end
end
<commit_msg>Simplify FeatureToggle module<commit_after>module OpenFoodNetwork
  # This feature toggles implementation provides two mechanisms to conditionally enable features.
  #
  # You can provide an ENV var with the prefix `OFN_FEATURE_` and query it using the
  # `ApplicationHelper#feature?` helper method. For instance, providing the ENV var
  # `OFN_FEATURE_NEW_SHINNY_FEATURE` you could then query it from view as follows:
  #
  #   - if feature? :new_shiny_feature
  #     = render "new_shiny_feature"
  #
  # Alternatively, you can choose which users have the feature toggled on. To do that you need to
  # register the feature and its users from an initializer like:
  #
  #   require 'open_food_network/feature_toggle'
  #   OpenFoodNetwork::FeatureToggle.enable(:new_shiny_feature, ['<EMAIL>'])
  #
  # Note, however, that it'd be better to read the user emails from an ENV var provisioned with
  # ofn-install:
  #
  #   require 'open_food_network/feature_toggle'
  #   OpenFoodNetwork::FeatureToggle.enable(:new_shiny_feature, ENV['PRODUCT_TEAM'])
  #
  # You can then check it from a view like:
  #
  #   - if feature? :new_shiny_feature, spree_current_user
  #     = render "new_shiny_feature"
  #
  module FeatureToggle
    def self.enabled?(feature_name, user = nil)
      features = Thread.current[:features] || {}

      if Flipper[feature_name].exist?
        Flipper.enabled?(feature_name, user)
      else
        feature = features.fetch(feature_name, DefaultFeature.new(feature_name))
        feature.enabled?(user)
      end
    end

    def self.enable(feature_name, &block)
      Thread.current[:features] ||= {}
      Thread.current[:features][feature_name] = Feature.new(block)
    end
  end

  class Feature
    def initialize(block)
      @block = block
    end

    def enabled?(user)
      block.call(user)
    end

    private

    attr_reader :block
  end

  class DefaultFeature
    attr_reader :feature_name

    def initialize(feature_name)
      @feature_name = feature_name
    end

    def enabled?(_user)
      true?(env_variable_value(feature_name))
    end

    private

    def env_variable_value(feature_name)
      ENV.fetch(env_variable_name(feature_name), nil)
    end

    def env_variable_name(feature_name)
      "OFN_FEATURE_#{feature_name.to_s.upcase}"
    end

    def true?(value)
      value.to_s.casecmp("true").zero?
    end
  end
end

{'fraction_non_alphanumeric': 0.0858132898491643, 'fraction_numerical': 0.0, 'mean_word_length': 3.3006134969325154, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7472931', 'n_tokens_mistral': 1543, 'n_tokens_neox': 1531, 'n_words': 396}