Module: RubyEvent::Generator

Defined in:
lib/ruby-event/generator.rb

Instance Method Summary collapse

Instance Method Details

#event(event_name) ⇒ Object

Constructs the methods and objects for an event automatically

Example:

event :data_received


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby-event/generator.rb', line 13

def event(event_name)
  self.class_eval <<-EOT
  
    def #{event_name}(*args, &block)
      _ensure_events
      if not @_events["#{event_name}"]
        @_events["#{event_name}"] = RubyEvent::Event.new
      end
      if args != []
        @_events["#{event_name}"].call(self, args)
      else
        if block
          #{event_name} + block
        else
          @_events["#{event_name}"]
        end
      end
    end
    
    def #{event_name}=(event)
      _ensure_events
      if RubyEvent::Event === event
        @_events["#{event_name}"] = event
      end
    end
    
    def _ensure_events
      if not @_events
        @_events = {}
      end
    end
    
    private :_ensure_events 
  EOT
end