Sha256: 897b9412ed9df0adb3595effb0003c1db60871470a236a5825cc274d4a88a3c2

Contents?: true

Size: 1.47 KB

Versions: 26

Compression:

Stored size: 1.47 KB

Contents

module Callbacks
  module ClassMethods
    def additional_callbacks(arr=[])
      @additional_callbacks ||= arr
    end
    def callback_block(&block)
      @callback_block ||= block
    end
  end
  
  module InstanceMethods
    # Callbacks on bootstrap and configuration
    # Defines the callback accessors:
    #   call_before/after_bootstrap/configure_callbacks
    # 
    # When called, this method will first check to see if there 
    # are plugins and call those plugin's callbacks when called
    # The method (before/after_bootstrap/configure) is called
    # on self if the callback method is defined on self
    def callback(call_time, *args, &block)
      on_all_callbacks(call_time, *args, &block)
      callback_on_self(call_time, *args, &block)
    end
    
    def callbacks
      @callbacks ||= []
    end
    
    def on_all_callbacks(call_time, *args, &block)
      self.class.callback_block.call(self, call_time) if self.class.callback_block
    end
    
    private
    
    # TODO: Add docs
    def callback_on_self(call_time, *args, &block)
      if respond_to?(call_time)
        callbacks << call_time.to_sym
        case self.method(call_time).arity
        when 0
          self.send(call_time)
        when 1
          self.send(call_time, *args)
        else
          self.send(call_time, *args, &block)
        end
      end
    end
    
  end
  
  def self.included(receiver)
    receiver.extend         ClassMethods
    receiver.send :include, InstanceMethods
  end
end

Version data entries

26 entries across 26 versions & 3 rubygems

Version Path
auser-poolparty-1.3.10 lib/mixins/callbacks.rb
auser-poolparty-1.3.11 lib/mixins/callbacks.rb
auser-poolparty-1.3.12 lib/mixins/callbacks.rb
auser-poolparty-1.3.13 lib/mixins/callbacks.rb
auser-poolparty-1.3.14 lib/mixins/callbacks.rb
auser-poolparty-1.3.15 lib/mixins/callbacks.rb
auser-poolparty-1.3.16 lib/mixins/callbacks.rb
auser-poolparty-1.3.17 lib/mixins/callbacks.rb
auser-poolparty-1.3.2 lib/mixins/callbacks.rb
auser-poolparty-1.3.3 lib/mixins/callbacks.rb
auser-poolparty-1.3.4 lib/mixins/callbacks.rb
auser-poolparty-1.3.5 lib/mixins/callbacks.rb
auser-poolparty-1.3.6 lib/mixins/callbacks.rb
auser-poolparty-1.3.7 lib/mixins/callbacks.rb
auser-poolparty-1.3.8 lib/mixins/callbacks.rb
fairchild-poolparty-1.3.17 lib/mixins/callbacks.rb
fairchild-poolparty-1.3.5 lib/mixins/callbacks.rb
poolparty-1.3.15 lib/mixins/callbacks.rb
poolparty-1.3.14 lib/mixins/callbacks.rb
poolparty-1.3.13 lib/mixins/callbacks.rb