Module: GollyUtils::Callbacks::ClassMethods

Defined in:
lib/golly-utils/callbacks.rb

Overview

Provides methods that can be run within definitions of classes that include GollyUtils::Callbacks.

Instance Method Summary collapse

Instance Method Details

#callbacksArray<Symbol>

Returns a list of all callbacks available to this class. (i.e. defined, inherited, and included.)

Returns:



176
177
178
179
180
# File 'lib/golly-utils/callbacks.rb', line 176

def callbacks
  c= superclass.respond_to?(:callbacks) ? superclass.callbacks : []
  c.concat _callbacks.keys
  c.uniq.sort_by(&:to_s)
end

#define_callbacks(*callbacks) ⇒ true Also known as: define_callback

Create one or more callback points for this class and its children.

Parameters:

  • callbacks (Array<String|Symbol>)

    The callback name(s).

Returns:

  • (true)

Raises:

  • If the callback has already been defined, or a method with that name already exists.

See Also:



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/golly-utils/callbacks.rb', line 153

def define_callbacks(*callbacks)
  callbacks.each do |name|
    name= ::GollyUtils::Callbacks.__norm_callback_key(name)

    if self.methods.include?(name.to_sym)
      raise "Can't create callback with name '#{name}'. A method with that name already exists."
    end

    _callbacks[name] ||= {}
    class_eval <<-EOB
      def self.#{name}(&block)
        v= (_callbacks[#{name.inspect}] ||= {})
        (v[:procs] ||= [])<< block
      end
    EOB
  end
  true
end