Sha256: dfa5cb725f4944ce38e5aa2dfac91eb898938b9f20cbfe78ee84615c0e33cd58

Contents?: true

Size: 1.24 KB

Versions: 70

Compression:

Stored size: 1.24 KB

Contents

module Rubydora

# Provides class level methods for handling
# callback methods that alter object instances
  module Callbacks
    # add callback framework to base class
    # @param [Class] base 
    def self.included(base)
      base.extend ExtendableClassMethods
    end

    # hooks support
    module ExtendableClassMethods
      # creates the @hooks container ("hooks" are blocks or procs).
      # returns an array
      def hooks
        @hooks ||= {}
      end

      # register callback procs
      # @param [Array<Symbol>] hook name
      def register_callback *attrs
        attrs.each do |method_name|  
          next if methods.include? method_name.to_s
          instance_eval %Q{
            def #{method_name}(&blk)
              self.hooks[:#{method_name}] ||= []
              self.hooks[:#{method_name}] << blk
            end

            def clear_#{method_name}_blocks!
              self.hooks[:#{method_name}] = []
            end
          }

          class_eval %Q{
            def call_#{method_name}
              self.class.hooks[:#{method_name}] ||= []
              self.class.hooks[:#{method_name}].each do |h|
                instance_eval &h
              end
            end

          }
        end
      end
    end
  end
end

Version data entries

70 entries across 70 versions & 1 rubygems

Version Path
rubydora-1.8.1 lib/rubydora/callbacks.rb
rubydora-1.8.0 lib/rubydora/callbacks.rb
rubydora-1.7.5 lib/rubydora/callbacks.rb
rubydora-1.7.4 lib/rubydora/callbacks.rb
rubydora-1.7.3 lib/rubydora/callbacks.rb
rubydora-1.7.1 lib/rubydora/callbacks.rb
rubydora-1.7.0 lib/rubydora/callbacks.rb
rubydora-1.7.0.pre1 lib/rubydora/callbacks.rb
rubydora-1.6.5 lib/rubydora/callbacks.rb
rubydora-1.6.4 lib/rubydora/callbacks.rb
rubydora-1.6.3 lib/rubydora/callbacks.rb
rubydora-1.6.2 lib/rubydora/callbacks.rb
rubydora-1.6.1 lib/rubydora/callbacks.rb
rubydora-1.6.0 lib/rubydora/callbacks.rb
rubydora-1.5.1 lib/rubydora/callbacks.rb
rubydora-1.5.0 lib/rubydora/callbacks.rb
rubydora-1.4.2 lib/rubydora/callbacks.rb
rubydora-1.4.1 lib/rubydora/callbacks.rb
rubydora-1.4.0 lib/rubydora/callbacks.rb
rubydora-1.3.0 lib/rubydora/callbacks.rb