inheritance_module_eval 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +1 -1
- data/HISTORY.md +9 -1
- data/README.md +16 -8
- data/lib/inheritance_module_eval/integration.rb +16 -0
- data/lib/inheritance_module_eval/version.rb +1 -1
- data/lib/inheritance_module_eval.rb +2 -18
- data/spec/inheritance_spec.rb +53 -54
- data/spec/intergation_spec.rb +18 -0
- metadata +8 -7
data/.travis.yml
CHANGED
data/HISTORY.md
CHANGED
data/README.md
CHANGED
@@ -9,30 +9,38 @@ Installation
|
|
9
9
|
|
10
10
|
Usage
|
11
11
|
-----
|
12
|
-
|
12
|
+
Inheritance module_eval provides 2 methods for eval instance and class methods:
|
13
13
|
|
14
|
-
*
|
15
|
-
*
|
14
|
+
* instance_eval_on
|
15
|
+
* class_eval_on
|
16
16
|
|
17
|
-
instance_module_eval method used to eval instance methods (ie: methods of an object)
|
18
17
|
|
19
|
-
|
18
|
+
InheritanceModuleEval.instance_eval_on(instance_object, code = nil, &block) #method used to eval instance methods (ie: methods of an object)
|
19
|
+
InheritanceModuleEval.class_eval_on(class_object, code = nil, &block) #method used to eval class methods (ie: methods of an class)
|
20
20
|
|
21
|
+
Note: you may include
|
22
|
+
|
23
|
+
require "inheritance_module_eval/integration"
|
24
|
+
to intergate inheritance_module_eval in ruby core. That will give 2 methods:
|
25
|
+
|
26
|
+
* instance_module_eval(code = nil, &block)
|
27
|
+
* class_module_eval(code = nil, &block)
|
21
28
|
|
22
29
|
Below is some simple example of inheritance_module_eval usage:
|
23
30
|
|
24
31
|
require 'inheritance_module_eval'
|
32
|
+
require "inheritance_module_eval/integration"
|
25
33
|
|
26
34
|
# Some dummy class that uses #instance_module_eval
|
27
35
|
class Content
|
28
36
|
self.field(name)
|
29
37
|
instance_module_eval %{
|
30
38
|
def #{name}
|
31
|
-
|
39
|
+
@#{name}
|
32
40
|
end
|
33
41
|
|
34
42
|
def #{name}=(new_value)
|
35
|
-
|
43
|
+
@#{name}= new_value
|
36
44
|
end
|
37
45
|
}
|
38
46
|
end
|
@@ -63,7 +71,7 @@ Compatibility
|
|
63
71
|
-------------
|
64
72
|
tested with Ruby
|
65
73
|
|
66
|
-
* 1.8.7
|
74
|
+
* 1.8.7
|
67
75
|
* 1.9.2
|
68
76
|
* 1.9.3
|
69
77
|
* jruby
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require "integration_module_eval"
|
2
|
+
class Module
|
3
|
+
# acts same as module_eval, but saves method hierarchy
|
4
|
+
# should be called only for instance methods evaluation
|
5
|
+
# @params (see Module#module_eval)
|
6
|
+
def instance_module_eval(code = nil, &block)
|
7
|
+
InheritanceModuleEval.instance_eval_on self, code, &block
|
8
|
+
end
|
9
|
+
|
10
|
+
# acts same as module_eval, but saves method hierarchy
|
11
|
+
# should be called only for class methods evaluation
|
12
|
+
# @params (see Module#module_eval)
|
13
|
+
def class_module_eval(code = nil, &block)
|
14
|
+
InheritanceModuleEval.class_eval_on self, code, &block
|
15
|
+
end
|
16
|
+
end
|
@@ -1,27 +1,11 @@
|
|
1
1
|
require "inheritance_module_eval/version"
|
2
2
|
|
3
|
-
class Module
|
4
|
-
# acts same as module_eval, but saves method hierarchy
|
5
|
-
# should be called only for instance methods evaluation
|
6
|
-
# @params (see Module#module_eval)
|
7
|
-
def instance_module_eval(code = nil, &block)
|
8
|
-
InheritanceModuleEval.instance_eval_on self, code, &block
|
9
|
-
end
|
10
|
-
|
11
|
-
# acts same as module_eval, but saves method hierarchy
|
12
|
-
# should be called only for class methods evaluation
|
13
|
-
# @params (see Module#module_eval)
|
14
|
-
def class_module_eval(code = nil, &block)
|
15
|
-
InheritanceModuleEval.class_eval_on self, code, &block
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
3
|
module InheritanceModuleEval
|
20
|
-
def self.instance_eval_on(sender, code, &block)
|
4
|
+
def self.instance_eval_on(sender, code = nil, &block)
|
21
5
|
sender.send :include, (get_module_for code, &block)
|
22
6
|
end
|
23
7
|
|
24
|
-
def self.class_eval_on(sender, code, &block)
|
8
|
+
def self.class_eval_on(sender, code = nil, &block)
|
25
9
|
sender.send :extend, (get_module_for code, &block)
|
26
10
|
end
|
27
11
|
|
data/spec/inheritance_spec.rb
CHANGED
@@ -8,55 +8,46 @@ class Child < Parent
|
|
8
8
|
|
9
9
|
end
|
10
10
|
describe InheritanceModuleEval do
|
11
|
-
describe "integration" do
|
12
|
-
specify "class should respond to instance_module_eval" do
|
13
|
-
Class.new.should respond_to(:instance_module_eval)
|
14
|
-
end
|
15
|
-
|
16
|
-
specify "module should respond to instance_module_eval" do
|
17
|
-
Module.new.should respond_to(:instance_module_eval)
|
18
|
-
end
|
19
|
-
|
20
|
-
specify "class should respond to class_module_eval" do
|
21
|
-
Class.new.should respond_to(:class_module_eval)
|
22
|
-
end
|
23
|
-
|
24
|
-
specify "module should respond to class_module_eval" do
|
25
|
-
Module.new.should respond_to(:class_module_eval)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
11
|
describe "add dynamic methods" do
|
30
12
|
before(:each) do
|
31
13
|
@test_object = Class.new
|
32
14
|
end
|
33
15
|
|
34
|
-
|
35
|
-
|
36
|
-
@test_object.
|
16
|
+
it "should delegate to module_eval" do
|
17
|
+
mock_module = mock(:mock_module)
|
18
|
+
@test_object.stub(:include)
|
19
|
+
@test_object.stub(:extend)
|
20
|
+
Module.stub(:new).and_return(mock_module)
|
21
|
+
|
22
|
+
mock_module.should_receive(:module_eval).once
|
23
|
+
|
24
|
+
InheritanceModuleEval.instance_eval_on @test_object do
|
37
25
|
def say_hi
|
38
26
|
puts hi
|
39
27
|
end
|
40
|
-
|
41
|
-
|
28
|
+
end
|
29
|
+
|
30
|
+
mock_module.should_receive(:module_eval).once
|
31
|
+
|
32
|
+
InheritanceModuleEval.class_eval_on @test_object do
|
42
33
|
def say_hi
|
43
34
|
puts hi
|
44
35
|
end
|
45
|
-
|
36
|
+
end
|
46
37
|
end
|
47
38
|
|
48
39
|
describe "instance methods" do
|
49
40
|
it "should eval a string" do
|
50
|
-
@test_object
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
41
|
+
InheritanceModuleEval.instance_eval_on @test_object, %{
|
42
|
+
def say_hi
|
43
|
+
puts 'hi'
|
44
|
+
end
|
45
|
+
}
|
55
46
|
@test_object.new.should respond_to :say_hi
|
56
47
|
|
57
48
|
end
|
58
49
|
it "should eval a block" do
|
59
|
-
@test_object
|
50
|
+
InheritanceModuleEval.instance_eval_on @test_object do
|
60
51
|
def say_hi
|
61
52
|
puts 'hi'
|
62
53
|
end
|
@@ -67,20 +58,21 @@ describe InheritanceModuleEval do
|
|
67
58
|
|
68
59
|
describe "class_methods" do
|
69
60
|
it "should eval a string" do
|
70
|
-
@test_object
|
61
|
+
InheritanceModuleEval.class_eval_on @test_object do
|
71
62
|
def say_hi
|
72
63
|
puts 'hi'
|
73
64
|
end
|
74
|
-
|
75
|
-
@test_object.should respond_to :say_hi
|
65
|
+
end
|
76
66
|
|
67
|
+
@test_object.should respond_to :say_hi
|
77
68
|
end
|
78
69
|
it "should eval a block" do
|
79
|
-
@test_object
|
70
|
+
InheritanceModuleEval.class_eval_on @test_object do
|
80
71
|
def say_hi
|
81
72
|
puts 'hi'
|
82
73
|
end
|
83
74
|
end
|
75
|
+
|
84
76
|
@test_object.should respond_to :say_hi
|
85
77
|
end
|
86
78
|
end
|
@@ -88,32 +80,35 @@ describe InheritanceModuleEval do
|
|
88
80
|
|
89
81
|
describe "collisions" do
|
90
82
|
describe "define already defined method in a class" do
|
91
|
-
|
83
|
+
context "redefine it after class declaration" do
|
92
84
|
it "should has lower priority then method, declared in a class" do
|
93
85
|
klass = Class.new
|
94
86
|
klass.class_eval do
|
95
87
|
def say_hi
|
96
88
|
"hi from method" << " " << super
|
97
89
|
end
|
98
|
-
instance_module_eval %{
|
99
|
-
def say_hi
|
100
|
-
"hi from eval"
|
101
|
-
end
|
102
|
-
}
|
103
90
|
end
|
91
|
+
|
92
|
+
InheritanceModuleEval.instance_eval_on klass do
|
93
|
+
def say_hi
|
94
|
+
"hi from eval"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
104
98
|
klass.new.say_hi.should == "hi from method hi from eval"
|
105
99
|
end
|
106
100
|
end
|
107
101
|
|
108
|
-
|
109
|
-
it "should
|
102
|
+
context "redefine it before class declaration" do
|
103
|
+
it "should has lower priority then method, declared in a class" do
|
110
104
|
klass = Class.new
|
111
105
|
klass.class_eval do
|
112
|
-
|
106
|
+
InheritanceModuleEval.instance_eval_on self do
|
113
107
|
def say_hi
|
114
108
|
"tiny hi"
|
115
109
|
end
|
116
|
-
|
110
|
+
end
|
111
|
+
|
117
112
|
def say_hi
|
118
113
|
super.upcase
|
119
114
|
end
|
@@ -126,26 +121,30 @@ describe InheritanceModuleEval do
|
|
126
121
|
it "should push dynamic method up in the inheritance tree" do
|
127
122
|
klass = Class.new
|
128
123
|
klass.class_eval do
|
129
|
-
|
124
|
+
InheritanceModuleEval.instance_eval_on self do
|
130
125
|
def say_hi
|
131
126
|
[5]
|
132
127
|
end
|
133
|
-
|
134
|
-
|
128
|
+
end
|
129
|
+
|
130
|
+
InheritanceModuleEval.instance_eval_on self do
|
135
131
|
def say_hi
|
136
132
|
[4] + super
|
137
133
|
end
|
138
|
-
|
139
|
-
|
134
|
+
end
|
135
|
+
|
136
|
+
InheritanceModuleEval.instance_eval_on self do
|
140
137
|
def say_hi
|
141
138
|
[3] + super
|
142
139
|
end
|
143
|
-
|
144
|
-
|
140
|
+
end
|
141
|
+
|
142
|
+
InheritanceModuleEval.instance_eval_on self do
|
145
143
|
def say_hi
|
146
144
|
[2] + super
|
147
145
|
end
|
148
|
-
|
146
|
+
end
|
147
|
+
|
149
148
|
def say_hi
|
150
149
|
[1] + super
|
151
150
|
end
|
@@ -166,7 +165,7 @@ describe InheritanceModuleEval do
|
|
166
165
|
klass = Class.new
|
167
166
|
klass.class_eval do
|
168
167
|
include test_module
|
169
|
-
|
168
|
+
InheritanceModuleEval.instance_eval_on self do
|
170
169
|
def say_hi
|
171
170
|
["hi from eval"] + super
|
172
171
|
end
|
@@ -185,7 +184,7 @@ describe InheritanceModuleEval do
|
|
185
184
|
|
186
185
|
describe "#get_module_for" do
|
187
186
|
it "should return Module" do
|
188
|
-
InheritanceModuleEval.send :get_module_for, "
|
187
|
+
InheritanceModuleEval.send :get_module_for, "'hi'"
|
189
188
|
end
|
190
189
|
|
191
190
|
it "should raise ArgumentError if no code given" do
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "inheritance_module_eval/integration"
|
2
|
+
describe "integration" do
|
3
|
+
specify "class should respond to instance_module_eval" do
|
4
|
+
Class.new.should respond_to(:instance_module_eval)
|
5
|
+
end
|
6
|
+
|
7
|
+
specify "module should respond to instance_module_eval" do
|
8
|
+
Module.new.should respond_to(:instance_module_eval)
|
9
|
+
end
|
10
|
+
|
11
|
+
specify "class should respond to class_module_eval" do
|
12
|
+
Class.new.should respond_to(:class_module_eval)
|
13
|
+
end
|
14
|
+
|
15
|
+
specify "module should respond to class_module_eval" do
|
16
|
+
Module.new.should respond_to(:class_module_eval)
|
17
|
+
end
|
18
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: inheritance_module_eval
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-11-
|
12
|
+
date: 2011-11-13 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &19224720 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '2.6'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *19224720
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rake
|
27
|
-
requirement: &
|
27
|
+
requirement: &19224100 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *19224100
|
36
36
|
description: ! 'saves inheritance structure of dynamically created methods.
|
37
37
|
|
38
38
|
Allows to redefine dynamic method in the same class or in any child class, and call
|
@@ -53,8 +53,10 @@ files:
|
|
53
53
|
- Rakefile
|
54
54
|
- inheritance_module_eval.gemspec
|
55
55
|
- lib/inheritance_module_eval.rb
|
56
|
+
- lib/inheritance_module_eval/integration.rb
|
56
57
|
- lib/inheritance_module_eval/version.rb
|
57
58
|
- spec/inheritance_spec.rb
|
59
|
+
- spec/intergation_spec.rb
|
58
60
|
homepage: http://github.com/AlexParamonov/inheritance_module_eval
|
59
61
|
licenses: []
|
60
62
|
post_install_message:
|
@@ -80,4 +82,3 @@ signing_key:
|
|
80
82
|
specification_version: 3
|
81
83
|
summary: inheritance safe module_eval.
|
82
84
|
test_files: []
|
83
|
-
has_rdoc:
|