<commit_before>require 'set'
require 'active_support/concern'
require 'active_support/configurable'
require 'active_support/callbacks'
require 'active_support/core_ext/module/aliasing'
require 'active_support/core_ext/array/extract_options'

module Circuit
  private

  module ConfigurationCallbacks
    extend ActiveSupport::Concern

    included do
      # TODO validate self.config inherits from AS::Configurable::Configuration and is an anonymous class
      self.config.send(:include, Configuration)
    end

    private

    module Configuration
      extend ActiveSupport::Concern
      include ActiveSupport::Callbacks

      included do
        alias_method :_set, :[]= # preserve the original #[] method
        protected :_set # make it protected
        alias_method_chain :define_callbacks, :repeat_prevention
      end

      def before(get_set, key, *args, &block)
        name = _setup_for_callback(get_set, key)
        self.class.set_callback(name, :before, *args, &block)
      end

      def after(get_set, key, *args, &block)
        name = _setup_for_callback(get_set, key)
        self.class.set_callback(name, :after, *args, &block)
      end

      def around(get_set, key, *args, &block)
        name = _setup_for_callback(get_set, key)
        self.class.set_callback(name, :around, *args, &block)
      end

      private

      def _setup_for_callback(get_set, key)
        key = key.to_sym
        name = "#{get_set}_#{key}"

        case get_set.to_sym
        when :get
          if method_defined?(key)
            warn "Cannot setup callbacks for #{self}##{key}.  Method already defined."
          else
            class_eval <<-EOS, __FILE__, __LINE__ + 1
              def #{key}() run_callbacks(#{name.inspect}) { _get(#{key.inspect}) }; end
            EOS
          end
        when :set
          if method_defined?(:"#{key}=")
            warn "Cannot setup callbacks for #{self}##{key}.  Method already defined."
          else
            class_eval <<-EOS, __FILE__, __LINE__ + 1
              def #{key}=(val) run_callbacks(#{name.inspect}) { _set(#{key.inspect}, val) }; end
            EOS
          end
        else
          raise ArgumentError, "Invalid get_set parameter %p"%[get_set]
        end

        self.class.define_callbacks(name)
        return name
      end

      module ClassMethods
        private

        def define_callbacks_with_repeat_prevention(*args)
          options = args.extract_options!
          @@_callbacks_defined ||= Set.new

          args = args.collect do |name|
            name = name.to_sym
            if @@_callbacks_defined.include?(name)
              nil
            else
              @@_callbacks_defined << name
              name
            end
          end.tap(&:compact!)

          return if args.empty?

          define_callbacks_without_repeat_prevention(*args, options)
        end
      end<commit_msg>fix Ruby 1.8.7 syntax error<commit_after>require 'set'
require 'active_support/concern'
require 'active_support/configurable'
require 'active_support/callbacks'
require 'active_support/core_ext/module/aliasing'
require 'active_support/core_ext/array/extract_options'

module Circuit
  private

  module ConfigurationCallbacks
    extend ActiveSupport::Concern

    included do
      # TODO validate self.config inherits from AS::Configurable::Configuration and is an anonymous class
      self.config.send(:include, Configuration)
    end

    private

    module Configuration
      extend ActiveSupport::Concern
      include ActiveSupport::Callbacks

      included do
        alias_method :_set, :[]= # preserve the original #[] method
        protected :_set # make it protected
        alias_method_chain :define_callbacks, :repeat_prevention
      end

      def before(get_set, key, *args, &block)
        name = _setup_for_callback(get_set, key)
        self.class.set_callback(name, :before, *args, &block)
      end

      def after(get_set, key, *args, &block)
        name = _setup_for_callback(get_set, key)
        self.class.set_callback(name, :after, *args, &block)
      end

      def around(get_set, key, *args, &block)
        name = _setup_for_callback(get_set, key)
        self.class.set_callback(name, :around, *args, &block)
      end

      private

      def _setup_for_callback(get_set, key)
        key = key.to_sym
        name = "#{get_set}_#{key}"

        case get_set.to_sym
        when :get
          if method_defined?(key)
            warn "Cannot setup callbacks for #{self}##{key}.  Method already defined."
          else
            class_eval <<-EOS, __FILE__, __LINE__ + 1
              def #{key}() run_callbacks(#{name.inspect}) { _get(#{key.inspect}) }; end
            EOS
          end
        when :set
          if method_defined?(:"#{key}=")
            warn "Cannot setup callbacks for #{self}##{key}.  Method already defined."
          else
            class_eval <<-EOS, __FILE__, __LINE__ + 1
              def #{key}=(val) run_callbacks(#{name.inspect}) { _set(#{key.inspect}, val) }; end
            EOS
          end
        else
          raise ArgumentError, "Invalid get_set parameter %p"%[get_set]
        end

        self.class.define_callbacks(name)
        return name
      end

      module ClassMethods
        private

        def define_callbacks_with_repeat_prevention(*args)
          options = args.extract_options!
          @@_callbacks_defined ||= Set.new

          args = args.collect do |name|
            name = name.to_sym
            if @@_callbacks_defined.include?(name)
              nil
            else
              @@_callbacks_defined << name
              name
            end
          end.tap(&:compact!)

          return if args.empty?

          args << options
          define_callbacks_without_repeat_prevention(*args)
        end
      end
{'fraction_non_alphanumeric': 0.09513236549957302, 'fraction_numerical': 0.0011955593509820667, 'mean_word_length': 2.2300055157198013, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 3, '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': '5613950', 'n_tokens_mistral': 1715, 'n_tokens_neox': 1669, 'n_words': 416}