Module: Events

Defined in:
lib/red_query/events.rb

Instance Method Summary collapse

Instance Method Details

#add_events(*event_names) ⇒ Object



15
16
17
18
19
# File 'lib/red_query/events.rb', line 15

def add_events(*event_names)
  event_names.each { |name| 
    @events[name] = []
  }
end

#fire(event_name, event = nil) ⇒ Object

fires event to all Procs registered for given event_name



36
37
38
39
# File 'lib/red_query/events.rb', line 36

def fire(event_name, event = nil)
  raise "invalid event '#{event_name.inspect}' on #{self.class}" if @events[event_name].nil?
  @events[event_name].each { |block| block.call(event) }
end

#has_event?(event_name) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/red_query/events.rb', line 21

def has_event?(event_name)
  !!@events[event_name]
end

#init_events(*event_names) ⇒ Object

Defines allowed Events eg: class Foo

include Events
def initialize
  init_events :bar, :bat, :bitz
end

end



10
11
12
13
# File 'lib/red_query/events.rb', line 10

def init_events(*event_names)
  @events = {}
  event_names.each { |name| @events[name] = [] }
end

#on(event_name, &block) ⇒ Object

registers Proc for given event_name

eg: foo = Foo.new (Foo includes Events) foo.on(“bar”) { |event| puts event }



30
31
32
33
# File 'lib/red_query/events.rb', line 30

def on(event_name, &block)
  raise "invalid event '#{event_name.inspect}' on #{self.class}" if @events[event_name].nil?
  @events[event_name].push(block)
end