Class: ActiveSupport::Callbacks::CallbackChain

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

Overview

:nodoc:

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.



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

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.



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

def config
  @config
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#append(*callbacks) ⇒ Object



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

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

#clearObject



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

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

#compile(type) ⇒ Object



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

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



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

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

#each(&block) ⇒ Object



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?;       @chain.empty?; end

#index(o) ⇒ Object



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

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

#initialize_copy(other) ⇒ Object



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

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

#insert(index, o) ⇒ Object



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

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

#prepend(*callbacks) ⇒ Object



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

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