object_tracker 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 54a446b93232eb855a137f08a67a61a5830ba71d
4
- data.tar.gz: c0f8898440e271bd328e433f947cb73842169c92
3
+ metadata.gz: b64ab16d9d0b0d15225d24cf7121afb3061e88e8
4
+ data.tar.gz: 740e4ee7f4eacbbeba0c36211f4c3e032b505d1f
5
5
  SHA512:
6
- metadata.gz: 74a1ff389ff3c7235a888c42b34b0ac6b224645ddb012920fa1f45c32354e27fcd4ff151dd70480c6f33df9a8d8d8d2648a9df44aece908976d583c44d0561f1
7
- data.tar.gz: 52351e4b114d66cf31c906ac26d4791a45e95987a4034397dde712f57d74e6f51e18601a6e878444c1a1e1e7b4ef9a253d4d0b12fc6055fda0719d952eb8b5b8
6
+ metadata.gz: c1efef10178e161cb122b2c9a21dd4cf283251551100679c3017553dfefd66387558c3e54a3ff9d8ad64c32a9ffe52ba94a4bf2f1ec5c07766e9f1d4d894d9f7
7
+ data.tar.gz: 94ece458adbd7320c1d72b2c1f161d149179055dae3f8780baefe15262181b355c1afefb71b901d81cdc22df68510e70800b405961bea26601df63cf4350e17b
data/README.md CHANGED
@@ -31,17 +31,41 @@ end
31
31
 
32
32
  Dog.track_all!
33
33
  Dog
34
- #=> inspect called from (irb):27
34
+ * called "inspect" [RUBY CORE]
35
+ #=> Dog
35
36
  Dog.new.fetch('Hudog')
36
- #=> called new from (irb):27
37
- #=> called fetch with Hudog from (irb):27
37
+ * called "new" [RUBY CORE]
38
+ * called "fetch" with Hudog [(irb):4]
38
39
  #=> Fetch the ball Hudog!
39
40
  ```
40
41
 
41
42
  Or just track single method:
42
43
 
43
44
  ```ruby
