mtrack 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mtrack/core.rb +12 -31
- data/lib/mtrack/module_mixin.rb +51 -6
- data/lib/mtrack/state.rb +4 -8
- data/lib/mtrack/version.rb +1 -1
- data/spec/lib/mtrack/state_spec.rb +7 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a37d3a4f84c29d88e6e624fe0e011b5b2c22355e
|
4
|
+
data.tar.gz: 46ac0e28e8b2098ba8c2fe4407360364f70891fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5fd6379634adcb84748ae9f1be5d6a4ca108ba9f798677f8e86da330466ecaab6990f6ad3c2032bd20921a90b8146f2857eaabc17b3702df72cdaf49a35fa7a9
|
7
|
+
data.tar.gz: d831966d9765c6b61c50e9faece76b8b1e02ee16e21f3890a9a200a0f91079e16533239c8a29471671735e16fbada3ddf97965774dbbcf479ae22b1852d4886f
|
data/lib/mtrack/core.rb
CHANGED
@@ -8,6 +8,15 @@ module MTrack
|
|
8
8
|
# it will extend Modules and Classes that include a Module that is tracking
|
9
9
|
# methods and Classes that inherit from a Class that is tracking methods.
|
10
10
|
module Core
|
11
|
+
class << self
|
12
|
+
private
|
13
|
+
|
14
|
+
##
|
15
|
+
# Initializes a State variable on the Class or Module that extended Core.
|
16
|
+
def extended(submodule)
|
17
|
+
submodule.instance_eval { @__mtrack__ ||= State.new }
|
18
|
+
end
|
19
|
+
end
|
11
20
|
|
12
21
|
##
|
13
22
|
# call-seq:
|
@@ -35,22 +44,15 @@ module MTrack
|
|
35
44
|
def included(submodule)
|
36
45
|
state = @__mtrack__
|
37
46
|
submodule.instance_eval do
|
38
|
-
|
39
|
-
|
40
|
-
extend Core
|
41
|
-
else
|
42
|
-
@__mtrack__.add_super_state state
|
43
|
-
end
|
47
|
+
extend Core
|
48
|
+
@__mtrack__.add_super_state state
|
44
49
|
end
|
45
50
|
end
|
46
51
|
|
47
52
|
##
|
48
53
|
# Sets this state as a super-state of the Class that has inherited from the
|
49
54
|
# current Class.
|
50
|
-
|
51
|
-
state = @__mtrack__
|
52
|
-
subclass.instance_eval { @__mtrack__ = State.new(state) }
|
53
|
-
end
|
55
|
+
alias_method :inherited, :included
|
54
56
|
|
55
57
|
##
|
56
58
|
# Allows method +name+ to be displayed on #tracked_methods once again after
|
@@ -73,26 +75,5 @@ module MTrack
|
|
73
75
|
@__mtrack__.delete_tracked name
|
74
76
|
@__mtrack__.add_undefined name
|
75
77
|
end
|
76
|
-
|
77
|
-
##
|
78
|
-
# call-seq:
|
79
|
-
# track_methods_with_block(group_name) {|| ... } => set
|
80
|
-
#
|
81
|
-
# All the methods defined within the block will be tracked under the
|
82
|
-
# +group_name+ parameter.
|
83
|
-
#
|
84
|
-
# Returns a set containing the methods that were defined within the block.
|
85
|
-
def track_methods_with_block(group_name, &b)
|
86
|
-
old_methods = public_instance_methods(false)
|
87
|
-
|
88
|
-
begin
|
89
|
-
module_eval(&b)
|
90
|
-
ensure
|
91
|
-
tracked = (public_instance_methods(false) - old_methods).map(&:to_sym).to_set
|
92
|
-
@__mtrack__[group_name].merge_tracked tracked unless tracked.empty?
|
93
|
-
end
|
94
|
-
|
95
|
-
tracked
|
96
|
-
end
|
97
78
|
end
|
98
79
|
end
|
data/lib/mtrack/module_mixin.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require "set"
|
2
2
|
|
3
3
|
require "mtrack/core"
|
4
|
-
require "mtrack/state"
|
5
4
|
|
6
5
|
module MTrack
|
7
6
|
|
@@ -9,6 +8,55 @@ module MTrack
|
|
9
8
|
# Provides the #track_methods method to all Classes and Modules by being mixed
|
10
9
|
# into the +Module+ class.
|
11
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)
|
29
|
+
#
|
30
|
+
# Saves +tracked+ methods for +mod+ under a +group_name+.
|
31
|
+
def save_tracked_methods(mod, group_name, tracked)
|
32
|
+
mod.instance_variable_get(:@__mtrack__)[group_name].merge_tracked tracked unless tracked.empty?
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# call-seq:
|
37
|
+
# track_methods_for(mod, group_name) => set
|
38
|
+
# track_methods_for(mod, group_name) {|| ... } => set
|
39
|
+
#
|
40
|
+
# Sets up an MTrack::State instance for +mod+.
|
41
|
+
#
|
42
|
+
# If a block is provided all the methods defined within the block will be
|
43
|
+
# tracked under the +group_name+ parameter.
|
44
|
+
#
|
45
|
+
# Returns a set containing the methods that were defined within the block.
|
46
|
+
def track_methods_for(mod, group_name, &b)
|
47
|
+
old_methods = mod.public_instance_methods false
|
48
|
+
|
49
|
+
begin
|
50
|
+
mod.module_eval &b if block_given?
|
51
|
+
ensure
|
52
|
+
tracked = newly_defined_methods(mod, old_methods)
|
53
|
+
save_tracked_methods(mod, group_name, tracked)
|
54
|
+
end
|
55
|
+
|
56
|
+
tracked
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
12
60
|
private
|
13
61
|
|
14
62
|
##
|
@@ -22,8 +70,7 @@ module MTrack
|
|
22
70
|
# If a block is provided all the methods defined within the block will be
|
23
71
|
# tracked under the optional +group_name+ parameter.
|
24
72
|
#
|
25
|
-
# Returns a set containing the methods that were defined within the block
|
26
|
-
# an empty set otherwise.
|
73
|
+
# Returns a set containing the methods that were defined within the block.
|
27
74
|
#
|
28
75
|
# class C
|
29
76
|
# track_methods do
|
@@ -33,10 +80,8 @@ module MTrack
|
|
33
80
|
# end
|
34
81
|
# end #=> #<Set: {:method_1, :method_2, :method_3}>
|
35
82
|
def track_methods(group_name = nil, &b)
|
36
|
-
@__mtrack__ ||= State.new
|
37
83
|
extend Core
|
38
|
-
|
39
|
-
Set.new
|
84
|
+
ModuleMixin.send(:track_methods_for, self, group_name, &b)
|
40
85
|
end
|
41
86
|
end
|
42
87
|
end
|
data/lib/mtrack/state.rb
CHANGED
@@ -10,16 +10,12 @@ module MTrack
|
|
10
10
|
|
11
11
|
##
|
12
12
|
# call-seq:
|
13
|
-
# new(
|
13
|
+
# new() => new_state
|
14
14
|
#
|
15
|
-
# Creates a new State instance.
|
16
|
-
|
17
|
-
#
|
18
|
-
# super_state = MTrack::State.new
|
19
|
-
# sub_state = MTrack::State.new(super_state)
|
20
|
-
def initialize(super_state = nil)
|
15
|
+
# Creates a new State instance.
|
16
|
+
def initialize()
|
21
17
|
self.groups = {}
|
22
|
-
self.super_states =
|
18
|
+
self.super_states = Set.new
|
23
19
|
self.undefined = Set.new
|
24
20
|
end
|
25
21
|
|
data/lib/mtrack/version.rb
CHANGED
@@ -50,7 +50,7 @@ describe MTrack::State do
|
|
50
50
|
end
|
51
51
|
|
52
52
|
context "with super state" do
|
53
|
-
subject { described_class.new sample_state }
|
53
|
+
subject { described_class.new.tap {|s| s.add_super_state sample_state } }
|
54
54
|
|
55
55
|
it "has super state's tracked methods" do
|
56
56
|
expect(sample_state.tracked(:con_1)).to match_array(sample_group[:con_1][:tracked])
|
@@ -61,7 +61,12 @@ describe MTrack::State do
|
|
61
61
|
end
|
62
62
|
|
63
63
|
context "with multiple super states" do
|
64
|
-
subject
|
64
|
+
subject do
|
65
|
+
described_class.new.tap do |s|
|
66
|
+
s.add_super_state sample_state
|
67
|
+
s.add_super_state sample_state_2
|
68
|
+
end
|
69
|
+
end
|
65
70
|
|
66
71
|
it "merges tracked methods from all super states" do
|
67
72
|
expect(subject.tracked(:con_1)).to match_array(sample_group[:con_1][:tracked])
|
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: 0.0.5
|
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-08-
|
11
|
+
date: 2014-08-26 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
|