<commit_before>module TieredValidation
  class ValidationTier
    DEFAULT_ACTION_CALLBACK_MAP = {
      :save => :validate,
      :update => :validate_on_update,
      :create => :validate_on_create
    }.freeze
    
    DEFAULT_VALIDATION_CALLBACKS = DEFAULT_ACTION_CALLBACK_MAP.values.freeze
    
    def initialize(name, klass, includes = [], exclusive = true)
      @name = name
      @klass = klass
      @included_tiers = includes.is_a?(Array) ? includes : [includes].compact
      @exclusive = exclusive
      
      DEFAULT_ACTION_CALLBACK_MAP.each do |action, default_callback|
        @klass.define_callbacks callback_name(default_callback)
      end
    end

    def callback_chain(action)
      callback_chain = base_callback_chain(action).clone

      @included_tiers.each do |tier|
        callback_chain += @klass::VALIDATION_TIERS[tier].callback_chain(action) 
      end
      
      callback_chain.uniq
    end

    def run_callbacks(action, instance)
      callback_chain(action).run(instance)
    end

    def setup_alias_methods
      DEFAULT_VALIDATION_CALLBACKS.each do |default_callback|
        if @exclusive
          @klass.class_eval <<-BLOCK, __FILE__, __LINE__ + 1                                
            def self.#{callback_with_tier(default_callback)}(*methods, &block)              # def self.validate_with_tier(*methods, &block)         
              #{callback_name(default_callback)} *methods, &block                           #   validate_for_admin *methods, &block
            end                                                                             # end
          BLOCK
        else
          @klass.class_eval <<-BLOCK, __FILE__, __LINE__ + 1
            def self.#{callback_with_tier(default_callback)}(*methods, &block)              # def self.validate_with_tier(*methods, &block)    
              #{callback_name(default_callback)} *methods, &block                           #   validate_for_admin *methods, &block
              #{callback_without_tier(default_callback)} *methods, &block                   #   validate_without_tier *methods, &block<commit_msg>Define callbacks more concisely<commit_after>module TieredValidation
  class ValidationTier
    DEFAULT_ACTION_CALLBACK_MAP = {
      :save => :validate,
      :update => :validate_on_update,
      :create => :validate_on_create
    }.freeze
    
    DEFAULT_VALIDATION_CALLBACKS = DEFAULT_ACTION_CALLBACK_MAP.values.freeze
    
    def initialize(name, klass, includes = [], exclusive = true)
      @name = name
      @klass = klass
      @included_tiers = includes.is_a?(Array) ? includes : [includes].compact
      @exclusive = exclusive
      @klass.define_callbacks *DEFAULT_VALIDATION_CALLBACKS.map{|default_callback| callback_name(default_callback)}
    end

    def callback_chain(action)
      callback_chain = base_callback_chain(action).clone

      @included_tiers.each do |tier|
        callback_chain += @klass::VALIDATION_TIERS[tier].callback_chain(action) 
      end
      
      callback_chain.uniq
    end

    def run_callbacks(action, instance)
      callback_chain(action).run(instance)
    end

    def setup_alias_methods
      DEFAULT_VALIDATION_CALLBACKS.each do |default_callback|
        if @exclusive
          @klass.class_eval <<-BLOCK, __FILE__, __LINE__ + 1                                
            def self.#{callback_with_tier(default_callback)}(*methods, &block)              # def self.validate_with_tier(*methods, &block)         
              #{callback_name(default_callback)} *methods, &block                           #   validate_for_admin *methods, &block
            end                                                                             # end
          BLOCK
        else
          @klass.class_eval <<-BLOCK, __FILE__, __LINE__ + 1
            def self.#{callback_with_tier(default_callback)}(*methods, &block)              # def self.validate_with_tier(*methods, &block)    
              #{callback_name(default_callback)} *methods, &block                           #   validate_for_admin *methods, &block
              #{callback_without_tier(default_callback)} *methods, &block                   #   validate_without_tier *methods, &block
{'fraction_non_alphanumeric': 0.0858226647700332, 'fraction_numerical': 0.000948316737790422, 'mean_word_length': 2.1745673438675697, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3092831', 'n_tokens_mistral': 1153, 'n_tokens_neox': 1132, 'n_words': 210}