Sha256: c1282741cfc7e715fa88029abe8ba978e1b0e375a01b1c8ce6806be58d244d4e

Contents?: true

Size: 1.43 KB

Versions: 12

Compression:

Stored size: 1.43 KB

Contents

module Events
  
  def self.included(base)
    base.extend(Events::EventClassMethods)
  end
  
  def events
    @events || {}
  end
  
  def trigger name,options = {}
    @events ||= {}
    ((@events[name.to_sym] || []) + (self.class.events[name.to_sym] || [])).each do |v|
      v.call self,options
    end
  end
  
  def register_handler name,lambda = nil, &block
    @events ||= {}
    @events[name.to_sym] ||= []
    @events[name.to_sym] << (lambda || block)
  end
  
  
  # Add code for handeling the dsl on_error,after_error,etc...
  def method_missing(sym,*args, &block)
    if sym.to_s.match(/^(on|before|after)_/)
      register_handler(sym.to_s,args.first,&block)
    else
      super
    end
  end
  
  def respond_to?(sym)
    sym.to_s.match(/^(on|before|after)_/).nil? == false || super
  end
  
  module EventClassMethods
    
    def events
      @@events
    end
    
    def self.extended(base)
      @@events ||= {}
    end
  
    def register_handler name,lambda = nil, &block
      @@events[name.to_sym] ||= []
      @@events[name.to_sym] << (lambda || block)
    end
    
    # Add code for handeling the dsl on_error,after_error,etc...
    def method_missing(sym,*args, &block)
      if sym.to_s.match(/^(on|before|after)_/)
        register_handler(sym.to_s,args.first,&block)
      else
        super
      end
    end

    def respond_to?(sym)
      sym.to_s.match(/^(on|before|after)_/).nil? == false || super
    end
    
  end
  
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
keepitsafe-0.2.6 lib/events.rb
keepitsafe-0.2.5 lib/events.rb
keepitsafe-0.2.4 lib/events.rb
keepitsafe-0.2.3 lib/events.rb
keepitsafe-0.2.2 lib/events.rb
keepitsafe-0.2.1 lib/events.rb
keepitsafe-0.2.0 lib/events.rb
keepitsafe-0.1.10 lib/events.rb
keepitsafe-0.1.7 lib/events.rb
keepitsafe-0.1.5 lib/events.rb
keepitsafe-0.1.4 lib/events.rb
keepitsafe-0.1.3 lib/events.rb