Module: Events

Included in:
Keepitsafe
Defined in:
lib/events.rb

Defined Under Namespace

Modules: EventClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

Add code for handeling the dsl on_error,after_error,etc…



26
27
28
29
30
31
32
# File 'lib/events.rb', line 26

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

Class Method Details

.included(base) ⇒ Object



3
4
5
# File 'lib/events.rb', line 3

def self.included(base)
  base.extend(Events::EventClassMethods)
end

Instance Method Details

#eventsObject



7
8
9
# File 'lib/events.rb', line 7

def events
  @events || {}
end

#register_handler(name, lambda = nil, &block) ⇒ Object



18
19
20
21
22
# File 'lib/events.rb', line 18

def register_handler name,lambda = nil, &block
  @events ||= {}
  @events[name.to_sym] ||= []
  @events[name.to_sym] << (lambda || block)
end

#respond_to?(sym) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/events.rb', line 34

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

#trigger(name, options = {}) ⇒ Object



11
12
13
14
15
16
# File 'lib/events.rb', line 11

def trigger name,options = {}
  @events ||= {}
  ((@events[name.to_sym] || []) + (self.class.events[name.to_sym] || [])).each do |v|
    v.call self,options
  end
end