123456789_123456789_123456789_123456789_123456789_

Class: ActiveSupport::Callbacks::Callback

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: activesupport/lib/active_support/callbacks.rb

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(name, filter, kind, options, chain_config) ⇒ Callback

[ GitHub ]

  
# File 'activesupport/lib/active_support/callbacks.rb', line 245

def initialize(name, filter, kind, options, chain_config)
  @chain_config = chain_config
  @name    = name
  @kind    = kind
  @filter  = filter
  @if      = check_conditionals(options[:if])
  @unless  = check_conditionals(options[:unless])

  compiled
end

Class Method Details

.build(chain, filter, kind, options)

[ GitHub ]

  
# File 'activesupport/lib/active_support/callbacks.rb', line 231

def self.build(chain, filter, kind, options)
  if filter.is_a?(String)
    raise ArgumentError, <<-MSG.squish
      Passing string to define a callback is not supported. See the `.set_callback`
      documentation to see supported values.
    MSG
  end

  new chain.name, filter, kind, options, chain.config
end

Instance Attribute Details

#chain_config (readonly)

[ GitHub ]

  
# File 'activesupport/lib/active_support/callbacks.rb', line 243

attr_reader :chain_config, :filter

#filter (readonly)

[ GitHub ]

  
# File 'activesupport/lib/active_support/callbacks.rb', line 243

attr_reader :chain_config, :filter

#kind (rw)

[ GitHub ]

  
# File 'activesupport/lib/active_support/callbacks.rb', line 242

attr_accessor :kind, :name

#name (rw)

[ GitHub ]

  
# File 'activesupport/lib/active_support/callbacks.rb', line 242

attr_accessor :kind, :name

Instance Method Details

#apply(callback_sequence)

Wraps code with filter

[ GitHub ]

  
# File 'activesupport/lib/active_support/callbacks.rb', line 299

def apply(callback_sequence)
  compiled.apply(callback_sequence)
end

#check_conditionals(conditionals) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/callbacks.rb', line 311

def check_conditionals(conditionals)
  return EMPTY_ARRAY if conditionals.blank?

  conditionals = Array(conditionals)
  if conditionals.any?(String)
    raise ArgumentError, <<-MSG.squish
      Passing string to be evaluated in :if and :unless conditional
      options is not supported. Pass a symbol for an instance method,
      or a lambda, proc or block, instead.
    MSG
  end

  conditionals.freeze
end

#compiled

[ GitHub ]

  
# File 'activesupport/lib/active_support/callbacks.rb', line 281

def compiled
  @compiled ||=
    begin
      user_conditions = conditions_lambdas
      user_callback = CallTemplate.build(@filter, self)

      case kind
      when :before
        Filters::Before.new(user_callback.make_lambda, user_conditions, chain_config, @filter, name)
      when :after
        Filters::After.new(user_callback.make_lambda, user_conditions, chain_config)
      when :around
        Filters::Around.new(user_callback, user_conditions)
      end
    end
end

#conditions_lambdas (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/callbacks.rb', line 326

def conditions_lambdas
  conditions =
    @if.map { |c| CallTemplate.build(c, self).make_lambda } +
    @unless.map { |c| CallTemplate.build(c, self).inverted_lambda }
  conditions.empty? ? EMPTY_ARRAY : conditions
end

#current_scopes

[ GitHub ]

  
# File 'activesupport/lib/active_support/callbacks.rb', line 303

def current_scopes
  Array(chain_config[:scope]).map { |s| public_send(s) }
end

#duplicates?(other) ⇒ Boolean

[ GitHub ]

  
# File 'activesupport/lib/active_support/callbacks.rb', line 272

def duplicates?(other)
  case @filter
  when Symbol
    matches?(other.kind, other.filter)
  else
    false
  end
end

#matches?(_kind, _filter) ⇒ Boolean

[ GitHub ]

  
# File 'activesupport/lib/active_support/callbacks.rb', line 268

def matches?(_kind, _filter)
  @kind == _kind && filter == _filter
end

#merge_conditional_options(chain, if_option:, unless_option:)

[ GitHub ]

  
# File 'activesupport/lib/active_support/callbacks.rb', line 256

def merge_conditional_options(chain, if_option:, unless_option:)
  options = {
    if: @if.dup,
    unless: @unless.dup
  }

  options[:if].concat     Array(unless_option)
  options[:unless].concat Array(if_option)

  self.class.build chain, @filter, @kind, options
end