eyeballer 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.
data/README.md CHANGED
@@ -1,7 +1,34 @@
1
- couch_loafer===========
2
- Description goes here.
1
+ # Eyeballer
3
2
 
4
- COPYRIGHT
5
- =========
3
+ A simple observer written in Ruby.
6
4
 
7
- Copyright (c) 2008 Adam Groves. See LICENSE for details.
5
+ Observe:
6
+
7
+ class Foo
8
+
9
+ def save
10
+ "Saved!"
11
+ end
12
+
13
+ def whatever
14
+ "Whatever ..."
15
+ end
16
+
17
+ end
18
+
19
+ class MyObserver
20
+ include Eyeballer
21
+
22
+ observe :foo, :save => [:do_something, :do_something_else]
23
+ observe :foo, :whatever => :do_something_else
24
+
25
+ def do_something
26
+ "Do something!"
27
+ end
28
+
29
+ def do_something_else
30
+ "Do something else!"
31
+ end
32
+
33
+ end
34
+
data/lib/eyeballer.rb CHANGED
@@ -32,10 +32,13 @@ module Eyeballer
32
32
  end
33
33
 
34
34
  self.eyeballed_methods.keys.each do |method_name|
35
- unless method_defined? :"#{method_name}_without_eyeball"
36
- alias_method :"#{method_name}_without_eyeball", method_name
35
+ # Pinched from alias_method_chain:
36
+ method_name_without_punctuation, punctuation = method_name.to_s.sub(/([?!=])$/, ''), $1
37
+ aliased_name = "#{method_name_without_punctuation}_without_eyeball#{punctuation}"
38
+ unless method_defined? aliased_name
39
+ alias_method aliased_name, method_name
37
40
  define_method method_name do
38
- result = self.send("#{method_name}_without_eyeball")
41
+ result = self.send(aliased_name)
39
42
  eyeball_executer(method_name)
40
43
  result
41
44
  end
@@ -13,12 +13,22 @@ class Foo
13
13
  def other_activity
14
14
  "Busy again ..."
15
15
  end
16
+
17
+ def valid?
18
+ true
19
+ end
20
+
21
+ def destroy!
22
+ true
23
+ end
16
24
  end
17
25
 
18
26
  class Job
19
27
  def self.number_one;end
20
28
  def self.number_two;end
21
29
  def self.number_three;end
30
+ def self.number_four(instance);end
31
+ def self.number_five(instance);end
22
32
  end
23
33
 
24
34
  class Observer
@@ -31,6 +41,10 @@ class Observer
31
41
 
32
42
  observe :foo, :other_activity => :do_something_with_the_instance
33
43
 
44
+ observe :foo, :valid? => :process_files
45
+
46
+ observe :foo, :destroy! => :email_someone
47
+
34
48
  def do_something
35
49
  Job.number_one
36
50
  end
@@ -46,6 +60,14 @@ class Observer
46
60
  def do_something_with_the_instance(instance)
47
61
  instance.greeting = "The Observer says hello too!"
48
62
  end
63
+
64
+ def process_files(instance)
65
+ Job.number_four(instance)
66
+ end
67
+
68
+ def email_someone(instance)
69
+ Job.number_five(instance)
70
+ end
49
71
  end
50
72
 
51
73
 
@@ -67,4 +89,12 @@ describe Eyeballer do
67
89
  foo.other_activity.should == "Busy again ..."
68
90
  foo.greeting.should == "The Observer says hello too!"
69
91
  end
92
+
93
+ it "should cope with predicates and bang methods" do
94
+ foo = Foo.new
95
+ Job.should_receive(:number_four).with(foo)
96
+ foo.should be_valid
97
+ Job.should_receive(:number_five).with(foo)
98
+ foo.destroy!.should be_true
99
+ end
70
100
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eyeballer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Groves