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.



117
118
119
# File 'lib/unobservable.rb', line 117

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.



123
124
125
# File 'lib/unobservable.rb', line 123

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

Instance Method Details

#define_singleton_event(name, args = {}) ⇒ Object

Defines an event directly on the object.



143
144
145
# File 'lib/unobservable.rb', line 143

def define_singleton_event(name, args = {})
  self.singleton_class.send(:define_event, name, args)
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.



157
158
159
160
161
162
163
164
165
166
167
168
169
# 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) ⇒ 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.



150
151
152
# File 'lib/unobservable.rb', line 150

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.



131
132
133
134
135
136
137
138
139
140
# 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