welo 0.0.7 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/TODO +4 -2
- data/lib/welo/core/observation.rb +76 -16
- data/lib/welo.rb +3 -3
- metadata +4 -4
data/TODO
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
- merge the branch
|
3
3
|
- use the complex serialization overhead only when needed loaded
|
4
4
|
* Observations
|
5
|
-
-
|
5
|
+
- boil-down to a better API
|
6
|
+
+ improve the DelayedObservation thing
|
7
|
+
+ improve the Observer's eventing
|
6
8
|
* Proxies
|
7
|
-
- a class like observations
|
9
|
+
- a class like observations to manipulate distant
|
@@ -1,35 +1,95 @@
|
|
1
1
|
|
2
2
|
module Welo
|
3
|
+
# DelayedObservation is a class representing a subsequent observation.
|
4
|
+
DelayedObservation = Struct.new(:observation, :relationship, :path)
|
5
|
+
|
6
|
+
# An ObservationStruct is a special Struct (i.e. a class whose
|
7
|
+
# instances are structures).
|
3
8
|
ObservationStruct = Class.new(Struct) do
|
9
|
+
# The source for an observation instance is the object from which it could
|
10
|
+
# be observed. Many applications may not matter, but some do.
|
11
|
+
attr_accessor :_source_
|
12
|
+
alias :source :_source_
|
13
|
+
|
14
|
+
# Creates a new structure, class which fields correspond to the perspective
|
15
|
+
# named name in resource. If a block is passed, then it is evaluated in
|
16
|
+
# the context of the new structure (like Struct.new { ... }).
|
17
|
+
#
|
18
|
+
# Normally, this method would be inherited by the structure, but we do not want to.
|
19
|
+
# Thus we undef it in self.inherited (see self.inherited).
|
20
|
+
def self.new_for_resource_in_perspective(resource, name, &blk)
|
21
|
+
persp = resource.perspective(name)
|
22
|
+
raise ArgumentError.new("no such perspective: #{name} for #{resource}") unless persp
|
23
|
+
st = self.new(*persp.fields, &blk)
|
24
|
+
st.resource = resource
|
25
|
+
st
|
26
|
+
end
|
27
|
+
|
28
|
+
# Hook to remove the new_for_resource_in_perspective methods, which is
|
29
|
+
# designed only for ObservationStruct but is inherited by the Struct
|
30
|
+
# classes if nothing is done.
|
31
|
+
def self.inherited(klass)
|
32
|
+
klass.instance_eval do
|
33
|
+
undef :new_for_resource_in_perspective
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
4
37
|
class << self
|
5
|
-
|
38
|
+
# The resource of an ObservationStruct is the type
|
39
|
+
# of resource it's looking at.
|
40
|
+
attr_accessor :resource
|
41
|
+
|
42
|
+
# Instanciates a new struct object (i.e., not a Struct class). The
|
43
|
+
# fields are populated from the key/value pairs from hash. If the
|
44
|
+
# structure fields correspond to a relationship, then a
|
45
|
+
# DelayedObservation object is created. It will then be the
|
46
|
+
# responsibility of the developper to continue observing the delayed
|
47
|
+
# observation or not.
|
6
48
|
def for_hash(hash)
|
7
49
|
vals = members.map do |k|
|
8
|
-
|
50
|
+
rel = resource.relationship(k)
|
51
|
+
if rel
|
52
|
+
DelayedObservation.new(self, rel, hash[k])
|
53
|
+
else
|
54
|
+
hash[k]
|
55
|
+
end
|
9
56
|
end
|
10
57
|
self.new(*vals)
|
11
58
|
end
|
12
59
|
end
|
13
60
|
end
|
14
61
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
62
|
+
# An Observer is an object which is responsible for creating resources
|
63
|
+
# observations.
|
64
|
+
class Observer
|
65
|
+
attr_reader :registrations, :models
|
66
|
+
def initialize(models=[])
|
67
|
+
@registrations = {}
|
68
|
+
@models = models
|
20
69
|
end
|
21
70
|
|
22
|
-
def
|
23
|
-
|
71
|
+
def event(name, obj)
|
72
|
+
registrations[name].each do |blk|
|
73
|
+
blk.call(obj)
|
74
|
+
end
|
24
75
|
end
|
25
76
|
|
26
|
-
def
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
77
|
+
def register(event_name,&blk)
|
78
|
+
registrations[event_name] ||= []
|
79
|
+
registrations[event_name] << blk
|
80
|
+
end
|
81
|
+
|
82
|
+
#TODO: unregister
|
83
|
+
|
84
|
+
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]
|
89
|
+
end
|
90
|
+
obs = observation_struct.for_hash(hash)
|
91
|
+
obs._source_ = source
|
92
|
+
event(:observation, obs)
|
33
93
|
end
|
34
94
|
end
|
35
95
|
end
|
data/lib/welo.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
|
2
2
|
module Welo
|
3
|
-
VERSION = "0.0
|
3
|
+
VERSION = "0.1.0"
|
4
4
|
AUTHORS = ['crapooze']
|
5
5
|
WEBSITE = "http://github.com/crapooze/welo"
|
6
6
|
LICENCE = "MIT"
|
7
|
-
|
8
|
-
|
7
|
+
require 'welo/base/resource'
|
8
|
+
require 'welo/core/observation'
|
9
9
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: welo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.7
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- crapooze
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-03-
|
18
|
+
date: 2011-03-18 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|