welo 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/TODO +0 -4
  2. data/lib/welo.rb +1 -1
  3. data/lib/welo/core/observation.rb +52 -15
  4. metadata +4 -15
data/TODO CHANGED
@@ -1,9 +1,5 @@
1
1
  * Embeddings
2
2
  - merge the branch
3
3
  - use the complex serialization overhead only when needed loaded
4
- * Observations
5
- - boil-down to a better API
6
- + improve the DelayedObservation thing
7
- + improve the Observer's eventing
8
4
  * Proxies
9
5
  - a class like observations to manipulate distant
@@ -1,6 +1,6 @@
1
1
 
2
2
  module Welo
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  AUTHORS = ['crapooze']
5
5
  WEBSITE = "http://github.com/crapooze/welo"
6
6
  LICENCE = "MIT"
@@ -22,6 +22,7 @@ module Welo
22
22
  raise ArgumentError.new("no such perspective: #{name} for #{resource}") unless persp
23
23
  st = self.new(*persp.fields, &blk)
24
24
  st.resource = resource
25
+ st.perspective = name
25
26
  st
26
27
  end
27
28
 
@@ -39,6 +40,10 @@ module Welo
39
40
  # of resource it's looking at.
40
41
  attr_accessor :resource
41
42
 
43
+ # The perspective of an ObservationStruct is the parts
44
+ # of resource it's looking at.
45
+ attr_accessor :perspective
46
+
42
47
  # Instanciates a new struct object (i.e., not a Struct class). The
43
48
  # fields are populated from the key/value pairs from hash. If the
44
49
  # structure fields correspond to a relationship, then a
@@ -62,34 +67,66 @@ module Welo
62
67
  # An Observer is an object which is responsible for creating resources
63
68
  # observations.
64
69
  class Observer
65
- attr_reader :registrations, :models
66
- def initialize(models=[])
70
+ Registration = Struct.new(:event_name, :cb)
71
+
72
+ # A hash mapping event name to their registrations
73
+ attr_reader :registrations
74
+
75
+ def initialize
67
76
  @registrations = {}
68
- @models = models
69
77
  end
70
78
 
71
- def event(name, obj)
72
- registrations[name].each do |blk|
73
- blk.call(obj)
79
+ # Calls all the callback for the registrations in one event named according
80
+ # to the first parameter.
81
+ # A second parameter can be passed to the callbacks.
82
+ #
83
+ # The name :observation should be reserved to the observing duties.
84
+ # See observe_source for why.
85
+ def event(name, obj=nil)
86
+ regs = registrations[name]
87
+ return unless regs
88
+ regs.each do |reg|
89
+ reg.cb.call(obj)
74
90
  end
75
91
  end
76
92
 
93
+ # Registers a new callback given in the block parameter for the given event.
94
+ # Returns a Registration instance, you should keep track of this instance if
95
+ # you plan to unregister it later.
77
96
  def register(event_name,&blk)
78
97
  registrations[event_name] ||= []
79
- registrations[event_name] << blk
98
+ reg = Registration.new(event_name,blk)
99
+ registrations[event_name] << reg
100
+ reg
80
101
  end
81
102
 
82
- #TODO: unregister
103
+ # Removes exactly one registration given the Registration instance.
104
+ # The parameter should be a value previously returned by register on the
105
+ # same object.
106
+ def unregister(registration)
107
+ regs = registrations[registration.event_name]
108
+ raise ArgumentError, "no registratons for #{registration.event_name}" unless regs
109
+ regs.delete(registration)
110
+ end
111
+
112
+ # Removes all the registrations for a given event name.
113
+ def unregister_all(event_name)
114
+ registrations.delete(event_name)
115
+ end
83
116
 
117
+ # Observe a source by calling it's observe method, and pushing it's
118
+ # successive yielded values into a new instance of observation_struct.
119
+ # Then calls the :observation event with the new observation as parameter.
84
120
  def observe_source(source, observation_struct)
85
- data = source.observe
86
- hash = {}
87
- observation_struct.members.each do |sym|
88
- hash[sym] = data[sym.to_s]
121
+ source.observe do |data|
122
+ hash = {}
123
+ observation_struct.members.each do |sym|
124
+ hash[sym] = data[sym.to_s]
125
+ end
126
+ obs = observation_struct.for_hash(hash)
127
+ obs._source_ = source
128
+ event(:observation, obs)
89
129
  end
90
- obs = observation_struct.for_hash(hash)
91
- obs._source_ = source
92
- event(:observation, obs)
93
130
  end
94
131
  end
95
132
  end
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: welo
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 1
9
- - 0
10
- version: 0.1.0
4
+ prerelease:
5
+ version: 0.1.1
11
6
  platform: ruby
12
7
  authors:
13
8
  - crapooze
@@ -15,7 +10,7 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-03-18 00:00:00 -04:00
13
+ date: 2011-04-10 00:00:00 -04:00
19
14
  default_executable:
20
15
  dependencies: []
21
16
 
@@ -54,23 +49,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
54
49
  requirements:
55
50
  - - ">="
56
51
  - !ruby/object:Gem::Version
57
- hash: 3
58
- segments:
59
- - 0
60
52
  version: "0"
61
53
  required_rubygems_version: !ruby/object:Gem::Requirement
62
54
  none: false
63
55
  requirements:
64
56
  - - ">="
65
57
  - !ruby/object:Gem::Version
66
- hash: 3
67
- segments:
68
- - 0
69
58
  version: "0"
70
59
  requirements: []
71
60
 
72
61
  rubyforge_project: welo
73
- rubygems_version: 1.3.7
62
+ rubygems_version: 1.6.2
74
63
  signing_key:
75
64
  specification_version: 3
76
65
  summary: A light resource model for Ruby