sentinel 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +13 -21
- data/VERSION +1 -1
- data/lib/sentinel.rb +2 -1
- data/spec/fixtures/my_observer.rb +2 -2
- data/spec/sentinel_spec.rb +5 -2
- metadata +3 -4
- data/sentinel.gemspec +0 -63
data/README.textile
CHANGED
@@ -14,34 +14,28 @@ First, install the gem:
|
|
14
14
|
$ [sudo] gem install sentinel
|
15
15
|
</pre>
|
16
16
|
|
17
|
-
Then, add it as a dependency in your code using your favorite way (a simple require or mechanisms like the
|
17
|
+
Then, add it as a dependency in your code using your favorite way (a simple require or mechanisms like the Bundler gem).
|
18
18
|
|
19
|
-
To use it, first you'll need an observer class
|
20
|
-
|
21
|
-
<pre>
|
22
|
-
class MyObserver
|
23
|
-
def self.notify(*args)
|
24
|
-
puts "Do your thing!"
|
25
|
-
end
|
26
|
-
end
|
27
|
-
</pre>
|
28
|
-
|
29
|
-
Then, simply include the _Sentinel_ module in the observer, setting up the desired class (or classes) and method (or methods) to be observed.
|
19
|
+
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:
|
30
20
|
|
31
21
|
<pre>
|
32
22
|
class MyObserver
|
33
23
|
include Sentinel
|
34
24
|
|
35
25
|
observe MyClass, :instance_method
|
36
|
-
observe MyClass, :class_method, :class_method => true #to observe a class method
|
26
|
+
observe MyClass, :class_method, :method_to_notify => :notify_class_method, :class_method => true #to observe a class method
|
37
27
|
|
38
|
-
def self.notify(*args)
|
28
|
+
def self.notify(options, *args)
|
39
29
|
puts "Do your thing!"
|
40
30
|
end
|
31
|
+
|
32
|
+
def self.notify_class_method(options, *args)
|
33
|
+
puts "Called class_method!"
|
34
|
+
end
|
41
35
|
end
|
42
36
|
</pre>
|
43
37
|
|
44
|
-
As you can see, Sentinel can observe both class and instance methods. Now, our actual class:
|
38
|
+
As you can see, Sentinel can observe both class and instance methods. The _notify_ method is the default one if you don't specify the :method_to_notify option. Now, our actual class:
|
45
39
|
|
46
40
|
<pre>
|
47
41
|
class MyClass
|
@@ -55,18 +49,16 @@ class MyClass
|
|
55
49
|
end
|
56
50
|
</pre>
|
57
51
|
|
58
|
-
And... that's it! Every time the
|
59
|
-
|
60
|
-
You can also choose another method to call when the observed method is called, instead of the default _notify_ method:
|
52
|
+
And... that's it! Every time the subject (in this case, MyClass) method is called, the specified observer method will be called *before* it, then it will run normally. The parameters passed to the subject method are passed to the observer method via the *args array. The options hash contains the key :subject, which contains the actual subject object in case you want to use it, like below:
|
61
53
|
|
62
54
|
<pre>
|
63
55
|
class MyObserver
|
64
56
|
include Sentinel
|
65
57
|
|
66
|
-
observe MyClass, :instance_method
|
58
|
+
observe MyClass, :instance_method
|
67
59
|
|
68
|
-
def self.
|
69
|
-
puts "
|
60
|
+
def self.notify(options, *args)
|
61
|
+
puts "Called from #{options[:subject]} with arguments #{args.inspect}"
|
70
62
|
end
|
71
63
|
end
|
72
64
|
</pre>
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/lib/sentinel.rb
CHANGED
@@ -15,7 +15,8 @@ module Sentinel
|
|
15
15
|
|
16
16
|
body = <<-CODE
|
17
17
|
define_method "#{method_name}_with_observer" do |*args|
|
18
|
-
|
18
|
+
observer_opt = {:subject => self}
|
19
|
+
sentinel.send("#{options[:method_to_notify]}", observer_opt, *args)
|
19
20
|
self.send("#{method_name}_without_observer", *args)
|
20
21
|
end
|
21
22
|
|
data/spec/sentinel_spec.rb
CHANGED
@@ -37,10 +37,13 @@ describe Sentinel, "defining an observer for a method" do
|
|
37
37
|
MyObserver.send(:observe, SentinelSubject, :class_method_with_params, :class_method => true)
|
38
38
|
MyObserver.send(:observe, SentinelSubject, :instance_method_with_params)
|
39
39
|
|
40
|
-
|
40
|
+
subject = SentinelSubject.new
|
41
|
+
|
42
|
+
MyObserver.expects(:notify).once.with({:subject => SentinelSubject}, "texto", 1)
|
43
|
+
MyObserver.expects(:notify).once.with({:subject => subject}, "texto", 1)
|
41
44
|
|
42
45
|
SentinelSubject.class_method_with_params("texto", 1)
|
43
|
-
|
46
|
+
subject.instance_method_with_params("texto", 1)
|
44
47
|
end
|
45
48
|
end
|
46
49
|
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 4
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.4.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- "Lucas H\xC3\xBAngaro"
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-04-19 00:00:00 -03:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -63,7 +63,6 @@ files:
|
|
63
63
|
- VERSION
|
64
64
|
- lib/metaid.rb
|
65
65
|
- lib/sentinel.rb
|
66
|
-
- sentinel.gemspec
|
67
66
|
- spec/fixtures/my_observer.rb
|
68
67
|
- spec/fixtures/sentinel_subject.rb
|
69
68
|
- spec/sentinel_spec.rb
|
data/sentinel.gemspec
DELETED
@@ -1,63 +0,0 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
-
# -*- encoding: utf-8 -*-
|
5
|
-
|
6
|
-
Gem::Specification.new do |s|
|
7
|
-
s.name = %q{sentinel}
|
8
|
-
s.version = "0.3.0"
|
9
|
-
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Lucas H\303\272ngaro"]
|
12
|
-
s.date = %q{2010-03-26}
|
13
|
-
s.description = %q{Transparent (unobtrusive) Observers for your Ruby code}
|
14
|
-
s.email = %q{lucashungaro@gmail.com}
|
15
|
-
s.extra_rdoc_files = [
|
16
|
-
"LICENSE",
|
17
|
-
"README.textile"
|
18
|
-
]
|
19
|
-
s.files = [
|
20
|
-
".document",
|
21
|
-
".gitignore",
|
22
|
-
"LICENSE",
|
23
|
-
"README.textile",
|
24
|
-
"Rakefile",
|
25
|
-
"VERSION",
|
26
|
-
"lib/metaid.rb",
|
27
|
-
"lib/sentinel.rb",
|
28
|
-
"sentinel.gemspec",
|
29
|
-
"spec/fixtures/my_observer.rb",
|
30
|
-
"spec/fixtures/sentinel_subject.rb",
|
31
|
-
"spec/sentinel_spec.rb",
|
32
|
-
"spec/spec.opts",
|
33
|
-
"spec/spec_helper.rb"
|
34
|
-
]
|
35
|
-
s.homepage = %q{http://github.com/lucashungaro/sentinel}
|
36
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
37
|
-
s.require_paths = ["lib"]
|
38
|
-
s.rubygems_version = %q{1.3.6}
|
39
|
-
s.summary = %q{Transparent (unobtrusive) Observers for your Ruby code}
|
40
|
-
s.test_files = [
|
41
|
-
"spec/fixtures/my_observer.rb",
|
42
|
-
"spec/fixtures/sentinel_subject.rb",
|
43
|
-
"spec/sentinel_spec.rb",
|
44
|
-
"spec/spec_helper.rb"
|
45
|
-
]
|
46
|
-
|
47
|
-
if s.respond_to? :specification_version then
|
48
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
49
|
-
s.specification_version = 3
|
50
|
-
|
51
|
-
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
52
|
-
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
53
|
-
s.add_development_dependency(%q<mocha>, [">= 0.9.8"])
|
54
|
-
else
|
55
|
-
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
56
|
-
s.add_dependency(%q<mocha>, [">= 0.9.8"])
|
57
|
-
end
|
58
|
-
else
|
59
|
-
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
60
|
-
s.add_dependency(%q<mocha>, [">= 0.9.8"])
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|