Class: ActiveSupport::Callbacks::CallbackChain

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/active_support/callbacks.rb

Overview

:nodoc:

Defined Under Namespace

Classes: DefaultTerminator

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#as_json, #compact_blank, #exclude?, #excluding, #in_order_of, #including, #index_by, #index_with, #many?, #maximum, #minimum, #pick, #pluck, #sole

Constructor Details

#initialize(name, config) ⇒ CallbackChain

Returns a new instance of CallbackChain.



573
574
575
576
577
578
579
580
581
582
583
# File 'lib/active_support/callbacks.rb', line 573

def initialize(name, config)
  @name = name
  @config = {
    scope: [:kind],
    terminator: DEFAULT_TERMINATOR
  }.merge!(config)
  @chain = []
  @all_callbacks = nil
  @single_callbacks = {}
  @mutex = Mutex.new
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



571
572
573
# File 'lib/active_support/callbacks.rb', line 571

def config
  @config
end

#nameObject (readonly)

Returns the value of attribute name.



571
572
573
# File 'lib/active_support/callbacks.rb', line 571

def name
  @name
end

Instance Method Details

#append(*callbacks) ⇒ Object



633
634
635
# File 'lib/active_support/callbacks.rb', line 633

def append(*callbacks)
  callbacks.each { |c| append_one(c) }
end

#clearObject



601
602
603
604
605
606
# File 'lib/active_support/callbacks.rb', line 601

def clear
  @all_callbacks = nil
  @single_callbacks.clear
  @chain.clear
  self
end

#compile(type) ⇒ Object



615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# File 'lib/active_support/callbacks.rb', line 615

def compile(type)
  if type.nil?
    @all_callbacks || @mutex.synchronize do
      final_sequence = CallbackSequence.new
      @all_callbacks ||= @chain.reverse.inject(final_sequence) do |callback_sequence, callback|
        callback.apply(callback_sequence)
      end
    end
  else
    @single_callbacks[type] || @mutex.synchronize do
      final_sequence = CallbackSequence.new
      @single_callbacks[type] ||= @chain.reverse.inject(final_sequence) do |callback_sequence, callback|
        type == callback.kind ? callback.apply(callback_sequence) : callback_sequence
      end
    end
  end
end

#delete(o) ⇒ Object



595
596
597
598
599
# File 'lib/active_support/callbacks.rb', line 595

def delete(o)
  @all_callbacks = nil
  @single_callbacks.clear
  @chain.delete(o)
end

#each(&block) ⇒ Object



585
# File 'lib/active_support/callbacks.rb', line 585

def each(&block); @chain.each(&block); end

#empty?Boolean

Returns:

  • (Boolean)


587
# File 'lib/active_support/callbacks.rb', line 587

def empty?;       @chain.empty?; end

#index(o) ⇒ Object



586
# File 'lib/active_support/callbacks.rb', line 586

def index(o);     @chain.index(o); end

#initialize_copy(other) ⇒ Object



608
609
610
611
612
613
# File 'lib/active_support/callbacks.rb', line 608

def initialize_copy(other)
  @all_callbacks = nil
  @single_callbacks = {}
  @chain     = other.chain.dup
  @mutex     = Mutex.new
end

#insert(index, o) ⇒ Object



589
590
591
592
593
# File 'lib/active_support/callbacks.rb', line 589

def insert(index, o)
  @all_callbacks = nil
  @single_callbacks.clear
  @chain.insert(index, o)
end

#prepend(*callbacks) ⇒ Object



637
638
639
# File 'lib/active_support/callbacks.rb', line 637

def prepend(*callbacks)
  callbacks.each { |c| prepend_one(c) }
end