class ActiveSupport::Callbacks::Filters::Before

def self.build(callback_sequence, user_callback, user_conditions, chain_config, filter, name)

def self.build(callback_sequence, user_callback, user_conditions, chain_config, filter, name)
  halted_lambda = chain_config[:terminator]
  if user_conditions.any?
    halting_and_conditional(callback_sequence, user_callback, user_conditions, halted_lambda, filter, name)
  else
    halting(callback_sequence, user_callback, halted_lambda, filter, name)
  end
end

def self.halting(callback_sequence, user_callback, halted_lambda, filter, name)

def self.halting(callback_sequence, user_callback, halted_lambda, filter, name)
  callback_sequence.before do |env|
    target = env.target
    value  = env.value
    halted = env.halted
    unless halted
      result_lambda = -> { user_callback.call target, value }
      env.halted = halted_lambda.call(target, result_lambda)
      if env.halted
        target.send :halted_callback_hook, filter, name
      end
    end
    env
  end
end

def self.halting_and_conditional(callback_sequence, user_callback, user_conditions, halted_lambda, filter, name)

def self.halting_and_conditional(callback_sequence, user_callback, user_conditions, halted_lambda, filter, name)
  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_lambda = -> { user_callback.call target, value }
      env.halted = halted_lambda.call(target, result_lambda)
      if env.halted
        target.send :halted_callback_hook, filter, name
      end
    end
    env
  end
end