hanging_methods 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -28,9 +28,36 @@ interesting.add.a_method
28
28
  interesting.add.another_method(1, 2)
29
29
 
30
30
  puts interesting.hanging_method_invocations(:add)
31
+ # [[:a_method], [:another_method, 1, 2]]
31
32
  ```
32
33
 
33
34
 
35
+ ## Get notified of a hanging method
36
+
37
+ You can be notified of method being hanged and specify its result.
38
+
39
+ ```ruby
40
+ require 'rubygems'
41
+ require 'hanging_methods'
42
+
43
+ class Interesting
44
+ include HangingMethods
45
+
46
+ add_hanging_method :add, after_invocation: :added
47
+
48
+ private
49
+
50
+ def added(method_name_and_arguments)
51
+ # method_name_and_arguments is [:a_method, 1, 2]
52
+ return :very_interesting
53
+ end
54
+ end
55
+
56
+ interesting = Interesting.new
57
+ interesting.add.a_method(1, 2)
58
+ # returns :very_interesting
59
+ ```
60
+
34
61
  # Credits
35
62
 
36
63
  [Hanging Methods](https://github.com/michaelgpearce/hanging_methods) is maintained by [Michael Pearce](https://github.com/michaelgpearce)
@@ -30,7 +30,7 @@ module HangingMethods
30
30
  def add_hanging_method_name_and_args_invocation(hanging_method_name, method_name_and_args)
31
31
  hanging_method_invocations(hanging_method_name) << method_name_and_args
32
32
 
33
- if after_invocation = self.class.hanging_method_options(hanging_method_name)[:after_invocation]
33
+ ret = if after_invocation = self.class.hanging_method_options(hanging_method_name)[:after_invocation]
34
34
  send(after_invocation, method_name_and_args)
35
35
  end
36
36
  end
@@ -6,7 +6,6 @@ module HangingMethods
6
6
 
7
7
  def method_missing(name, *arguments, &block)
8
8
  @callback.call(name, *arguments)
9
- self
10
9
  end
11
10
  end
12
11
  end
@@ -1,3 +1,3 @@
1
1
  module HangingMethods
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -2,9 +2,12 @@ require 'spec_helper'
2
2
 
3
3
  describe HangingMethods::MethodCallNotifier do
4
4
  describe "#method_missing" do
5
+ let(:return_value) { rand }
6
+
5
7
  before do
6
8
  @notifier = HangingMethods::MethodCallNotifier.new do |*args|
7
9
  @callback_args = args
10
+ return_value
8
11
  end
9
12
  end
10
13
 
@@ -17,8 +20,8 @@ describe HangingMethods::MethodCallNotifier do
17
20
  expect(@callback_args).to eq([:call_a_method, 'with args'])
18
21
  end
19
22
 
20
- it "should return the notifier" do
21
- expect(subject).to eq(@notifier)
23
+ it "should return block result" do
24
+ expect(subject).to eq(return_value)
22
25
  end
23
26
  end
24
27
  end
@@ -10,11 +10,13 @@ describe HangingMethods do
10
10
  def add_more_invocations
11
11
  @add_more_invocations ||= []
12
12
  end
13
-
13
+
14
14
  private
15
15
 
16
16
  def add_more_after_invocation(method_name_and_args)
17
17
  add_more_invocations << method_name_and_args
18
+
19
+ method_name_and_args.size
18
20
  end
19
21
  end
20
22
 
@@ -37,15 +39,18 @@ describe HangingMethods do
37
39
  end
38
40
 
39
41
  context "with after_invocation" do
40
- before do
41
- client.add_more.a_method
42
- client.add_more.another_method('arg1', 'arg2')
43
- end
42
+ let!(:method_1_return) { client.add_more.a_method }
43
+ let!(:method_2_return) { client.add_more.another_method('arg1', 'arg2') }
44
44
 
45
45
  it "should add invocation" do
46
46
  subject
47
47
  expect(client.add_more_invocations).to eq [[:a_method], [:another_method, 'arg1', 'arg2']]
48
48
  end
49
+
50
+ it "should return the value of the after_invocation method" do
51
+ expect(method_1_return).to eq 1
52
+ expect(method_2_return).to eq 3
53
+ end
49
54
  end
50
55
  end
51
56
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hanging_methods
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -84,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
84
  version: '0'
85
85
  requirements: []
86
86
  rubyforge_project: hanging_methods
87
- rubygems_version: 1.8.21
87
+ rubygems_version: 1.8.25
88
88
  signing_key:
89
89
  specification_version: 3
90
90
  summary: Add a method that you can hang other methods