eager_beaver 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module EagerBeaver
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/eager_beaver.rb CHANGED
@@ -9,9 +9,7 @@ module EagerBeaver
9
9
 
10
10
  def method_missing(method_name, *args, &block)
11
11
  self.class.method_matchers.each do |method_matcher|
12
- mm = method_matcher.dup
13
- self.class.context = mm
14
- mm.original_receiver = self
12
+ mm = configure_matcher method_matcher
15
13
  if mm.match?(method_name)
16
14
  method_string = mm.evaluate mm.new_method_code_maker
17
15
  self.class.class_eval method_string, __FILE__, __LINE__ + 1
@@ -23,11 +21,18 @@ module EagerBeaver
23
21
 
24
22
  def respond_to_missing?(method_name, include_private=false)
25
23
  self.class.method_matchers.each do |method_matcher|
26
- return true if method_matcher.match?(method_name)
24
+ mm = configure_matcher method_matcher
25
+ return true if mm.match?(method_name)
27
26
  end
28
27
  super
29
28
  end
30
29
 
30
+ def configure_matcher(matcher)
31
+ mm = matcher.dup
32
+ mm.original_receiver = self
33
+ mm.original_receiver.class.context = mm
34
+ end
35
+
31
36
  module ClassMethods
32
37
  def method_matchers
33
38
  @method_matchers ||= []
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "matcher context" do
3
+ describe "EagerBeaver matcher context" do
4
4
 
5
5
  describe "#missing_method_name" do
