<commit_before>require 'set'
require 'flipper'
require 'active_record'

require 'flipper/activerecord/feature'
require 'flipper/activerecord/gate'

module Flipper
  module Adapters
    class ActiveRecord
      include Flipper::Adapter

      # Public: The name of the adapter.
      attr_reader :name

      def initialize
        @name = :activerecord
      end

      # Public: The set of known features.
      def features
        Flipper::ActiveRecord::Feature.select(:name).map(&:name).to_set
      end

      # Public: Adds a feature to the set of known features.
      def add(feature)
        Flipper::ActiveRecord::Feature.find_or_create_by!(name: feature.name.to_s)
        true
      end

      # Public: Removes a feature from the set of known features.
      def remove(feature)
        clear(feature)
      end

      # Public: Clears all the gate values for a feature.
      def clear(feature)
        Flipper::ActiveRecord::Feature.where(name: feature.key).destroy_all
        true
      end

      # Public: Gets the values for all gates for a given feature.
      #
      # Returns a Hash of Flipper::Gate#key => value.
      def get(feature)
        result = {}
        f = Flipper::ActiveRecord::Feature.eager_load(:gates).find_by(name: feature.key)

        feature.gates.each do |gate|
          result[gate.key] = case gate.data_type
          when :boolean, :integer
            if f
              g = f.gates.detect {|g| g.name == gate.key.to_s}
              g.value if g
            end
          when :set
            if f
              f.gates.select {|g| g.name == gate.key.to_s}.map{|g| g.value }.to_set
            else
              Set.new
            end
          else
            unsupported_data_type gate.data_type
          end
        end

        result
      end

      # Public: Enables a gate for a given thing.
      #
      # feature - The Flipper::Feature for the gate.
      # gate - The Flipper::Gate to disable.
      # thing - The Flipper::Type being disabled for the gate.
      #
      # Returns true.<commit_msg>Update activerecord.rb<commit_after>require 'set'
require 'flipper'
require 'active_record'

require 'flipper/activerecord/feature'
require 'flipper/activerecord/gate'

module Flipper
  module Adapters
    class ActiveRecord
      include Flipper::Adapter

      # Public: The name of the adapter.
      attr_reader :name

      def initialize
        @name = :activerecord
      end

      # Public: The set of known features.
      def features
        Flipper::ActiveRecord::Feature.select(:name).map(&:name).to_set
      end

      # Public: Adds a feature to the set of known features.
      def add(feature)
        Flipper::ActiveRecord::Feature.find_or_create_by!(name: feature.name.to_s)
        true
      end

      # Public: Removes a feature from the set of known features.
      def remove(feature)
        clear(feature)
      end

      # Public: Clears all the gate values for a feature.
      def clear(feature)
        Flipper::ActiveRecord::Feature.where(name: feature.key).destroy_all
        true
      end

      # Public: Gets the values for all gates for a given feature.
      #
      # Returns a Hash of Flipper::Gate#key => value.
      def get(feature)
        result = {}
        f = Flipper::ActiveRecord::Feature.eager_load(:gates).where(name: feature.key)

        feature.gates.each do |gate|
          result[gate.key] = case gate.data_type
          when :boolean, :integer
            if f
              g = f.gates.detect {|g| g.name == gate.key.to_s}
              g.value if g
            end
          when :set
            if f
              f.gates.select {|g| g.name == gate.key.to_s}.map{|g| g.value }.to_set
            else
              Set.new
            end
          else
            unsupported_data_type gate.data_type
          end
        end

        result
      end

      # Public: Enables a gate for a given thing.
      #
      # feature - The Flipper::Feature for the gate.
      # gate - The Flipper::Gate to disable.
      # thing - The Flipper::Type being disabled for the gate.
      #
      # Returns true.
{'fraction_non_alphanumeric': 0.08669403525718425, 'fraction_numerical': 0.0, 'mean_word_length': 1.8664359861591695, 'pattern_counts': {'":': 0, '<': 3, '<?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': '231801', 'n_tokens_mistral': 1212, 'n_tokens_neox': 1199, 'n_words': 356}