Class: ActiveSupport::Callbacks::CallbackChain

Inherits:
Array show all
Defined in:
lib/active_support/callbacks.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveSupport::CoreExtensions::Array::RandomAccess

#rand

Methods included from ActiveSupport::CoreExtensions::Array::Grouping

#in_groups_of, #split

Methods included from ActiveSupport::CoreExtensions::Array::ExtractOptions

#extract_options!

Methods included from ActiveSupport::CoreExtensions::Array::Conversions

included, #to_formatted_s, #to_param, #to_query, #to_sentence, #to_xml

Methods included from ActiveSupport::CoreExtensions::Array::Access

#from, #to

Class Method Details

.build(kind, *methods, &block) ⇒ Object


80
81
82
83
84
# File 'lib/active_support/callbacks.rb', line 80

def self.build(kind, *methods, &block)
  methods, options = extract_options(*methods, &block)
  methods.map! { |method| Callback.new(kind, method, options) }
  new(methods)
end

Instance Method Details

#delete(callback) ⇒ Object


116
117
118
# File 'lib/active_support/callbacks.rb', line 116

def delete(callback)
  super(callback.is_a?(Callback) ? callback : find(callback))
end

#find(callback, &block) ⇒ Object


112
113
114
# File 'lib/active_support/callbacks.rb', line 112

def find(callback, &block)
  select { |c| c == callback && (!block_given? || yield(c)) }.first
end

#run(object, options = {}, &terminator) ⇒ Object


86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/active_support/callbacks.rb', line 86

def run(object, options = {}, &terminator)
  enumerator = options[:enumerator] || :each

  unless block_given?
    send(enumerator) { |callback| callback.call(object) }
  else
    send(enumerator) do |callback|
      result = callback.call(object)
      break result if terminator.call(result, object)
    end
  end
end

#|(chain) ⇒ Object


99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/active_support/callbacks.rb', line 99

def |(chain)
  if chain.is_a?(CallbackChain)
    chain.each { |callback| self | callback }
  else
    if (found_callback = find(chain)) && (index = index(chain))
      self[index] = chain
    else
      self << chain
    end
  end
  self
end