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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(models = []) ⇒ Observer

Returns a new instance of Observer.



66
67
68
69
# File 'lib/welo/core/observation.rb', line 66

def initialize(models=[])
  @registrations = {}
  @models = models
end

Instance Attribute Details

#modelsObject (readonly)

Returns the value of attribute models.



65
66
67
# File 'lib/welo/core/observation.rb', line 65

def models
  @models
end

#registrationsObject (readonly)

Returns the value of attribute registrations.



65
66
67
# File 'lib/welo/core/observation.rb', line 65

def registrations
  @registrations
end

Instance Method Details

#event(name, obj) ⇒ Object



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

def event(name, obj)
  registrations[name].each do |blk|
    blk.call(obj)
  end
end

#observe_source(source, observation_struct) ⇒ Object

TODO: unregister



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

def observe_source(source, observation_struct)
  data = source.observe
  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

#register(event_name, &blk) ⇒ Object



77
78
79
80
# File 'lib/welo/core/observation.rb', line 77

def register(event_name,&blk)
  registrations[event_name] ||= []
  registrations[event_name] << blk 
end