6
6
  it "provides the name of the missing method" do
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe "EagerBeaver includer" do
4
+
5
+ describe "added methods" do
6
+
7
+ before :each do
8
+ @klass = Class.new do
9
+ include EagerBeaver
10
+ end
11
+ end
12
+
13
+ it "has #add_method_matcher" do
14
+ expect(@klass.methods).to include :add_method_matcher
15
+ end
16
+
17
+ it "has #method_matchers" do
18
+ expect(@klass.methods).to include :method_matchers
19
+ end
20
+
21
+ end
22
+
23
+ describe "#add_method_matcher" do
24
+
25
+ it "registers a new method matcher" do
26
+ klass = Class.new do
27
+ include EagerBeaver
28
+
29
+ add_method_matcher do |mm|
30
+ mm.matcher = lambda { true }
31
+ mm.new_method_code = lambda {
32
+ return %Q{
33
+ def #{context.missing_method_name}
34
+ end
35
+ }
36
+ }
37
+ end
38
+ end
39
+
40
+ klass.method_matchers.size.should == 1
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -0,0 +1,150 @@
1
+ require 'spec_helper'
2
+
3
+ describe "instance of EagerBeaver includer" do
4
+
5
+ before :each do
6
+ @klass = Class.new do
7
+ include EagerBeaver
8
+ end
9
+ end
10
+
11
+ it "has #method_missing" do
12
+ expect(@klass.instance_methods(:false)).to include :method_missing
13
+ end
14
+
15
+ it "has #respond_to_missing?" do
16
+ expect(@klass.instance_methods(:false)).to include :respond_to_missing?
17
+ end
18
+
19
+ describe "#respond_to?" do
20
+
21
+ before :each do
22
+ klass = Class.new do
23
+ include EagerBeaver
24
+
25
+ add_method_matcher do |mm|
26
+ mm.matcher = lambda { /\Aaaa_\w+\z/ =~ context.missing_method_name }
27
+ mm.new_method_code = lambda {
28
+ %Q{
29
+ def #{context.missing_method_name}
30
+ end
31
+ }
32
+ }
33
+ end
34
+ end
35
+ @instance = klass.new
36
+ end
37
+
38
+ it "returns true for matched method names" do
39
+ expect(@instance.respond_to? :aaa_1).to be_true
40
+ expect(@instance.respond_to? :aaa_2).to be_true
41
+ end
42
+
43
+ it "returns false for unmatched method names" do
44
+ expect(@instance.respond_to? :bbb_1).to be_false
45
+ expect(@instance.respond_to? :bbb_2).to be_false
46
+ end
47
+
48
+ end
49
+
50
+ describe "#method" do
51
+
52
+ before :each do
53
+ klass = Class.new do
54
+ include EagerBeaver
55
+
56
+ add_method_matcher do |mm|
57
+ mm.matcher = lambda { /\Aaaa_\w+\z/ =~ context.missing_method_name }
58
+ mm.new_method_code = lambda {
59
+ %Q{
60
+ def #{context.missing_method_name}
61
+ end
62
+ }
63
+ }
64
+ end
65
+ end
66
+ @instance = klass.new
67
+ end
68
+
69
+ it "returns a Method for matched method names" do
70
+ expect{ @instance.method :aaa_1}.to_not raise_error
71
+ expect{ @instance.method :aaa_2}.to_not raise_error
72
+ end
73
+
74
+ it "returns nil for unmatched method names" do
75
+ expect{ @instance.method :bbb_1 }.to raise_error
76
+ end
77
+
78
+ end
79
+
80
+ describe "#method_missing" do
81
+
82
+ it "invokes the first matching matcher" do
83
+ klass = Class.new do
84
+ include EagerBeaver
85
+
86
+ add_method_matcher do |mm|
87
+ mm.matcher = lambda { /\Aaaa\z/ =~ context.missing_method_name }
88
+ mm.new_method_code = lambda {
89
+ %Q{
90
+ def #{context.missing_method_name}
91
+ 1
92
+ end
93
+ }
94
+ }
95
+ end
96
+
97
+ add_method_matcher do |mm|
98
+ mm.matcher = lambda { /\Abbb\z/ =~ context.missing_method_name }
99
+ mm.new_method_code = lambda {
100
+ %Q{
101
+ def #{context.missing_method_name}
102
+ 2
103
+ end
104
+ }
105
+ }
106
+ end
107
+
108
+ add_method_matcher do |mm|
109
+ mm.matcher = lambda { /\Abbb\z/ =~ context.missing_method_name }
110
+ mm.new_method_code = lambda {
111
+ %Q{
112
+ def #{context.missing_method_name}
113
+ 3
114
+ end
115
+ }
116
+ }
117
+ end
118
+ end
119
+
120
+ klass.new.bbb.should == 2
121
+ end
122
+
123
+ it "calls super #method_missing if no matcher matches" do
124
+ klass1 = Class.new do
125
+ def method_missing(method_name, *args, &block)
126
+ 10
127
+ end
128
+ end
129
+
130
+ klass2 = Class.new(klass1) do
131
+ include EagerBeaver
132
+
133
+ add_method_matcher do |mm|
134
+ mm.matcher = lambda { /\Aaaa\z/ =~ context.missing_method_name }
135
+ mm.new_method_code = lambda {
136
+ %Q{
137
+ def #{context.missing_method_name}
138
+ 1
139
+ end
140
+ }
141
+ }
142
+ end
143
+ end
144
+
145
+ klass2.new.bbb.should == 10
146
+ end
147
+
148
+ end
149
+
150
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eager_beaver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -46,7 +46,8 @@ files:
46
46
  - lib/eager_beaver/method_matcher.rb
47
47
  - lib/eager_beaver/version.rb
48
48
  - spec/eager_beaver/context_spec.rb
49
- - spec/eager_beaver/eager_beaver_spec.rb
49
+ - spec/eager_beaver/includer_spec.rb
50
+ - spec/eager_beaver/instance_spec.rb
50
51
  - spec/spec_helper.rb
51
52
  homepage: http://github.com/kevinburleigh75/eager_beaver
52
53
  licenses: []
@@ -78,5 +79,6 @@ summary: ! 'Facilitates method_missing, respond_to_missing?, and method-generati
78
79
  as well. Generated methods are added to the missing method receiver.'
