Class: Welo::Observer

Inherits:
Object
  • Object
show all
Defined in:
lib/welo/core/observation.rb

Overview

An Observer is an object which is responsible for creating resources observations.

Defined Under Namespace

Classes: Registration

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObserver

Returns a new instance of Observer.



75
76
77
# File 'lib/welo/core/observation.rb', line 75

def initialize
  @registrations = {}
end

Instance Attribute Details

#registrationsObject (readonly)

A hash mapping event name to their registrations



73
74
75
# File 'lib/welo/core/observation.rb', line 73

def registrations
  @registrations
end

Instance Method Details

#event(name, obj = nil) ⇒ Object

Calls all the callback for the registrations in one event named according to the first parameter. A second parameter can be passed to the callbacks.

The name :observation should be reserved to the observing duties. See observe_source for why.



85
86
87
88
89
90
91
# File 'lib/welo/core/observation.rb', line 85

def event(name, obj=nil)
  regs = registrations[name]
  return unless regs
  regs.each do |reg|
    reg.cb.call(obj)
  end
end

#observe_source(source, observation_struct) ⇒ Object

Observe a source by calling it’s observe method, and pushing it’s successive yielded values into a new instance of observation_struct. Then calls the :observation event with the new observation as parameter.



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/welo/core/observation.rb', line 120

def observe_source(source, observation_struct)
  source.observe do |data|
    hash = {}
    observation_struct.members.each do |sym|
      hash[sym] = data[sym.to_s]
    end
    obs = observation_struct.for_hash(hash)
    obs._source_ = source
    event(:observation, obs)
  end
end

#register(event_name, &blk) ⇒ Object

Registers a new callback given in the block parameter for the given event. Returns a Registration instance, you should keep track of this instance if you plan to unregister it later.



96
97
98
99
100
101
# File 'lib/welo/core/observation.rb', line 96

def register(event_name,&blk)
  registrations[event_name] ||= []
  reg = Registration.new(event_name,blk)
  registrations[event_name] << reg
  reg
end

#unregister(registration) ⇒ Object

Removes exactly one registration given the Registration instance. The parameter should be a value previously returned by register on the same object.

Raises:

  • (ArgumentError)


106
107
108
109
110
# File 'lib/welo/core/observation.rb', line 106

def unregister(registration)
  regs = registrations[registration.event_name]
  raise ArgumentError, "no registratons for #{registration.event_name}" unless regs
  regs.delete(registration)
end

#unregister_all(event_name) ⇒ Object

Removes all the registrations for a given event name.



113
114
115
# File 'lib/welo/core/observation.rb', line 113

def unregister_all(event_name)
  registrations.delete(event_name)
end