module Unobservable::Support

Public Class Methods

extended(obj) click to toggle source

When an individual object EXTENDS the Support module, then we must ensure that the object's singleton class EXTENDS ModuleSupport.

# File lib/unobservable.rb, line 117
def self.extended(obj)
  obj.singleton_class.extend ModuleSupport
end
included(other_mod) click to toggle source

When a class/module INCLUDES the Support module, then we must ensure that the class/module also EXTENDS ModuleSupport.

# File lib/unobservable.rb, line 123
def self.included(other_mod)
  other_mod.extend ModuleSupport
end

Public Instance Methods

define_singleton_event(name, args = {}) click to toggle source

Defines an event directly on the object.

# File lib/unobservable.rb, line 143
def define_singleton_event(name, args = {})
  self.singleton_class.send(:define_event, name, args)
end
event(name) click to toggle source

Returns the Event that has the specified name. A NameError will be raised if the object does not define any event that has the given name.

# File lib/unobservable.rb, line 157
def event(name)
  @unobservable_events_map ||= {}
  e = @unobservable_events_map[name]
  if not e
    if self.events.include? name
      e = Event.new
      @unobservable_events_map[name] = e
    else
      raise NameError, "Undefined event: #{name}"
    end
  end
  return e
end
events(all = true) click to toggle source

Obtains the names of the events that are supported by this object. If all = false, then this list will only contain the names of the instance events that are explicitly defined by the object's class.

# File lib/unobservable.rb, line 150
def events(all = true)
  self.singleton_class.instance_events(all)
end
singleton_events(all = true) click to toggle source

Obtains the list of events that are unique to this object. If all = true, then this list will also include events that were defined within a module that the object extended.

# File lib/unobservable.rb, line 131
def singleton_events(all = true)
  if all
    contributors  = self.singleton_class.included_modules
    contributors -= self.class.included_modules
    contributors.push self.singleton_class
    Unobservable.collect_instance_events_defined_by(contributors)
  else
    Unobservable.collect_instance_events_defined_by([self.singleton_class])
  end
end

Private Instance Methods

raise_event(name, *args, &block) click to toggle source

Calls the Event that has the specified name. A NameError will be raised if the object does not define any event that has the given name.

# File lib/unobservable.rb, line 175
def raise_event(name, *args, &block)
  event(name).call(*args, &block)
end