Module: Unobservable::Support

Defined in:
lib/unobservable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(obj) ⇒ Object

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



82
83
84
# File 'lib/unobservable.rb', line 82

def self.extended(obj)
  obj.singleton_class.extend ModuleSupport
end

.included(other_mod) ⇒ Object

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



88
89
90
# File 'lib/unobservable.rb', line 88

def self.included(other_mod)
  other_mod.extend ModuleSupport
end

Instance Method Details

#define_singleton_event(*name) ⇒ Object

Defines an event directly on the object.



108
109
110
# File 'lib/unobservable.rb', line 108

def define_singleton_event(*name)
  self.singleton_class.send(:attr_event, *name)
end

#event(name) ⇒ Object

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.



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/unobservable.rb', line 122

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) ⇒ Object

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.



115
116
117
# File 'lib/unobservable.rb', line 115

def events(all = true)
  self.singleton_class.instance_events(all)
end

#singleton_events(all = true) ⇒ Object

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.



96
97
98
99
100
101
102
103
104
105
# File 'lib/unobservable.rb', line 96

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