mtrack 0.0.6 → 1.0.0
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.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/Gemfile +4 -4
- data/README.md +7 -2
- data/lib/mtrack.rb +1 -3
- data/lib/mtrack/mixin.rb +182 -0
- data/lib/mtrack/version.rb +2 -1
- data/spec/lib/mtrack/mixin_spec.rb +405 -0
- metadata +6 -9
- data/lib/mtrack/core.rb +0 -111
- data/lib/mtrack/module_mixin.rb +0 -90
- data/spec/lib/mtrack/core_spec.rb +0 -377
- data/spec/lib/mtrack/module_mixin_spec.rb +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b7d5ea4aa7136ed160c80c7c6e466ad9bd1ac740
|
4
|
+
data.tar.gz: 550043786f96f7223b185eaf3fd127e9745116cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc1c526fe0e5936825f9756c9f4f5b8c7d4577c6c22b100796e01aaa32004c9c01694efde3d4dd5b0a7f8995f7bb77430bb16fa334021643f9b5a1a14f2a022b
|
7
|
+
data.tar.gz: d10f0fc4dcba69e9704d4806c3cdbc17b6cbae7dd9beb6ed9ff5b193f63e78ec24262cd10bc932c9ac47da5204a6a94d98094e9525da40a70608f9c7485cb246
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.1.
|
1
|
+
2.1.3
|
data/Gemfile
CHANGED
@@ -2,15 +2,15 @@ source "https://rubygems.org"
|
|
2
2
|
gemspec
|
3
3
|
|
4
4
|
group :development do
|
5
|
-
gem "byebug", "~> 3.
|
5
|
+
gem "byebug", "~> 3.5.1"
|
6
6
|
gem "guard-rspec", "~> 4.3.1"
|
7
|
-
gem "libnotify", "~> 0.8.
|
7
|
+
gem "libnotify", "~> 0.8.4"
|
8
8
|
gem "pry", "~> 0.10.1"
|
9
9
|
end
|
10
10
|
|
11
11
|
group :development, :test do
|
12
|
-
gem "codeclimate-test-reporter", "~> 0.4.
|
12
|
+
gem "codeclimate-test-reporter", "~> 0.4.1"
|
13
13
|
gem "rake", "~> 10.3.2"
|
14
14
|
gem "rspec", "~> 3.1.0"
|
15
|
-
gem "simplecov", "~> 0.9.
|
15
|
+
gem "simplecov", "~> 0.9.1"
|
16
16
|
end
|
data/README.md
CHANGED
@@ -4,13 +4,12 @@
|
|
4
4
|
[][travis]
|
5
5
|
[][codeclimate]
|
6
6
|
[][codeclimate]
|
7
|
-
[][gemnasium]
|
8
8
|
[][inch-ci]
|
9
9
|
|
10
10
|
[gem]: https://rubygems.org/gems/mtrack
|
11
11
|
[travis]: http://travis-ci.org/gdeoliveira/mtrack
|
12
12
|
[codeclimate]: https://codeclimate.com/github/gdeoliveira/mtrack
|
13
|
-
[gemnasium]: https://gemnasium.com/gdeoliveira/mtrack
|
14
13
|
[gemnasium]: https://gemnasium.com/gdeoliveira/mtrack#development-dependencies
|
15
14
|
[inch-ci]: http://inch-ci.org/github/gdeoliveira/mtrack
|
16
15
|
|
@@ -42,6 +41,8 @@ To track a group of methods within a Module (or a Class).
|
|
42
41
|
require "mtrack"
|
43
42
|
|
44
43
|
module Stooges
|
44
|
+
extend MTrack::Mixin
|
45
|
+
|
45
46
|
def shemp; end
|
46
47
|
|
47
48
|
track_methods do
|
@@ -60,6 +61,8 @@ Methods can be grouped using an optional name.
|
|
60
61
|
require "mtrack"
|
61
62
|
|
62
63
|
module Numbers
|
64
|
+
extend MTrack::Mixin
|
65
|
+
|
63
66
|
def zero; end
|
64
67
|
|
65
68
|
track_methods :integers do
|
@@ -109,6 +112,8 @@ abstraction for the state machine.
|
|
109
112
|
require "mtrack"
|
110
113
|
|
111
114
|
class SimpleStateMachine
|
115
|
+
extend MTrack::Mixin
|
116
|
+
|
112
117
|
class << self
|
113
118
|
private
|
114
119
|
|
data/lib/mtrack.rb
CHANGED
data/lib/mtrack/mixin.rb
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
require "mtrack/state"
|
2
|
+
|
3
|
+
module MTrack
|
4
|
+
##
|
5
|
+
# This module provides the #track_methods method to Classes or Modules that
|
6
|
+
# mix it in. It also enables the extended Class or Module to pass tracked
|
7
|
+
# methods to its subclasses and submodules.
|
8
|
+
module Mixin
|
9
|
+
class << self
|
10
|
+
private
|
11
|
+
|
12
|
+
##
|
13
|
+
# call-seq:
|
14
|
+
# extended(submodule) => submodule
|
15
|
+
#
|
16
|
+
# Initializes a State variable on the Class or Module that extended Mixin.
|
17
|
+
#
|
18
|
+
# Returns passed +submodule+.
|
19
|
+
def extended(submodule)
|
20
|
+
submodule.instance_eval { @__mtrack__ ||= State.new }
|
21
|
+
submodule
|
22
|
+
end
|
23
|
+
|
24
|
+
##
|
25
|
+
# call-seq:
|
26
|
+
# newly_defined_methods(mod, old_methods) => set
|
27
|
+
#
|
28
|
+
# Calculates the difference between +mod+'s currently defined public
|
29
|
+
# methods and +old_methods+.
|
30
|
+
#
|
31
|
+
# Returns a set with the result.
|
32
|
+
def newly_defined_methods(mod, old_methods)
|
33
|
+
(mod.public_instance_methods(false) - old_methods).map(&:to_sym).to_set
|
34
|
+
end
|
35
|
+
|
36
|
+
##
|
37
|
+
# call-seq:
|
38
|
+
# save_tracked_methods(mod, group_name, tracked) => nil
|
39
|
+
#
|
40
|
+
# Saves +tracked+ methods for +mod+ under a +group_name+.
|
41
|
+
#
|
42
|
+
# Returns a +nil+ value.
|
43
|
+
def save_tracked_methods(mod, group_name, tracked)
|
44
|
+
mod.instance_variable_get(:@__mtrack__)[group_name].merge_tracked tracked unless tracked.empty?
|
45
|
+
nil
|
46
|
+
end
|
47
|
+
|
48
|
+
##
|
49
|
+
# call-seq:
|
50
|
+
# track_methods_for(mod, group_name) => set
|
51
|
+
# track_methods_for(mod, group_name) {|| ... } => set
|
52
|
+
#
|
53
|
+
# Sets up an MTrack::State instance for +mod+.
|
54
|
+
#
|
55
|
+
# If a block is provided all the methods defined within the block will be
|
56
|
+
# tracked under the +group_name+ parameter.
|
57
|
+
#
|
58
|
+
# Returns a set containing the methods that were defined within the block.
|
59
|
+
def track_methods_for(mod, group_name, &b)
|
60
|
+
old_methods = mod.public_instance_methods false
|
61
|
+
|
62
|
+
begin
|
63
|
+
mod.module_eval &b if block_given?
|
64
|
+
ensure
|
65
|
+
tracked = newly_defined_methods(mod, old_methods)
|
66
|
+
save_tracked_methods(mod, group_name, tracked)
|
67
|
+
end
|
68
|
+
|
69
|
+
tracked
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
public
|
74
|
+
|
75
|
+
##
|
76
|
+
# call-seq:
|
77
|
+
# tracked_methods(group_name = nil) => set
|
78
|
+
#
|
79
|
+
# Returns a set containing the currently tracked methods for a +group_name+.
|
80
|
+
#
|
81
|
+
# class C
|
82
|
+
# extend MTrack::Mixin
|
83
|
+
# track_methods :my_group do
|
84
|
+
# def method_1; end
|
85
|
+
# def method_2; end
|
86
|
+
# end
|
87
|
+
# end
|
88
|
+
#
|
89
|
+
# C.tracked_methods :my_group #=> #<Set: {:method_1, :method_2}>
|
90
|
+
def tracked_methods(group_name = nil)
|
91
|
+
@__mtrack__.tracked group_name
|
92
|
+
end
|
93
|
+
|
94
|
+
private
|
95
|
+
|
96
|
+
##
|
97
|
+
# call-seq:
|
98
|
+
# included(submodule) => submodule
|
99
|
+
#
|
100
|
+
# Sets this state as a super-state of the +submodule+ (Class or Module) that
|
101
|
+
# has included the current Module.
|
102
|
+
#
|
103
|
+
# Returns passed +submodule+.
|
104
|
+
def included(submodule)
|
105
|
+
state = @__mtrack__
|
106
|
+
submodule.instance_eval do
|
107
|
+
extend Mixin
|
108
|
+
@__mtrack__.add_super_state state
|
109
|
+
end
|
110
|
+
submodule
|
111
|
+
end
|
112
|
+
|
113
|
+
##
|
114
|
+
# call-seq:
|
115
|
+
# inherited(submodule) => submodule
|
116
|
+
#
|
117
|
+
# Sets this state as a super-state of the +submodule+ (Class) that has
|
118
|
+
# inherited from the current Class.
|
119
|
+
#
|
120
|
+
# Returns passed +submodule+.
|
121
|
+
alias_method :inherited, :included
|
122
|
+
|
123
|
+
##
|
124
|
+
# call-seq:
|
125
|
+
# method_added(name) => name
|
126
|
+
#
|
127
|
+
# Allows method +name+ to be displayed on #tracked_methods once again after
|
128
|
+
# being disabled by a call to #method_undefined.
|
129
|
+
#
|
130
|
+
# Returns passed +name+.
|
131
|
+
def method_added(name)
|
132
|
+
@__mtrack__.delete_undefined name
|
133
|
+
end
|
134
|
+
|
135
|
+
##
|
136
|
+
# call-seq:
|
137
|
+
# method_removed(name) => name
|
138
|
+
#
|
139
|
+
# Stops tracking method +name+ in the current Class or Module.
|
140
|
+
#
|
141
|
+
# Returns passed +name+.
|
142
|
+
def method_removed(name)
|
143
|
+
@__mtrack__.delete_tracked name
|
144
|
+
end
|
145
|
+
|
146
|
+
##
|
147
|
+
# call-seq:
|
148
|
+
# method_undefined(name) => name
|
149
|
+
#
|
150
|
+
# Stops tracking method +name+ in the current Class or Module and prevents
|
151
|
+
# homonymous methods tracked in super-states from being displayed as
|
152
|
+
# #tracked_methods.
|
153
|
+
#
|
154
|
+
# Returns passed +name+.
|
155
|
+
def method_undefined(name)
|
156
|
+
@__mtrack__.delete_tracked name
|
157
|
+
@__mtrack__.add_undefined name
|
158
|
+
end
|
159
|
+
|
160
|
+
##
|
161
|
+
# call-seq:
|
162
|
+
# track_methods(group_name = nil) => set
|
163
|
+
# track_methods(group_name = nil) {|| ... } => set
|
164
|
+
#
|
165
|
+
# If a block is provided all the methods defined within the block will be
|
166
|
+
# tracked under the optional +group_name+ parameter.
|
167
|
+
#
|
168
|
+
# Returns a set containing the methods that were defined within the block.
|
169
|
+
#
|
170
|
+
# class C
|
171
|
+
# extend MTrack::Mixin
|
172
|
+
# track_methods do
|
173
|
+
# def method_1; end
|
174
|
+
# track_methods(:inner_group_1) { def method_2; end }
|
175
|
+
# def method_3; end
|
176
|
+
# end
|
177
|
+
# end #=> #<Set: {:method_1, :method_2, :method_3}>
|
178
|
+
def track_methods(group_name = nil, &b)
|
179
|
+
Mixin.send(:track_methods_for, self, group_name, &b)
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
data/lib/mtrack/version.rb
CHANGED
@@ -4,6 +4,7 @@
|
|
4
4
|
# even through a hierarchy of inclusion and/or inheritance.
|
5
5
|
#
|
6
6
|
# module M
|
7
|
+
# extend MTrack::Mixin
|
7
8
|
# track_methods { def method_1; end }
|
8
9
|
# end
|
9
10
|
#
|
@@ -20,5 +21,5 @@
|
|
20
21
|
module MTrack
|
21
22
|
|
22
23
|
# Current version of MTrack.
|
23
|
-
VERSION = "0.0
|
24
|
+
VERSION = "1.0.0"
|
24
25
|
end
|
@@ -0,0 +1,405 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
METHOD_DEFINITION = proc {}
|
4
|
+
|
5
|
+
describe MTrack::Mixin do
|
6
|
+
describe "#track_methods" do
|
7
|
+
it "is added after extending #{described_class}" do
|
8
|
+
mod = ::Module.new
|
9
|
+
expect(mod.private_methods.map(&:to_sym)).not_to include(:track_methods)
|
10
|
+
mod.module_eval { extend MTrack::Mixin }
|
11
|
+
expect(mod.private_methods.map(&:to_sym)).to include(:track_methods)
|
12
|
+
end
|
13
|
+
|
14
|
+
context "no block given" do
|
15
|
+
it "returns an empty set" do
|
16
|
+
mod = ::Module.new.module_eval do
|
17
|
+
extend MTrack::Mixin
|
18
|
+
track_methods :group
|
19
|
+
end
|
20
|
+
expect(mod).to be_a(Set)
|
21
|
+
expect(mod).to be_empty
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "hierarchy" do
|
27
|
+
let(:base_module_1) do
|
28
|
+
Module.new.tap do |m|
|
29
|
+
m.module_eval do
|
30
|
+
extend MTrack::Mixin
|
31
|
+
define_method :unt_1, METHOD_DEFINITION
|
32
|
+
track_methods { define_method :meth, METHOD_DEFINITION }
|
33
|
+
track_methods :numbers do
|
34
|
+
track_methods(:odd) { define_method :one, METHOD_DEFINITION }
|
35
|
+
track_methods(:even) { define_method :two, METHOD_DEFINITION }
|
36
|
+
end
|
37
|
+
define_method :unt_2, METHOD_DEFINITION
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
let(:base_module_2) do
|
43
|
+
Module.new.tap do |m|
|
44
|
+
m.module_eval do
|
45
|
+
extend MTrack::Mixin
|
46
|
+
define_method :unt_2, METHOD_DEFINITION
|
47
|
+
track_methods { define_method :meth, METHOD_DEFINITION }
|
48
|
+
track_methods :numbers do
|
49
|
+
track_methods(:even) { define_method :two, METHOD_DEFINITION }
|
50
|
+
track_methods(:odd) { define_method :three, METHOD_DEFINITION }
|
51
|
+
end
|
52
|
+
define_method :unt_3, METHOD_DEFINITION
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
let(:base_module_3) do
|
58
|
+
Module.new.tap do |m|
|
59
|
+
m.module_eval do
|
60
|
+
extend MTrack::Mixin
|
61
|
+
define_method :unt_3, METHOD_DEFINITION
|
62
|
+
track_methods { define_method :meth, METHOD_DEFINITION }
|
63
|
+
track_methods :numbers do
|
64
|
+
track_methods(:odd) { define_method :three, METHOD_DEFINITION }
|
65
|
+
track_methods(:even) { define_method :four, METHOD_DEFINITION }
|
66
|
+
end
|
67
|
+
define_method :unt_4, METHOD_DEFINITION
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
let(:sub_module_1) do
|
73
|
+
bm_1 = base_module_1
|
74
|
+
bm_2 = base_module_2
|
75
|
+
Module.new.tap do |m|
|
76
|
+
m.module_eval do
|
77
|
+
include bm_1
|
78
|
+
define_method :unt_4, METHOD_DEFINITION
|
79
|
+
track_methods { define_method :meth, METHOD_DEFINITION }
|
80
|
+
track_methods :numbers do
|
81
|
+
track_methods(:even) { define_method :four, METHOD_DEFINITION }
|
82
|
+
track_methods(:odd) { define_method :five, METHOD_DEFINITION }
|
83
|
+
end
|
84
|
+
define_method :unt_5, METHOD_DEFINITION
|
85
|
+
include bm_2
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
let(:sub_module_2) do
|
91
|
+
bm_3 = base_module_3
|
92
|
+
Module.new.tap do |m|
|
93
|
+
m.module_eval do
|
94
|
+
extend MTrack::Mixin
|
95
|
+
define_method :unt_5, METHOD_DEFINITION
|
96
|
+
track_methods { define_method :meth, METHOD_DEFINITION }
|
97
|
+
include bm_3
|
98
|
+
track_methods :numbers do
|
99
|
+
track_methods(:odd) { define_method :five, METHOD_DEFINITION }
|
100
|
+
track_methods(:even) { define_method :six, METHOD_DEFINITION }
|
101
|
+
end
|
102
|
+
define_method :unt_6, METHOD_DEFINITION
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
let(:super_class) do
|
108
|
+
sm_1 = sub_module_1
|
109
|
+
Class.new.tap do |c|
|
110
|
+
c.class_eval do
|
111
|
+
include sm_1
|
112
|
+
define_method :unt_6, METHOD_DEFINITION
|
113
|
+
track_methods { define_method :meth, METHOD_DEFINITION }
|
114
|
+
track_methods :numbers do
|
115
|
+
track_methods(:even) { define_method :six, METHOD_DEFINITION }
|
116
|
+
track_methods(:odd) { define_method :seven, METHOD_DEFINITION }
|
117
|
+
end
|
118
|
+
define_method :unt_7, METHOD_DEFINITION
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
let(:sub_class) do
|
124
|
+
sm_2 = sub_module_2
|
125
|
+
sc = super_class
|
126
|
+
Class.new(sc).tap do |c|
|
127
|
+
c.class_eval do
|
128
|
+
define_method :unt_7, METHOD_DEFINITION
|
129
|
+
track_methods { define_method :meth, METHOD_DEFINITION }
|
130
|
+
track_methods :numbers do
|
131
|
+
track_methods(:odd) { define_method :seven, METHOD_DEFINITION }
|
132
|
+
track_methods(:even) { define_method :eight, METHOD_DEFINITION }
|
133
|
+
end
|
134
|
+
define_method :unt_8, METHOD_DEFINITION
|
135
|
+
include sm_2
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
context "base module 1" do
|
141
|
+
subject { base_module_1 }
|
142
|
+
|
143
|
+
it "tracks methods" do
|
144
|
+
expect(subject.tracked_methods).to match_array([:meth])
|
145
|
+
expect(subject.tracked_methods(:numbers)).to match_array([:one, :two])
|
146
|
+
expect(subject.tracked_methods(:odd)).to match_array([:one])
|
147
|
+
expect(subject.tracked_methods(:even)).to match_array([:two])
|
148
|
+
end
|
149
|
+
|
150
|
+
it "untracks removed methods" do
|
151
|
+
subject.module_eval { remove_method :meth }
|
152
|
+
expect(subject.tracked_methods).to be_empty
|
153
|
+
end
|
154
|
+
|
155
|
+
it "untracks undefined methods" do
|
156
|
+
subject.module_eval { undef_method :meth }
|
157
|
+
expect(subject.tracked_methods).to be_empty
|
158
|
+
|
159
|
+
subject.module_eval { define_method :meth, METHOD_DEFINITION }
|
160
|
+
expect(subject.tracked_methods).to be_empty
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
context "base module 2" do
|
165
|
+
subject { base_module_2 }
|
166
|
+
|
167
|
+
it "tracks methods" do
|
168
|
+
expect(subject.tracked_methods).to match_array([:meth])
|
169
|
+
expect(subject.tracked_methods(:numbers)).to match_array([:two, :three])
|
170
|
+
expect(subject.tracked_methods(:even)).to match_array([:two])
|
171
|
+
expect(subject.tracked_methods(:odd)).to match_array([:three])
|
172
|
+
end
|
173
|
+
|
174
|
+
it "untracks removed methods" do
|
175
|
+
subject.module_eval { remove_method :meth }
|
176
|
+
expect(subject.tracked_methods).to be_empty
|
177
|
+
end
|
178
|
+
|
179
|
+
it "untracks undefined methods" do
|
180
|
+
subject.module_eval { undef_method :meth }
|
181
|
+
expect(subject.tracked_methods).to be_empty
|
182
|
+
|
183
|
+
subject.module_eval { define_method :meth, METHOD_DEFINITION }
|
184
|
+
expect(subject.tracked_methods).to be_empty
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
context "base module 3" do
|
189
|
+
subject { base_module_3 }
|
190
|
+
|
191
|
+
it "tracks methods" do
|
192
|
+
expect(subject.tracked_methods).to match_array([:meth])
|
193
|
+
expect(subject.tracked_methods(:numbers)).to match_array([:three, :four])
|
194
|
+
expect(subject.tracked_methods(:odd)).to match_array([:three])
|
195
|
+
expect(subject.tracked_methods(:even)).to match_array([:four])
|
196
|
+
end
|
197
|
+
|
198
|
+
it "untracks removed methods" do
|
199
|
+
subject.module_eval { remove_method :meth }
|
200
|
+
expect(subject.tracked_methods).to be_empty
|
201
|
+
end
|
202
|
+
|
203
|
+
it "untracks undefined methods" do
|
204
|
+
subject.module_eval { undef_method :meth }
|
205
|
+
expect(subject.tracked_methods).to be_empty
|
206
|
+
|
207
|
+
subject.module_eval { define_method :meth, METHOD_DEFINITION }
|
208
|
+
expect(subject.tracked_methods).to be_empty
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
context "sub module 1" do
|
213
|
+
subject { sub_module_1 }
|
214
|
+
|
215
|
+
it "tracks methods" do
|
216
|
+
expect(subject.tracked_methods).to match_array([:meth])
|
217
|
+
expect(subject.tracked_methods(:numbers)).to match_array([:one, :two, :three, :four, :five])
|
218
|
+
expect(subject.tracked_methods(:odd)).to match_array([:one, :three, :five])
|
219
|
+
expect(subject.tracked_methods(:even)).to match_array([:two, :four])
|
220
|
+
end
|
221
|
+
|
222
|
+
it "untracks removed methods" do
|
223
|
+
subject.module_eval { remove_method :meth }
|
224
|
+
expect(subject.tracked_methods).to match_array([:meth])
|
225
|
+
|
226
|
+
base_module_1.module_eval { remove_method :meth }
|
227
|
+
expect(subject.tracked_methods).to match_array([:meth])
|
228
|
+
|
229
|
+
base_module_2.module_eval { remove_method :meth }
|
230
|
+
expect(subject.tracked_methods).to be_empty
|
231
|
+
end
|
232
|
+
|
233
|
+
it "untracks undefined methods" do
|
234
|
+
subject.module_eval { undef_method :meth }
|
235
|
+
expect(subject.tracked_methods).to be_empty
|
236
|
+
|
237
|
+
subject.module_eval { define_method :meth, METHOD_DEFINITION }
|
238
|
+
expect(subject.tracked_methods).to match_array([:meth])
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
context "sub module 2" do
|
243
|
+
subject { sub_module_2 }
|
244
|
+
|
245
|
+
it "tracks methods" do
|
246
|
+
expect(subject.tracked_methods).to match_array([:meth])
|
247
|
+
expect(subject.tracked_methods(:numbers)).to match_array([:three, :four, :five, :six])
|
248
|
+
expect(subject.tracked_methods(:odd)).to match_array([:three, :five])
|
249
|
+
expect(subject.tracked_methods(:even)).to match_array([:four, :six])
|
250
|
+
end
|
251
|
+
|
252
|
+
it "untracks removed methods" do
|
253
|
+
base_module_3.module_eval { remove_method :meth }
|
254
|
+
expect(subject.tracked_methods).to match_array([:meth])
|
255
|
+
|
256
|
+
subject.module_eval { remove_method :meth }
|
257
|
+
expect(subject.tracked_methods).to be_empty
|
258
|
+
end
|
259
|
+
|
260
|
+
it "untracks undefined methods" do
|
261
|
+
subject.module_eval { undef_method :meth }
|
262
|
+
expect(subject.tracked_methods).to be_empty
|
263
|
+
|
264
|
+
subject.module_eval { define_method :meth, METHOD_DEFINITION }
|
265
|
+
expect(subject.tracked_methods).to match_array([:meth])
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
context "super class" do
|
270
|
+
subject { super_class }
|
271
|
+
|
272
|
+
it "tracks methods" do
|
273
|
+
expect(subject.tracked_methods).to match_array([:meth])
|
274
|
+
expect(subject.tracked_methods(:numbers)).to match_array([:one, :two, :three, :four, :five, :six, :seven])
|
275
|
+
expect(subject.tracked_methods(:odd)).to match_array([:one, :three, :five, :seven])
|
276
|
+
expect(subject.tracked_methods(:even)).to match_array([:two, :four, :six])
|
277
|
+
end
|
278
|
+
|
279
|
+
it "untracks removed methods" do
|
280
|
+
subject.class_eval { remove_method :meth }
|
281
|
+
expect(subject.tracked_methods).to match_array([:meth])
|
282
|
+
|
283
|
+
sub_module_1.module_eval { remove_method :meth }
|
284
|
+
expect(subject.tracked_methods).to match_array([:meth])
|
285
|
+
|
286
|
+
base_module_2.module_eval { remove_method :meth }
|
287
|
+
expect(subject.tracked_methods).to match_array([:meth])
|
288
|
+
|
289
|
+
base_module_1.module_eval { remove_method :meth }
|
290
|
+
expect(subject.tracked_methods).to be_empty
|
291
|
+
end
|
292
|
+
|
293
|
+
it "untracks undefined methods" do
|
294
|
+
subject.module_eval { undef_method :meth }
|
295
|
+
expect(subject.tracked_methods).to be_empty
|
296
|
+
|
297
|
+
subject.module_eval { define_method :meth, METHOD_DEFINITION }
|
298
|
+
expect(subject.tracked_methods).to match_array([:meth])
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
302
|
+
context "sub class" do
|
303
|
+
subject { sub_class }
|
304
|
+
|
305
|
+
it "tracks methods" do
|
306
|
+
expect(subject.tracked_methods).to match_array([:meth])
|
307
|
+
expect(subject.tracked_methods(:numbers)).to match_array([:one, :two, :three, :four, :five, :six, :seven, :eight])
|
308
|
+
expect(subject.tracked_methods(:odd)).to match_array([:one, :three, :five, :seven])
|
309
|
+
expect(subject.tracked_methods(:even)).to match_array([:two, :four, :six, :eight])
|
310
|
+
end
|
311
|
+
|
312
|
+
it "untracks removed methods" do
|
313
|
+
base_module_1.module_eval { remove_method :meth }
|
314
|
+
expect(subject.tracked_methods).to match_array([:meth])
|
315
|
+
|
316
|
+
base_module_2.module_eval { remove_method :meth }
|
317
|
+
expect(subject.tracked_methods).to match_array([:meth])
|
318
|
+
|
319
|
+
base_module_3.module_eval { remove_method :meth }
|
320
|
+
expect(subject.tracked_methods).to match_array([:meth])
|
321
|
+
|
322
|
+
sub_module_1.module_eval { remove_method :meth }
|
323
|
+
expect(subject.tracked_methods).to match_array([:meth])
|
324
|
+
|
325
|
+
sub_module_2.module_eval { remove_method :meth }
|
326
|
+
expect(subject.tracked_methods).to match_array([:meth])
|
327
|
+
|
328
|
+
super_class.class_eval { remove_method :meth }
|
329
|
+
expect(subject.tracked_methods).to match_array([:meth])
|
330
|
+
|
331
|
+
subject.class_eval { remove_method :meth }
|
332
|
+
expect(subject.tracked_methods).to be_empty
|
333
|
+
end
|
334
|
+
|
335
|
+
it "untracks undefined methods" do
|
336
|
+
subject.module_eval { undef_method :meth }
|
337
|
+
expect(subject.tracked_methods).to be_empty
|
338
|
+
|
339
|
+
subject.module_eval { define_method :meth, METHOD_DEFINITION }
|
340
|
+
expect(subject.tracked_methods).to match_array([:meth])
|
341
|
+
end
|
342
|
+
end
|
343
|
+
|
344
|
+
context "partially defined" do
|
345
|
+
context "module" do
|
346
|
+
it "tracks methods" do
|
347
|
+
m = ::Module.new
|
348
|
+
|
349
|
+
expect do
|
350
|
+
m.module_eval do
|
351
|
+
extend MTrack::Mixin
|
352
|
+
track_methods do
|
353
|
+
define_method :meth_1, METHOD_DEFINITION
|
354
|
+
define_method :meth_2, METHOD_DEFINITION
|
355
|
+
raise "Unexpected error"
|
356
|
+
define_method :meth_3, METHOD_DEFINITION
|
357
|
+
define_method :meth_4, METHOD_DEFINITION
|
358
|
+
end
|
359
|
+
end
|
360
|
+
end.to raise_error(RuntimeError, "Unexpected error")
|
361
|
+
|
362
|
+
expect(m.public_instance_methods(false).map(&:to_sym)).to match_array([:meth_1, :meth_2])
|
363
|
+
expect(m.tracked_methods).to match_array([:meth_1, :meth_2])
|
364
|
+
end
|
365
|
+
end
|
366
|
+
|
367
|
+
context "class" do
|
368
|
+
it "tracks methods" do
|
369
|
+
c = ::Class.new
|
370
|
+
|
371
|
+
expect do
|
372
|
+
c.class_eval do
|
373
|
+
extend MTrack::Mixin
|
374
|
+
track_methods do
|
375
|
+
define_method :meth_1, METHOD_DEFINITION
|
376
|
+
define_method :meth_2, METHOD_DEFINITION
|
377
|
+
raise "Unexpected error"
|
378
|
+
define_method :meth_3, METHOD_DEFINITION
|
379
|
+
define_method :meth_4, METHOD_DEFINITION
|
380
|
+
end
|
381
|
+
end
|
382
|
+
end.to raise_error(RuntimeError, "Unexpected error")
|
383
|
+
|
384
|
+
expect(c.public_instance_methods(false).map(&:to_sym)).to match_array([:meth_1, :meth_2])
|
385
|
+
expect(c.tracked_methods).to match_array([:meth_1, :meth_2])
|
386
|
+
end
|
387
|
+
end
|
388
|
+
end
|
389
|
+
|
390
|
+
context "inclusion" do
|
391
|
+
it "adds #{described_class} to submodule" do
|
392
|
+
bm_1 = base_module_1
|
393
|
+
m = ::Module.new.module_eval { include bm_1 }
|
394
|
+
expect(m.tracked_methods).to match_array([:meth])
|
395
|
+
end
|
396
|
+
end
|
397
|
+
|
398
|
+
context "inheritance" do
|
399
|
+
it "adds #{described_class} to subclass" do
|
400
|
+
c = ::Class.new(super_class)
|
401
|
+
expect(c.tracked_methods).to match_array([:meth])
|
402
|
+
end
|
403
|
+
end
|
404
|
+
end
|
405
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mtrack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gabriel de Oliveira
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-03 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: MTrack extends the functionality of modules and classes and enables them
|
14
14
|
to define public methods within groups. These methods can then be queried back even
|
@@ -28,14 +28,12 @@ files:
|
|
28
28
|
- README.md
|
29
29
|
- Rakefile
|
30
30
|
- lib/mtrack.rb
|
31
|
-
- lib/mtrack/
|
32
|
-
- lib/mtrack/module_mixin.rb
|
31
|
+
- lib/mtrack/mixin.rb
|
33
32
|
- lib/mtrack/state.rb
|
34
33
|
- lib/mtrack/state/group.rb
|
35
34
|
- lib/mtrack/version.rb
|
36
35
|
- mtrack.gemspec
|
37
|
-
- spec/lib/mtrack/
|
38
|
-
- spec/lib/mtrack/module_mixin_spec.rb
|
36
|
+
- spec/lib/mtrack/mixin_spec.rb
|
39
37
|
- spec/lib/mtrack/state/group_spec.rb
|
40
38
|
- spec/lib/mtrack/state_spec.rb
|
41
39
|
- spec/lib/mtrack/version_spec.rb
|
@@ -62,13 +60,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
60
|
version: '0'
|
63
61
|
requirements: []
|
64
62
|
rubyforge_project:
|
65
|
-
rubygems_version: 2.4.
|
63
|
+
rubygems_version: 2.4.2
|
66
64
|
signing_key:
|
67
65
|
specification_version: 4
|
68
66
|
summary: Group and track methods on classes and modules.
|
69
67
|
test_files:
|
70
|
-
- spec/lib/mtrack/
|
71
|
-
- spec/lib/mtrack/module_mixin_spec.rb
|
68
|
+
- spec/lib/mtrack/mixin_spec.rb
|
72
69
|
- spec/lib/mtrack/state/group_spec.rb
|
73
70
|
- spec/lib/mtrack/state_spec.rb
|
74
71
|
- spec/lib/mtrack/version_spec.rb
|
data/lib/mtrack/core.rb
DELETED
@@ -1,111 +0,0 @@
|
|
1
|
-
require "mtrack/state"
|
2
|
-
|
3
|
-
module MTrack
|
4
|
-
|
5
|
-
##
|
6
|
-
# Implements the core tracking functionality of the gem by extending those
|
7
|
-
# Modules and Classes that use MTrack::ModuleMixin#track_methods. Additionally
|
8
|
-
# it will extend Modules and Classes that include a Module that is tracking
|
9
|
-
# methods and Classes that inherit from a Class that is tracking methods.
|
10
|
-
module Core
|
11
|
-
class << self
|
12
|
-
private
|
13
|
-
|
14
|
-
##
|
15
|
-
# call-seq:
|
16
|
-
# extended(submodule) => submodule
|
17
|
-
#
|
18
|
-
# Initializes a State variable on the Class or Module that extended Core.
|
19
|
-
#
|
20
|
-
# Returns passed +submodule+.
|
21
|
-
def extended(submodule)
|
22
|
-
submodule.instance_eval { @__mtrack__ ||= State.new }
|
23
|
-
submodule
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
##
|
28
|
-
# call-seq:
|
29
|
-
# tracked_methods(group_name = nil) => set
|
30
|
-
#
|
31
|
-
# Returns a set containing the currently tracked methods for a +group_name+.
|
32
|
-
#
|
33
|
-
# class C
|
34
|
-
# track_methods :my_group do
|
35
|
-
# def method_1; end
|
36
|
-
# def method_2; end
|
37
|
-
# end
|
38
|
-
# end
|
39
|
-
#
|
40
|
-
# C.tracked_methods :my_group #=> #<Set: {:method_1, :method_2}>
|
41
|
-
def tracked_methods(group_name = nil)
|
42
|
-
@__mtrack__.tracked group_name
|
43
|
-
end
|
44
|
-
|
45
|
-
private
|
46
|
-
|
47
|
-
##
|
48
|
-
# call-seq:
|
49
|
-
# included(submodule) => submodule
|
50
|
-
#
|
51
|
-
# Sets this state as a super-state of the +submodule+ (Class or Module) that
|
52
|
-
# has included the current Module.
|
53
|
-
#
|
54
|
-
# Returns passed +submodule+.
|
55
|
-
def included(submodule)
|
56
|
-
state = @__mtrack__
|
57
|
-
submodule.instance_eval do
|
58
|
-
extend Core
|
59
|
-
@__mtrack__.add_super_state state
|
60
|
-
end
|
61
|
-
submodule
|
62
|
-
end
|
63
|
-
|
64
|
-
##
|
65
|
-
# call-seq:
|
66
|
-
# inherited(submodule) => submodule
|
67
|
-
#
|
68
|
-
# Sets this state as a super-state of the +submodule+ (Class) that has
|
69
|
-
# inherited from the current Class.
|
70
|
-
#
|
71
|
-
# Returns passed +submodule+.
|
72
|
-
alias_method :inherited, :included
|
73
|
-
|
74
|
-
##
|
75
|
-
# call-seq:
|
76
|
-
# method_added(name) => name
|
77
|
-
#
|
78
|
-
# Allows method +name+ to be displayed on #tracked_methods once again after
|
79
|
-
# being disabled by a call to #method_undefined.
|
80
|
-
#
|
81
|
-
# Returns passed +name+.
|
82
|
-
def method_added(name)
|
83
|
-
@__mtrack__.delete_undefined name
|
84
|
-
end
|
85
|
-
|
86
|
-
##
|
87
|
-
# call-seq:
|
88
|
-
# method_removed(name) => name
|
89
|
-
#
|
90
|
-
# Stops tracking method +name+ in the current Class or Module.
|
91
|
-
#
|
92
|
-
# Returns passed +name+.
|
93
|
-
def method_removed(name)
|
94
|
-
@__mtrack__.delete_tracked name
|
95
|
-
end
|
96
|
-
|
97
|
-
##
|
98
|
-
# call-seq:
|
99
|
-
# method_undefined(name) => name
|
100
|
-
#
|
101
|
-
# Stops tracking method +name+ in the current Class or Module and prevents
|
102
|
-
# homonymous methods tracked in super-states from being displayed as
|
103
|
-
# #tracked_methods.
|
104
|
-
#
|
105
|
-
# Returns passed +name+.
|
106
|
-
def method_undefined(name)
|
107
|
-
@__mtrack__.delete_tracked name
|
108
|
-
@__mtrack__.add_undefined name
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
data/lib/mtrack/module_mixin.rb
DELETED
@@ -1,90 +0,0 @@
|
|
1
|
-
require "set"
|
2
|
-
|
3
|
-
require "mtrack/core"
|
4
|
-
|
5
|
-
module MTrack
|
6
|
-
|
7
|
-
##
|
8
|
-
# Provides the #track_methods method to all Classes and Modules by being mixed
|
9
|
-
# into the +Module+ class.
|
10
|
-
module ModuleMixin
|
11
|
-
class << self
|
12
|
-
private
|
13
|
-
|
14
|
-
##
|
15
|
-
# call-seq:
|
16
|
-
# newly_defined_methods(mod, old_methods) => set
|
17
|
-
#
|
18
|
-
# Calculates the difference between +mod+'s currently defined public
|
19
|
-
# methods and +old_methods+.
|
20
|
-
#
|
21
|
-
# Returns a set with the result.
|
22
|
-
def newly_defined_methods(mod, old_methods)
|
23
|
-
(mod.public_instance_methods(false) - old_methods).map(&:to_sym).to_set
|
24
|
-
end
|
25
|
-
|
26
|
-
##
|
27
|
-
# call-seq:
|
28
|
-
# save_tracked_methods(mod, group_name, tracked) => nil
|
29
|
-
#
|
30
|
-
# Saves +tracked+ methods for +mod+ under a +group_name+.
|
31
|
-
#
|
32
|
-
# Returns a +nil+ value.
|
33
|
-
def save_tracked_methods(mod, group_name, tracked)
|
34
|
-
mod.instance_variable_get(:@__mtrack__)[group_name].merge_tracked tracked unless tracked.empty?
|
35
|
-
nil
|
36
|
-
end
|
37
|
-
|
38
|
-
##
|
39
|
-
# call-seq:
|
40
|
-
# track_methods_for(mod, group_name) => set
|
41
|
-
# track_methods_for(mod, group_name) {|| ... } => set
|
42
|
-
#
|
43
|
-
# Sets up an MTrack::State instance for +mod+.
|
44
|
-
#
|
45
|
-
# If a block is provided all the methods defined within the block will be
|
46
|
-
# tracked under the +group_name+ parameter.
|
47
|
-
#
|
48
|
-
# Returns a set containing the methods that were defined within the block.
|
49
|
-
def track_methods_for(mod, group_name, &b)
|
50
|
-
old_methods = mod.public_instance_methods false
|
51
|
-
|
52
|
-
begin
|
53
|
-
mod.module_eval &b if block_given?
|
54
|
-
ensure
|
55
|
-
tracked = newly_defined_methods(mod, old_methods)
|
56
|
-
save_tracked_methods(mod, group_name, tracked)
|
57
|
-
end
|
58
|
-
|
59
|
-
tracked
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
private
|
64
|
-
|
65
|
-
##
|
66
|
-
# call-seq:
|
67
|
-
# track_methods(group_name = nil) => set
|
68
|
-
# track_methods(group_name = nil) {|| ... } => set
|
69
|
-
#
|
70
|
-
# Sets up an MTrack::State instance for this Class or Module and extends it
|
71
|
-
# using MTrack::Core.
|
72
|
-
#
|
73
|
-
# If a block is provided all the methods defined within the block will be
|
74
|
-
# tracked under the optional +group_name+ parameter.
|
75
|
-
#
|
76
|
-
# Returns a set containing the methods that were defined within the block.
|
77
|
-
#
|
78
|
-
# class C
|
79
|
-
# track_methods do
|
80
|
-
# def method_1; end
|
81
|
-
# track_methods(:inner_group_1) { def method_2; end }
|
82
|
-
# def method_3; end
|
83
|
-
# end
|
84
|
-
# end #=> #<Set: {:method_1, :method_2, :method_3}>
|
85
|
-
def track_methods(group_name = nil, &b)
|
86
|
-
extend Core
|
87
|
-
ModuleMixin.send(:track_methods_for, self, group_name, &b)
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
@@ -1,377 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
METHOD_DEFINITION = proc {}
|
4
|
-
|
5
|
-
describe MTrack::Core do
|
6
|
-
let(:base_module_1) do
|
7
|
-
Module.new.tap do |m|
|
8
|
-
m.module_eval do
|
9
|
-
define_method :unt_1, METHOD_DEFINITION
|
10
|
-
track_methods { define_method :meth, METHOD_DEFINITION }
|
11
|
-
track_methods :numbers do
|
12
|
-
track_methods(:odd) { define_method :one, METHOD_DEFINITION }
|
13
|
-
track_methods(:even) { define_method :two, METHOD_DEFINITION }
|
14
|
-
end
|
15
|
-
define_method :unt_2, METHOD_DEFINITION
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
let(:base_module_2) do
|
21
|
-
Module.new.tap do |m|
|
22
|
-
m.module_eval do
|
23
|
-
define_method :unt_2, METHOD_DEFINITION
|
24
|
-
track_methods { define_method :meth, METHOD_DEFINITION }
|
25
|
-
track_methods :numbers do
|
26
|
-
track_methods(:even) { define_method :two, METHOD_DEFINITION }
|
27
|
-
track_methods(:odd) { define_method :three, METHOD_DEFINITION }
|
28
|
-
end
|
29
|
-
define_method :unt_3, METHOD_DEFINITION
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
let(:base_module_3) do
|
35
|
-
Module.new.tap do |m|
|
36
|
-
m.module_eval do
|
37
|
-
define_method :unt_3, METHOD_DEFINITION
|
38
|
-
track_methods { define_method :meth, METHOD_DEFINITION }
|
39
|
-
track_methods :numbers do
|
40
|
-
track_methods(:odd) { define_method :three, METHOD_DEFINITION }
|
41
|
-
track_methods(:even) { define_method :four, METHOD_DEFINITION }
|
42
|
-
end
|
43
|
-
define_method :unt_4, METHOD_DEFINITION
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
let(:sub_module_1) do
|
49
|
-
bm_1 = base_module_1
|
50
|
-
bm_2 = base_module_2
|
51
|
-
Module.new.tap do |m|
|
52
|
-
m.module_eval do
|
53
|
-
include bm_1
|
54
|
-
define_method :unt_4, METHOD_DEFINITION
|
55
|
-
track_methods { define_method :meth, METHOD_DEFINITION }
|
56
|
-
track_methods :numbers do
|
57
|
-
track_methods(:even) { define_method :four, METHOD_DEFINITION }
|
58
|
-
track_methods(:odd) { define_method :five, METHOD_DEFINITION }
|
59
|
-
end
|
60
|
-
define_method :unt_5, METHOD_DEFINITION
|
61
|
-
include bm_2
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
let(:sub_module_2) do
|
67
|
-
bm_3 = base_module_3
|
68
|
-
Module.new.tap do |m|
|
69
|
-
m.module_eval do
|
70
|
-
define_method :unt_5, METHOD_DEFINITION
|
71
|
-
track_methods { define_method :meth, METHOD_DEFINITION }
|
72
|
-
include bm_3
|
73
|
-
track_methods :numbers do
|
74
|
-
track_methods(:odd) { define_method :five, METHOD_DEFINITION }
|
75
|
-
track_methods(:even) { define_method :six, METHOD_DEFINITION }
|
76
|
-
end
|
77
|
-
define_method :unt_6, METHOD_DEFINITION
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
let(:super_class) do
|
83
|
-
sm_1 = sub_module_1
|
84
|
-
Class.new.tap do |c|
|
85
|
-
c.class_eval do
|
86
|
-
include sm_1
|
87
|
-
define_method :unt_6, METHOD_DEFINITION
|
88
|
-
track_methods { define_method :meth, METHOD_DEFINITION }
|
89
|
-
track_methods :numbers do
|
90
|
-
track_methods(:even) { define_method :six, METHOD_DEFINITION }
|
91
|
-
track_methods(:odd) { define_method :seven, METHOD_DEFINITION }
|
92
|
-
end
|
93
|
-
define_method :unt_7, METHOD_DEFINITION
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
let(:sub_class) do
|
99
|
-
sm_2 = sub_module_2
|
100
|
-
sc = super_class
|
101
|
-
Class.new(sc).tap do |c|
|
102
|
-
c.class_eval do
|
103
|
-
define_method :unt_7, METHOD_DEFINITION
|
104
|
-
track_methods { define_method :meth, METHOD_DEFINITION }
|
105
|
-
track_methods :numbers do
|
106
|
-
track_methods(:odd) { define_method :seven, METHOD_DEFINITION }
|
107
|
-
track_methods(:even) { define_method :eight, METHOD_DEFINITION }
|
108
|
-
end
|
109
|
-
define_method :unt_8, METHOD_DEFINITION
|
110
|
-
include sm_2
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
context "base module 1" do
|
116
|
-
subject { base_module_1 }
|
117
|
-
|
118
|
-
it "tracks methods" do
|
119
|
-
expect(subject.tracked_methods).to match_array([:meth])
|
120
|
-
expect(subject.tracked_methods(:numbers)).to match_array([:one, :two])
|
121
|
-
expect(subject.tracked_methods(:odd)).to match_array([:one])
|
122
|
-
expect(subject.tracked_methods(:even)).to match_array([:two])
|
123
|
-
end
|
124
|
-
|
125
|
-
it "untracks removed methods" do
|
126
|
-
subject.module_eval { remove_method :meth }
|
127
|
-
expect(subject.tracked_methods).to be_empty
|
128
|
-
end
|
129
|
-
|
130
|
-
it "untracks undefined methods" do
|
131
|
-
subject.module_eval { undef_method :meth }
|
132
|
-
expect(subject.tracked_methods).to be_empty
|
133
|
-
|
134
|
-
subject.module_eval { define_method :meth, METHOD_DEFINITION }
|
135
|
-
expect(subject.tracked_methods).to be_empty
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
context "base module 2" do
|
140
|
-
subject { base_module_2 }
|
141
|
-
|
142
|
-
it "tracks methods" do
|
143
|
-
expect(subject.tracked_methods).to match_array([:meth])
|
144
|
-
expect(subject.tracked_methods(:numbers)).to match_array([:two, :three])
|
145
|
-
expect(subject.tracked_methods(:even)).to match_array([:two])
|
146
|
-
expect(subject.tracked_methods(:odd)).to match_array([:three])
|
147
|
-
end
|
148
|
-
|
149
|
-
it "untracks removed methods" do
|
150
|
-
subject.module_eval { remove_method :meth }
|
151
|
-
expect(subject.tracked_methods).to be_empty
|
152
|
-
end
|
153
|
-
|
154
|
-
it "untracks undefined methods" do
|
155
|
-
subject.module_eval { undef_method :meth }
|
156
|
-
expect(subject.tracked_methods).to be_empty
|
157
|
-
|
158
|
-
subject.module_eval { define_method :meth, METHOD_DEFINITION }
|
159
|
-
expect(subject.tracked_methods).to be_empty
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
context "base module 3" do
|
164
|
-
subject { base_module_3 }
|
165
|
-
|
166
|
-
it "tracks methods" do
|
167
|
-
expect(subject.tracked_methods).to match_array([:meth])
|
168
|
-
expect(subject.tracked_methods(:numbers)).to match_array([:three, :four])
|
169
|
-
expect(subject.tracked_methods(:odd)).to match_array([:three])
|
170
|
-
expect(subject.tracked_methods(:even)).to match_array([:four])
|
171
|
-
end
|
172
|
-
|
173
|
-
it "untracks removed methods" do
|
174
|
-
subject.module_eval { remove_method :meth }
|
175
|
-
expect(subject.tracked_methods).to be_empty
|
176
|
-
end
|
177
|
-
|
178
|
-
it "untracks undefined methods" do
|
179
|
-
subject.module_eval { undef_method :meth }
|
180
|
-
expect(subject.tracked_methods).to be_empty
|
181
|
-
|
182
|
-
subject.module_eval { define_method :meth, METHOD_DEFINITION }
|
183
|
-
expect(subject.tracked_methods).to be_empty
|
184
|
-
end
|
185
|
-
end
|
186
|
-
|
187
|
-
context "sub module 1" do
|
188
|
-
subject { sub_module_1 }
|
189
|
-
|
190
|
-
it "tracks methods" do
|
191
|
-
expect(subject.tracked_methods).to match_array([:meth])
|
192
|
-
expect(subject.tracked_methods(:numbers)).to match_array([:one, :two, :three, :four, :five])
|
193
|
-
expect(subject.tracked_methods(:odd)).to match_array([:one, :three, :five])
|
194
|
-
expect(subject.tracked_methods(:even)).to match_array([:two, :four])
|
195
|
-
end
|
196
|
-
|
197
|
-
it "untracks removed methods" do
|
198
|
-
subject.module_eval { remove_method :meth }
|
199
|
-
expect(subject.tracked_methods).to match_array([:meth])
|
200
|
-
|
201
|
-
base_module_1.module_eval { remove_method :meth }
|
202
|
-
expect(subject.tracked_methods).to match_array([:meth])
|
203
|
-
|
204
|
-
base_module_2.module_eval { remove_method :meth }
|
205
|
-
expect(subject.tracked_methods).to be_empty
|
206
|
-
end
|
207
|
-
|
208
|
-
it "untracks undefined methods" do
|
209
|
-
subject.module_eval { undef_method :meth }
|
210
|
-
expect(subject.tracked_methods).to be_empty
|
211
|
-
|
212
|
-
subject.module_eval { define_method :meth, METHOD_DEFINITION }
|
213
|
-
expect(subject.tracked_methods).to match_array([:meth])
|
214
|
-
end
|
215
|
-
end
|
216
|
-
|
217
|
-
context "sub module 2" do
|
218
|
-
subject { sub_module_2 }
|
219
|
-
|
220
|
-
it "tracks methods" do
|
221
|
-
expect(subject.tracked_methods).to match_array([:meth])
|
222
|
-
expect(subject.tracked_methods(:numbers)).to match_array([:three, :four, :five, :six])
|
223
|
-
expect(subject.tracked_methods(:odd)).to match_array([:three, :five])
|
224
|
-
expect(subject.tracked_methods(:even)).to match_array([:four, :six])
|
225
|
-
end
|
226
|
-
|
227
|
-
it "untracks removed methods" do
|
228
|
-
base_module_3.module_eval { remove_method :meth }
|
229
|
-
expect(subject.tracked_methods).to match_array([:meth])
|
230
|
-
|
231
|
-
subject.module_eval { remove_method :meth }
|
232
|
-
expect(subject.tracked_methods).to be_empty
|
233
|
-
end
|
234
|
-
|
235
|
-
it "untracks undefined methods" do
|
236
|
-
subject.module_eval { undef_method :meth }
|
237
|
-
expect(subject.tracked_methods).to be_empty
|
238
|
-
|
239
|
-
subject.module_eval { define_method :meth, METHOD_DEFINITION }
|
240
|
-
expect(subject.tracked_methods).to match_array([:meth])
|
241
|
-
end
|
242
|
-
end
|
243
|
-
|
244
|
-
context "super class" do
|
245
|
-
subject { super_class }
|
246
|
-
|
247
|
-
it "tracks methods" do
|
248
|
-
expect(subject.tracked_methods).to match_array([:meth])
|
249
|
-
expect(subject.tracked_methods(:numbers)).to match_array([:one, :two, :three, :four, :five, :six, :seven])
|
250
|
-
expect(subject.tracked_methods(:odd)).to match_array([:one, :three, :five, :seven])
|
251
|
-
expect(subject.tracked_methods(:even)).to match_array([:two, :four, :six])
|
252
|
-
end
|
253
|
-
|
254
|
-
it "untracks removed methods" do
|
255
|
-
subject.class_eval { remove_method :meth }
|
256
|
-
expect(subject.tracked_methods).to match_array([:meth])
|
257
|
-
|
258
|
-
sub_module_1.module_eval { remove_method :meth }
|
259
|
-
expect(subject.tracked_methods).to match_array([:meth])
|
260
|
-
|
261
|
-
base_module_2.module_eval { remove_method :meth }
|
262
|
-
expect(subject.tracked_methods).to match_array([:meth])
|
263
|
-
|
264
|
-
base_module_1.module_eval { remove_method :meth }
|
265
|
-
expect(subject.tracked_methods).to be_empty
|
266
|
-
end
|
267
|
-
|
268
|
-
it "untracks undefined methods" do
|
269
|
-
subject.module_eval { undef_method :meth }
|
270
|
-
expect(subject.tracked_methods).to be_empty
|
271
|
-
|
272
|
-
subject.module_eval { define_method :meth, METHOD_DEFINITION }
|
273
|
-
expect(subject.tracked_methods).to match_array([:meth])
|
274
|
-
end
|
275
|
-
end
|
276
|
-
|
277
|
-
context "sub class" do
|
278
|
-
subject { sub_class }
|
279
|
-
|
280
|
-
it "tracks methods" do
|
281
|
-
expect(subject.tracked_methods).to match_array([:meth])
|
282
|
-
expect(subject.tracked_methods(:numbers)).to match_array([:one, :two, :three, :four, :five, :six, :seven, :eight])
|
283
|
-
expect(subject.tracked_methods(:odd)).to match_array([:one, :three, :five, :seven])
|
284
|
-
expect(subject.tracked_methods(:even)).to match_array([:two, :four, :six, :eight])
|
285
|
-
end
|
286
|
-
|
287
|
-
it "untracks removed methods" do
|
288
|
-
base_module_1.module_eval { remove_method :meth }
|
289
|
-
expect(subject.tracked_methods).to match_array([:meth])
|
290
|
-
|
291
|
-
base_module_2.module_eval { remove_method :meth }
|
292
|
-
expect(subject.tracked_methods).to match_array([:meth])
|
293
|
-
|
294
|
-
base_module_3.module_eval { remove_method :meth }
|
295
|
-
expect(subject.tracked_methods).to match_array([:meth])
|
296
|
-
|
297
|
-
sub_module_1.module_eval { remove_method :meth }
|
298
|
-
expect(subject.tracked_methods).to match_array([:meth])
|
299
|
-
|
300
|
-
sub_module_2.module_eval { remove_method :meth }
|
301
|
-
expect(subject.tracked_methods).to match_array([:meth])
|
302
|
-
|
303
|
-
super_class.class_eval { remove_method :meth }
|
304
|
-
expect(subject.tracked_methods).to match_array([:meth])
|
305
|
-
|
306
|
-
subject.class_eval { remove_method :meth }
|
307
|
-
expect(subject.tracked_methods).to be_empty
|
308
|
-
end
|
309
|
-
|
310
|
-
it "untracks undefined methods" do
|
311
|
-
subject.module_eval { undef_method :meth }
|
312
|
-
expect(subject.tracked_methods).to be_empty
|
313
|
-
|
314
|
-
subject.module_eval { define_method :meth, METHOD_DEFINITION }
|
315
|
-
expect(subject.tracked_methods).to match_array([:meth])
|
316
|
-
end
|
317
|
-
end
|
318
|
-
|
319
|
-
context "partially defined" do
|
320
|
-
context "module" do
|
321
|
-
it "tracks methods" do
|
322
|
-
m = ::Module.new
|
323
|
-
|
324
|
-
expect do
|
325
|
-
m.module_eval do
|
326
|
-
track_methods do
|
327
|
-
define_method :meth_1, METHOD_DEFINITION
|
328
|
-
define_method :meth_2, METHOD_DEFINITION
|
329
|
-
raise "Unexpected error"
|
330
|
-
define_method :meth_3, METHOD_DEFINITION
|
331
|
-
define_method :meth_4, METHOD_DEFINITION
|
332
|
-
end
|
333
|
-
end
|
334
|
-
end.to raise_error(RuntimeError, "Unexpected error")
|
335
|
-
|
336
|
-
expect(m.public_instance_methods(false).map(&:to_sym)).to match_array([:meth_1, :meth_2])
|
337
|
-
expect(m.tracked_methods).to match_array([:meth_1, :meth_2])
|
338
|
-
end
|
339
|
-
end
|
340
|
-
|
341
|
-
context "class" do
|
342
|
-
it "tracks methods" do
|
343
|
-
c = ::Class.new
|
344
|
-
|
345
|
-
expect do
|
346
|
-
c.class_eval do
|
347
|
-
track_methods do
|
348
|
-
define_method :meth_1, METHOD_DEFINITION
|
349
|
-
define_method :meth_2, METHOD_DEFINITION
|
350
|
-
raise "Unexpected error"
|
351
|
-
define_method :meth_3, METHOD_DEFINITION
|
352
|
-
define_method :meth_4, METHOD_DEFINITION
|
353
|
-
end
|
354
|
-
end
|
355
|
-
end.to raise_error(RuntimeError, "Unexpected error")
|
356
|
-
|
357
|
-
expect(c.public_instance_methods(false).map(&:to_sym)).to match_array([:meth_1, :meth_2])
|
358
|
-
expect(c.tracked_methods).to match_array([:meth_1, :meth_2])
|
359
|
-
end
|
360
|
-
end
|
361
|
-
end
|
362
|
-
|
363
|
-
context "inclusion" do
|
364
|
-
it "adds #{described_class} to submodule" do
|
365
|
-
bm_1 = base_module_1
|
366
|
-
m = ::Module.new.module_eval { include bm_1 }
|
367
|
-
expect(m.tracked_methods).to match_array([:meth])
|
368
|
-
end
|
369
|
-
end
|
370
|
-
|
371
|
-
context "inheritance" do
|
372
|
-
it "adds #{described_class} to subclass" do
|
373
|
-
c = ::Class.new(super_class)
|
374
|
-
expect(c.tracked_methods).to match_array([:meth])
|
375
|
-
end
|
376
|
-
end
|
377
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe MTrack::ModuleMixin do
|
4
|
-
describe "#track_methods" do
|
5
|
-
context "no block given" do
|
6
|
-
it "returns an empty set" do
|
7
|
-
ret_val = ::Module.new.module_eval { track_methods :group }
|
8
|
-
expect(ret_val).to be_a(Set)
|
9
|
-
expect(ret_val).to be_empty
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|