module EventObject

Constants

VERSION

Public Instance Methods

def(symbol, &b) click to toggle source

Define an instance method. `def` is public method.

@param [Symbol] @return [Symbol]

# File lib/event_object/class.rb, line 18
def def(symbol, &b)
  define_method symbol, &b
end
emit(*a, &b)
Alias for: fire
events(*ev) click to toggle source

Create events in specified class. For each event we create two methods: `event` and `event!`. `event` returns an array of event listeners. `event!` emits the listeners.

@param [Array] the array of the symbols @return [Symbol]

# File lib/event_object/class.rb, line 9
def events(*ev)
  ev.each { |name| self.def("#{name}!") { |*a, &b| var(name, Array).fire(*a, &b) } }
  ev.map { |name| self.def(name) { var(name, Array) } }
end
fire(*a, &b) click to toggle source

Emit event with specified arguments and specified block.

@example

using EventObject
event = [() -> 1, () -> 2]
p event.fire
# [1, 2]

@param *a […] arguments to be passed @param &b [Block] block to be passed @return [Array] the results of the call

# File lib/event_object/array.rb, line 14
def fire(*a, &b)
  dup.map { |h| h.call(*a, &b) }
end
Also aliased as: emit
instance_fire(context, *a) click to toggle source

Emit event with specified arguments within the context. It's giving the each listener access to object intance variables. Unlike fire, the block is not allowed.

@param context [Object] the context of the receiver @return [Array]

# File lib/event_object/array.rb, line 24
def instance_fire(context, *a)
  dup.map { |h| context.instance_exec(*a, &h) }
end
listen(instance, event, name) click to toggle source

Add the `instance` method with `name` like block for the specified event.

@param instance [Object] @param event [Symbol] @param name [Symbol] @return []

# File lib/event_object/object.rb, line 62
def listen(instance, event, name)
  instance.on event, method(name)
end
off(listener=nil) click to toggle source

Removes the specified listener from the array. If no arguments are supplied then all listeners will be removed.

@example

events = [() -> 1, () -> 2]
events.off

@param listener [Proc] listener or nil @return [Proc, Array] listener or nil or array of listeners

# File lib/event_object/array.rb, line 37
def off(listener=nil)
  listener ? delete(listener) : clear
end
on(event, listener=nil, &block) click to toggle source

Add the `listener` block for the specified `event`. `on` calls can be chained.

@example

class EventEmitter
  using Emmy
  events :success, :error
end
emitter = EventEmitter.new
emitter.on :success do
  [200, {}, "OK"]
end
p emitter.success!

@param event [Symbol] @param listener [Proc] @param block [Block] @return [Proc]

# File lib/event_object/object.rb, line 21
def on(event, listener=nil, &block)
  var(event, Array).tap { |e| e << (listener || block) }; (listener || block)
end
once(event, listener=nil, &block) click to toggle source

Add the `listener` block for the specified `event`. When event happened `listener` will be removed.

@param event @param listener @param block @return [Proc]

# File lib/event_object/object.rb, line 31
def once(event, listener=nil, &block)
  (ev = nil).tap { |ev|
    var(event, Array).tap { |e| e << (ev = ->(*a) { (listener || block).call(*a); e.delete(ev) }) }
  }
end
stop_listen(instance, event) click to toggle source

Remove all listeners being methods of the `instance`.

@param [] @return []

# File lib/event_object/object.rb, line 70
def stop_listen(instance, event)
  instance.var(event, Array).delete_if { |e| e.is_a?(Method) && e.receiver == self }
end
var(symbol, init=nil) click to toggle source

Get a class variable. If the variable is undefined then the variable will be initialized.

@param symbol [Symbol] the name of the variable @param init […] default value @return […] the value

# File lib/event_object/class.rb, line 27
def var(symbol, init=nil)
  class_variable_defined?(sym = "@#{symbol}") ? class_variable_get(sym) :
  class_variable_set(sym, (init.is_a?(Class) ? init.new : init))
end