79
80
  test_files:
80
81
  - spec/eager_beaver/context_spec.rb
81
- - spec/eager_beaver/eager_beaver_spec.rb
82
+ - spec/eager_beaver/includer_spec.rb
83
+ - spec/eager_beaver/instance_spec.rb
82
84
  - spec/spec_helper.rb
@@ -1,135 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe EagerBeaver do
4
-
5
- describe ".included" do
6
-
7
- before :each do
8
- @klass = Class.new do
9
- include EagerBeaver
10
- end
11
- end
12
-
13
- it "adds Includer.add_method_matcher" do
14
- expect(@klass.methods).to include :add_method_matcher
15
- end
16
-
17
- it "adds Includer.method_matchers" do
18
- expect(@klass.methods).to include :method_matchers
19
- end
20
-
21
- it "adds Includer.context" do
22
- expect(@klass.methods).to include :context
23
- end
24
-
25
- it "adds Includer.context=" do
26
- expect(@klass.methods).to include :context=
27
- end
28
-
29
- it "adds Includer#method_missing" do
30
- expect(@klass.instance_methods(:false)).to include :method_missing
31
- end
32
-
33
- it "adds Includer#respond_to_missing?" do
34
- expect(@klass.instance_methods(:false)).to include :respond_to_missing?
35
- end
36
-
37
- end
38
-
39
- describe "#add_method_matcher" do
40
-
41
- it "registers a new method matcher" do
42
- klass = Class.new do
43
- include EagerBeaver
44
-
45
- add_method_matcher do |mm|
46
- mm.matcher = lambda { true }
47
- mm.new_method_code = lambda {
48
- return %Q{
49
- def #{context.missing_method_name}
50
- end
51
- }
52
- }
53
- end
54
- end
55
-
56
- klass.method_matchers.size.should == 1
57
- end
58
-
59
- end
60
-
61
- describe "#method_missing" do
62
-
63
- context "method matching" do
64
-
65
- it "invokes the first matching matcher" do
66
- klass = Class.new do
67
- include EagerBeaver
68
-
69
- add_method_matcher do |mm|
70
- mm.matcher = lambda { /\Aaaa\z/ =~ context.missing_method_name }
71
- mm.new_method_code = lambda {
72
- %Q{
73
- def #{context.missing_method_name}
74
- 1
75
- end
76
- }
77
- }
78
- end
79
-
80
- add_method_matcher do |mm|
81
- mm.matcher = lambda { /\Abbb\z/ =~ context.missing_method_name }
82
- mm.new_method_code = lambda {
83
- %Q{
84
- def #{context.missing_method_name}
85
- 2
86
- end
87
- }
88
- }
89
- end
90
-
91
- add_method_matcher do |mm|
92
- mm.matcher = lambda { /\Abbb\z/ =~ context.missing_method_name }
93
- mm.new_method_code = lambda {
94
- %Q{
95
- def #{context.missing_method_name}
96
- 3
97
- end
98
- }
99
- }
100
- end
101
- end
102
-
103
- klass.new.bbb.should == 2
104
- end
105
-
106
- it "calls super #method_missing if no matcher matches" do
107
- klass1 = Class.new do
108
- def method_missing(method_name, *args, &block)
109
- 10
110
- end
111
- end
112
-
113
- klass2 = Class.new(klass1) do
114
- include EagerBeaver
115
-
116
- add_method_matcher do |mm|
117
- mm.matcher = lambda { /\Aaaa\z/ =~ context.missing_method_name }
118
- mm.new_method_code = lambda {
119
- %Q{
120
- def #{context.missing_method_name}
121
- 1
122
- end
123
- }
124
- }
125
- end
126
- end
127
-
128
- klass2.new.bbb.should == 10
129
- end
130
-
131
- end
132
-
133
- end
134
-
135
- end