sentinel 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +18 -7
- data/VERSION +1 -1
- data/lib/sentinel.rb +1 -1
- data/rails/init.rb +1 -0
- data/spec/sentinel_spec.rb +18 -14
- metadata +14 -4
data/README.textile
CHANGED
@@ -18,6 +18,13 @@ $ [sudo] gem install sentinel
|
|
18
18
|
|
19
19
|
Then, add it as a dependency in your code using your favorite way (a simple require or mechanisms like the Bundler gem).
|
20
20
|
|
21
|
+
On Rails, you can install as a plugin:
|
22
|
+
|
23
|
+
<pre>
|
24
|
+
$ cd your_rails_app
|
25
|
+
$ ./script/plugin install http://github.com/lucashugaro/sentinel.git
|
26
|
+
</pre>
|
27
|
+
|
21
28
|
To use it, first you'll need an observer class with the Sentinel mixin. This class contains the methods to be notified and the configuration specifying what subject methods will be observed. See an example below:
|
22
29
|
|
23
30
|
<pre>
|
@@ -28,11 +35,11 @@ class MyObserver
|
|
28
35
|
observe MyClass, :class_method, :method_to_notify => :notify_class_method, :class_method => true #to observe a class method
|
29
36
|
|
30
37
|
def self.notify(options, *args)
|
31
|
-
puts "Do your thing!"
|
38
|
+
puts "Do your thing! Called from #{options[:subject]}"
|
32
39
|
end
|
33
40
|
|
34
41
|
def self.notify_class_method(options, *args)
|
35
|
-
puts "Called class_method!"
|
42
|
+
puts "Called class_method! Returned #{options[:result]}"
|
36
43
|
end
|
37
44
|
end
|
38
45
|
</pre>
|
@@ -51,20 +58,20 @@ class MyClass
|
|
51
58
|
end
|
52
59
|
</pre>
|
53
60
|
|
54
|
-
And... that's it! Every time the subject (in this case, MyClass) method is called, the specified observer method will be called too. By default, the observer method is called *
|
61
|
+
And... that's it! Every time the subject (in this case, MyClass) method is called, the specified observer method will be called too. By default, the observer method is called *after* the subject method execution, but you can call it *before* it too (we'll see that in the next example).
|
55
62
|
|
56
|
-
The parameters passed to the subject method are passed to the observer method via the *args array. The options hash
|
63
|
+
The parameters passed to the subject method are passed to the observer method via the *args array. The observer method also receives an options hash with the key :subject, which contains the actual subject object and the key :result, which contains the return of the observed method in case you intercepted the call after its execution.
|
57
64
|
|
58
|
-
Below is
|
65
|
+
Below is an example showing the interception before the execution:
|
59
66
|
|
60
67
|
<pre>
|
61
68
|
class MyObserver
|
62
69
|
include Sentinel
|
63
70
|
|
64
|
-
observe MyClass, :instance_method, :intercept => :
|
71
|
+
observe MyClass, :instance_method, :intercept => :before
|
65
72
|
|
66
73
|
def self.notify(options, *args)
|
67
|
-
puts "Called from #{options[:subject]} with arguments #{args.inspect}
|
74
|
+
puts "Called from #{options[:subject]} with arguments #{args.inspect}"
|
68
75
|
end
|
69
76
|
end
|
70
77
|
</pre>
|
@@ -79,6 +86,10 @@ h2. Note on Patches/Pull Requests
|
|
79
86
|
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
80
87
|
* Send me a pull request. Bonus points for topic branches.
|
81
88
|
|
89
|
+
h2. Contributors
|
90
|
+
|
91
|
+
* "Celestino Gomes":http://twitter.com/tinogomes
|
92
|
+
|
82
93
|
h3. Copyright
|
83
94
|
|
84
95
|
Copyright (c) 2010 Lucas Húngaro. See LICENSE for details.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.6.0
|
data/lib/sentinel.rb
CHANGED
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'sentinel'
|
data/spec/sentinel_spec.rb
CHANGED
@@ -13,24 +13,28 @@ describe Sentinel do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
context "basic premises" do
|
16
|
-
it "should not modify the subject method output when called
|
16
|
+
it "should not modify the subject method output when called after it" do
|
17
17
|
MyObserver.send(:observe, SentinelSubject, :class_method, :class_method => true)
|
18
18
|
MyObserver.send(:observe, SentinelSubject, :instance_method)
|
19
|
+
subject = SentinelSubject.new
|
19
20
|
|
20
|
-
MyObserver.expects(:notify).
|
21
|
+
MyObserver.expects(:notify).once.with({:result => 42, :subject => subject})
|
22
|
+
MyObserver.expects(:notify).once.with({:result => 42, :subject => SentinelSubject})
|
21
23
|
|
22
24
|
SentinelSubject.class_method.should == 42
|
23
|
-
|
25
|
+
subject.instance_method.should == 42
|
24
26
|
end
|
25
27
|
|
26
|
-
it "should not modify the subject method output when called
|
27
|
-
MyObserver.send(:observe, SentinelSubject, :class_method, :class_method => true, :intercept => :
|
28
|
-
MyObserver.send(:observe, SentinelSubject, :instance_method, :intercept => :
|
28
|
+
it "should not modify the subject method output when called before it" do
|
29
|
+
MyObserver.send(:observe, SentinelSubject, :class_method, :class_method => true, :intercept => :before)
|
30
|
+
MyObserver.send(:observe, SentinelSubject, :instance_method, :intercept => :before)
|
31
|
+
subject = SentinelSubject.new
|
29
32
|
|
30
|
-
MyObserver.expects(:notify).
|
33
|
+
MyObserver.expects(:notify).once.with({:subject => SentinelSubject})
|
34
|
+
MyObserver.expects(:notify).once.with({:subject => subject})
|
31
35
|
|
32
36
|
SentinelSubject.class_method.should == 42
|
33
|
-
|
37
|
+
subject.instance_method.should == 42
|
34
38
|
end
|
35
39
|
|
36
40
|
it "should pass all parameters of the observed methods to the observer" do
|
@@ -39,8 +43,8 @@ describe Sentinel do
|
|
39
43
|
|
40
44
|
subject = SentinelSubject.new
|
41
45
|
|
42
|
-
MyObserver.expects(:notify).once.with({:subject => SentinelSubject}, "texto", 1)
|
43
|
-
MyObserver.expects(:notify).once.with({:subject => subject}, "texto", 1)
|
46
|
+
MyObserver.expects(:notify).once.with({:result => 'hi from class method with params!', :subject => SentinelSubject}, "texto", 1)
|
47
|
+
MyObserver.expects(:notify).once.with({:result => 'hi from instance method with params!', :subject => subject}, "texto", 1)
|
44
48
|
|
45
49
|
SentinelSubject.class_method_with_params("texto", 1)
|
46
50
|
subject.instance_method_with_params("texto", 1)
|
@@ -62,11 +66,11 @@ describe Sentinel do
|
|
62
66
|
SentinelSubject.class_method
|
63
67
|
end
|
64
68
|
|
65
|
-
it "should be called
|
69
|
+
it "should be called after the method execution" do
|
66
70
|
MyObserver.send(:observe, SentinelSubject, :instance_method)
|
67
71
|
subject = SentinelSubject.new
|
68
72
|
|
69
|
-
MyObserver.expects(:notify).once.with({:subject => subject})
|
73
|
+
MyObserver.expects(:notify).once.with({:result => 42, :subject => subject})
|
70
74
|
|
71
75
|
subject.instance_method
|
72
76
|
end
|
@@ -87,8 +91,8 @@ describe Sentinel do
|
|
87
91
|
|
88
92
|
context "when intercepting the call after the method execution" do
|
89
93
|
it "should pass the result of the execution to the observer" do
|
90
|
-
MyObserver.send(:observe, SentinelSubject, :instance_method
|
91
|
-
MyObserver.send(:observe, SentinelSubject, :class_method, :
|
94
|
+
MyObserver.send(:observe, SentinelSubject, :instance_method)
|
95
|
+
MyObserver.send(:observe, SentinelSubject, :class_method, :class_method => true)
|
92
96
|
|
93
97
|
subject = SentinelSubject.new
|
94
98
|
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sentinel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 7
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
|
-
-
|
8
|
+
- 6
|
8
9
|
- 0
|
9
|
-
version: 0.
|
10
|
+
version: 0.6.0
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- "Lucas H\xC3\xBAngaro"
|
@@ -14,16 +15,18 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2010-
|
18
|
+
date: 2010-06-10 00:00:00 -03:00
|
18
19
|
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
21
22
|
name: rspec
|
22
23
|
prerelease: false
|
23
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
24
26
|
requirements:
|
25
27
|
- - ">="
|
26
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 13
|
27
30
|
segments:
|
28
31
|
- 1
|
29
32
|
- 2
|
@@ -35,9 +38,11 @@ dependencies:
|
|
35
38
|
name: mocha
|
36
39
|
prerelease: false
|
37
40
|
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
38
42
|
requirements:
|
39
43
|
- - ">="
|
40
44
|
- !ruby/object:Gem::Version
|
45
|
+
hash: 43
|
41
46
|
segments:
|
42
47
|
- 0
|
43
48
|
- 9
|
@@ -63,6 +68,7 @@ files:
|
|
63
68
|
- VERSION
|
64
69
|
- lib/metaid.rb
|
65
70
|
- lib/sentinel.rb
|
71
|
+
- rails/init.rb
|
66
72
|
- spec/fixtures/my_observer.rb
|
67
73
|
- spec/fixtures/sentinel_subject.rb
|
68
74
|
- spec/sentinel_spec.rb
|
@@ -78,23 +84,27 @@ rdoc_options:
|
|
78
84
|
require_paths:
|
79
85
|
- lib
|
80
86
|
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
81
88
|
requirements:
|
82
89
|
- - ">="
|
83
90
|
- !ruby/object:Gem::Version
|
91
|
+
hash: 3
|
84
92
|
segments:
|
85
93
|
- 0
|
86
94
|
version: "0"
|
87
95
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
88
97
|
requirements:
|
89
98
|
- - ">="
|
90
99
|
- !ruby/object:Gem::Version
|
100
|
+
hash: 3
|
91
101
|
segments:
|
92
102
|
- 0
|
93
103
|
version: "0"
|
94
104
|
requirements: []
|
95
105
|
|
96
106
|
rubyforge_project:
|
97
|
-
rubygems_version: 1.3.
|
107
|
+
rubygems_version: 1.3.7
|
98
108
|
signing_key:
|
99
109
|
specification_version: 3
|
100
110
|
summary: Transparent (unobtrusive) Observers for your Ruby code
|