class ActiveSupport::Callbacks::Filters::Before - activesupport-4.2.4 Documentation

# class ActiveSupport::Callbacks::Filters::Before

### Public Class Methods

build(callback_sequence, user_callback, user_conditions, chain_config, filter) click to toggle source

    # File lib/active_support/callbacks.rb, line 124
    def self.build(callback_sequence, user_callback, user_conditions, chain_config, filter)
      halted_lambda = chain_config[:terminator]

      if chain_config.key?(:terminator) && user_conditions.any?
        halting_and_conditional(callback_sequence, user_callback, user_conditions, halted_lambda, filter)
      elsif chain_config.key? :terminator
        halting(callback_sequence, user_callback, halted_lambda, filter)
      elsif user_conditions.any?
        conditional(callback_sequence, user_callback, user_conditions)
      else
        simple callback_sequence, user_callback
      end
    end

### Private Class Methods

conditional(callback_sequence, user_callback, user_conditions) click to toggle source

    # File lib/active_support/callbacks.rb, line 176
    def self.conditional(callback_sequence, user_callback, user_conditions)
      callback_sequence.before do |env|
        target = env.target
        value  = env.value

        if user_conditions.all? { |c| c.call(target, value) }
          user_callback.call target, value
        end

        env
      end
    end

halting(callback_sequence, user_callback, halted_lambda, filter) click to toggle source

    # File lib/active_support/callbacks.rb, line 157
    def self.halting(callback_sequence, user_callback, halted_lambda, filter)
      callback_sequence.before do |env|
        target = env.target
        value  = env.value
        halted = env.halted

        unless halted
          result = user_callback.call target, value
          env.halted = halted_lambda.call(target, result)
          if env.halted
            target.send :halted_callback_hook, filter
          end
        end

        env
      end
    end

halting_and_conditional(callback_sequence, user_callback, user_conditions, halted_lambda, filter) click to toggle source

    # File lib/active_support/callbacks.rb, line 138
    def self.halting_and_conditional(callback_sequence, user_callback, user_conditions, halted_lambda, filter)
      callback_sequence.before do |env|
        target = env.target
        value  = env.value
        halted = env.halted

        if !halted && user_conditions.all? { |c| c.call(target, value) }
          result = user_callback.call target, value
          env.halted = halted_lambda.call(target, result)
          if env.halted
            target.send :halted_callback_hook, filter
          end
        end

        env
      end
    end

simple(callback_sequence, user_callback) click to toggle source

    # File lib/active_support/callbacks.rb, line 190
    def self.simple(callback_sequence, user_callback)
      callback_sequence.before do |env|
        user_callback.call env.target, env.value

        env
      end
    end