44
- Dog.track! :fetch
45
+ Dog.track :name
46
+ ```
47
+
48
+ Or track methods for a single object:
49
+
50
+ ```ruby
51
+ class Go
52
+ def fish
53
+ 'We go!'
54
+ end
55
+ end
56
+
57
+ a = Go.new
58
+ a.extend ObjectTracker
59
+ a.track_all!
60
+ ```
61
+
62
+ ## Troubleshooting
63
+
64
+ Having problems? Maybe a specific method is throwing some obscure error? Try ignoring that method, so we can get back on track!
65
+
66
+ ```ruby
67
+ Dog.track_not :bad_doggy
68
+ Dog.track_all! #=> will exclude tracking for :bad_doggy
45
69
  ```
46
70
 
47
71
  ## Contributing
@@ -4,33 +4,57 @@ fail "ObjectTracker #{ObjectTracker::VERSION} only supports Ruby 2+" if RUBY_VER
4
4
 
5
5
  module ObjectTracker
6
6
  def track(*args)
7
- @__tracking ||= []
8
- args.each { |arg| @__tracking << arg unless tracking?(arg) }
7
+ args.each do |method_name|
8
+ next if tracking?(method_name) || track_reserved_methods.include?(method_name)
9
+ if respond_to?(method_name)
10
+ track!(method_name => track_with_source(self, method_name))
11
+ elsif respond_to?(:allocate)
12
+ inst = allocate
13
+ if inst.respond_to?(method_name)
14
+ track!(method_name => track_with_source(inst, method_name))
15
+ else
16
+ fail UntrackableMethod, method_name
17
+ end
18
+ else
19
+ fail UntrackableMethod, method_name
20
+ end
21
+ end
22
+ nil
9
23
  end
10
24
 
11
- def track!(*args)
12
- track(*args)
13
- start_tracking!
25
+ def tracking?(method_name)
26
+ tracking.keys.include?(method_name.to_sym)
14
27
  end
15
28
 
16
- def track_all!
17
- track!(*(methods | instance_methods))
29
+ def track_not(*args)
30
+ args.each do |method_name|
31
+ track_reserved_methods << method_name unless track_reserved_methods.include?(method_name)
32
+ end
33
+ nil
18
34
  end
19
35
 
20
- def tracking?(method_name)
21
- @__tracking.include?(method_name)
36
+ def track_all!
37
+ track_methods_for(self)
38
+ track_methods_for(allocate) if respond_to?(:allocate)
39
+ track!
22
40
  end
23
41
 
24
- def start_tracking!
42
+ #
43
+ # PRIVATE
44
+ #
45
+
46
+ def track!(method_names = nil)
25
47
  mod = Module.new
26
- @__tracking.each do |method_name|
48
+ Array(method_names || tracking).each do |method_name, source_def|
27
49
  mod.module_eval <<-RUBY, __FILE__, __LINE__
28
50
  def #{method_name}(*args, &block)
29
- msg = "called #{method_name} "
51
+ msg = %Q( * called "#{method_name}" )
30
52
  msg << "with " << args.join(', ') << " " if args.any?
31
- msg << "from #{__FILE__}:#{__LINE__}"
53
+ msg << "[#{source_def}]"
32
54
  puts msg
33
55
  super
56
+ rescue NoMethodError => e
57
+ raise e if e.message !~ /no superclass/
34
58
  end
35
59
  RUBY
36
60
  end
@@ -41,6 +65,65 @@ module ObjectTracker
41
65
  end
42
66
  RUBY
43
67
 
44
- prepend(mod)
68
+ # Handle both instance and class level extension
69
+ if respond_to?(:prepend)
70
+ prepend(mod)
71
+ else
72
+ extend(mod)
73
+ end
74
+ end
75
+
76
+ def track_methods_for(obj)
77
+ (obj.methods - track_reserved_methods).each do |method_name|
78
+ track_with_source(obj, method_name)
79
+ end
80
+ end
81
+
82
+ def track_with_source(obj, method_name)
83
+ source = obj.method(method_name).source_location || ['RUBY CORE']
84
+ tracking[method_name.to_sym] = source.join(':').split('/').last(5).join('/')
85
+ end
86
+
87
+ def tracking
88
+ @__tracking ||= {}
89
+ end
90
+
91
+ def track_reserved_methods
92
+ @__reserved_methods ||= [
93
+ :!,
94
+ :!=,
95
+ :!~,
96
+ :<,
97
+ :<=,
98
+ :<=>,
99
+ :==,
100
+ :===,
101
+ :=~,
102
+ :>,
103
+ :>=,
104
+ :[],
105
+ :[]=,
106
+ :__id__,
107
+ :__send__,
108
+ :`,
109
+ :public_send,
110
+ :send,
111
+ :class,
112
+ :track,
113
+ :tracking?,
114
+ :track_not,
115
+ :track_all!,
116
+ :track!,
117
+ :track_methods_for,
118
+ :track_with_source,
119
+ :tracking,
120
+ :track_reserved_methods
121
+ ]
122
+ end
123
+
124
+ class UntrackableMethod < StandardError
125
+ def initialize(method_name)
126
+ super "Can't track :#{method_name} because it's not defined on this class or it's instance"
127
+ end
45
128
  end
46
- end
129
+ end
@@ -1,3 +1,3 @@
1
1
  module ObjectTracker
2
- VERSION = "0.0.2"
2
+ VERSION = '1.0.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: object_tracker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Buckley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-05 00:00:00.000000000 Z
11
+ date: 2015-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -74,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
74
  version: '0'
75
75
  requirements: []
76
76
  rubyforge_project:
77
- rubygems_version: 2.4.3
77
+ rubygems_version: 2.2.2
78
78
  signing_key:
79
79
  specification_version: 4
80
80
  summary: Track method calls to any object.