source_route 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  module SourceRoute
2
2
 
3
- class Proxy # todo Rename it to Proxy
3
+ class Proxy
4
4
  include Singleton
5
5
 
6
6
  attr_accessor :config, :tp, :result_builder
@@ -19,17 +19,17 @@ module SourceRoute
19
19
  end
20
20
 
21
21
  def trace
22
- tp_filter = TpFilter.new(config)
22
+ trace_filter = TraceFilter.new(config)
23
23
  track = TracePoint.new(*config.event) do |tp|
24
- next if tp_filter.block_it?(tp)
24
+ next if trace_filter.block_it?(tp)
25
25
  @result_builder.output(tp)
26
26
  end
27
27
  track.enable
28
28
  self.tp = track
29
29
  end
30
30
 
31
- def tp_result_chain
32
- result_builder.tp_result_chain
31
+ def trace_chain
32
+ result_builder.trace_chain
33
33
  end
34
34
  end # END Proxy
35
35
 
@@ -3,18 +3,17 @@ module Rack
3
3
 
4
4
  def initialize(app, opts={})
5
5
  @opts = opts
6
- if @opts.present?
7
- ::SourceRoute.enable do
8
- # defined_class 'Cors'
9
- # method_id opts[:method_id] # will crashed
10
- method_id 'resource_for_path'
11
- full_feature
12
- end
13
- end
14
6
  @app = app
15
7
  end
16
8
 
17
9
  def call(env)
10
+ ::SourceRoute.enable do
11
+ # defined_class 'Cors'
12
+ # method_id opts[:method_id] # will crashed
13
+ method_id 'render'
14
+ full_feature
15
+ end
16
+
18
17
  @app.call(env)
19
18
  end
20
19
 
@@ -123,6 +123,8 @@ module SourceRoute
123
123
  def get_local_or_params_var
124
124
  local_var_hash = {}
125
125
  # Warn: @tp_ins.binding.eval('local_variables') =! @tp_ins.binding.send('local_variables')
126
+ # Params var is complex, see https://alexcastano.com/everything-about-ruby-splats/ for more details
127
+ # so actually here just get local variables which includes params
126
128
  @tp_ins.binding.eval('local_variables').each do |v|
127
129
  # I need comment out why i need source_route_display
128
130
  # must be some strange variables require it
@@ -1,6 +1,6 @@
1
1
  module SourceRoute
2
2
  # delegate to Array
3
- class TpResultChain
3
+ class TraceChain
4
4
  attr_reader :chain
5
5
 
6
6
  extend Forwardable
@@ -69,5 +69,5 @@ module SourceRoute
69
69
  end
70
70
  end
71
71
 
72
- end # END TpResultChain
72
+ end # END TraceChain
73
73
  end
@@ -1,6 +1,6 @@
1
1
  module SourceRoute
2
2
 
3
- class TpFilter
3
+ class TraceFilter
4
4
  attr_accessor :cond
5
5
  def initialize(condition)
6
6
  @cond = condition
@@ -8,9 +8,18 @@ module SourceRoute
8
8
 
9
9
  # to improve performance, we didnt assign tp as instance variable
10
10
  def block_it?(tp)
11
- return true if negative_check(tp)
12
- return false if positives_check(tp)
13
- true
11
+ if @cond.track_params
12
+ return true if negative_check(tp)
13
+ if positives_check(tp)
14
+ return !tp.binding.eval('local_variables').any? do |v|
15
+ tp.binding.local_variable_get(v).object_id == @cond.track_params
16
+ end
17
+ end
18
+ else
19
+ return true if negative_check(tp)
20
+ return false if positives_check(tp)
21
+ end
22
+ true # default is blocked the tp
14
23
  end
15
24
 
16
25
  def negative_check(tp)
@@ -1,3 +1,3 @@
1
1
  module SourceRoute
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -25,7 +25,6 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "bundler"
26
26
  spec.add_development_dependency "rake"
27
27
  spec.add_development_dependency 'minitest'
28
- spec.add_development_dependency 'pry'
29
28
  spec.add_development_dependency 'pry-byebug'
30
29
  spec.add_development_dependency 'guard'
31
30
  spec.add_development_dependency 'guard-shell'
@@ -48,6 +48,14 @@ class SampleApp
48
48
  cool_in_foo = 'foo data'
49
49
  @cool_ins_after = 'will shown in result?'
50
50
  end
51
+
52
+ def reverse_hash(h)
53
+ ret = {}
54
+ h.each do |k, v|
55
+ ret[v] = k
56
+ end
57
+ ret
58
+ end
51
59
  end
52
60
 
53
61
  class RetObj
@@ -59,4 +67,5 @@ class SampleApp
59
67
  {temp: 'testing'}
60
68
  end
61
69
  end
70
+
62
71
  end
@@ -3,11 +3,11 @@ require 'test_helper'
3
3
  module SourceRoute
4
4
  class ConfigTest < Minitest::Test
5
5
 
6
- def test_tp_filter_methods
7
- assert TP_FILTER_METHODS.include?(:defined_class_not)
8
- assert TP_FILTER_METHODS.include?(:method_id_not)
9
- assert TP_FILTER_METHODS.include?(:path)
10
- assert TP_FILTER_METHODS.frozen?
6
+ def test_trace_filter_methods
7
+ assert TRACE_FILTER_METHODS.include?(:defined_class_not)
8
+ assert TRACE_FILTER_METHODS.include?(:method_id_not)
9
+ assert TRACE_FILTER_METHODS.include?(:path)
10
+ assert TRACE_FILTER_METHODS.frozen?
11
11
  end
12
12
 
13
13
  def test_block_parser
@@ -56,11 +56,13 @@ module SourceRoute
56
56
  def test_full_feature_of_params_parser
57
57
  params = {output_format: 'html', event: :c_call,
58
58
  defined_class: 'ActiveRecord::Base',
59
- method_id_not: ['initialize', 'nonsense']
59
+ method_id_not: ['initialize', 'nonsense'],
60
+ full_feature: true
60
61
  }
61
62
  config = ParamsConfigParser.run(params)
62
63
  assert_equal :html, config.output_format
63
- assert_equal [:c_call], config.event
64
+ assert_equal [:c_call, :call, :return], config.event
65
+ assert config.full_feature
64
66
  assert_equal 'ActiveRecord::Base', config.positives[:defined_class]
65
67
  assert_equal 'initialize|nonsense', config.negatives[:method_id]
66
68
  end
@@ -7,7 +7,7 @@ module SourceRoute
7
7
 
8
8
  FakeTp = Struct.new(:method_id, :defined_class, :lineno)
9
9
 
10
- class TpFilterTest < Minitest::Test
10
+ class TraceFilterTest < Minitest::Test
11
11
 
12
12
  def setup
13
13
  @devise_tp = FakeTp.new(:auth, Devise, 5)
@@ -20,24 +20,24 @@ module SourceRoute
20
20
  def test_filter_method_not_auth
21
21
  cond = Config.new
22
22
  cond.negatives[:method_id] = 'auth'
23
- @tp_filter = TpFilter.new(cond)
24
- filtered = @tps.reject { |tp| @tp_filter.block_it?(tp) }
23
+ @trace_filter = TraceFilter.new(cond)
24
+ filtered = @tps.reject { |tp| @trace_filter.block_it?(tp) }
25
25
  assert_equal [@user_tp], filtered
26
26
  end
27
27
 
28
28
  def test_filter_class_is_admin
29
29
  cond = Config.new
30
30
  cond.positives[:defined_class] = 'Admin'
31
- @tp_filter = TpFilter.new(cond)
32
- filtered = @tps.reject { |tp| @tp_filter.block_it?(tp) }
31
+ @trace_filter = TraceFilter.new(cond)
32
+ filtered = @tps.reject { |tp| @trace_filter.block_it?(tp) }
33
33
  assert_equal [], filtered
34
34
  end
35
35
 
36
36
  def test_filter_method_is_auth
37
37
  cond = Config.new
38
38
  cond.positives[:method_id] = 'auth'
39
- @tp_filter = TpFilter.new(cond)
40
- filtered = @tps.reject { |tp| @tp_filter.block_it?(tp) }
39
+ @trace_filter = TraceFilter.new(cond)
40
+ filtered = @tps.reject { |tp| @trace_filter.block_it?(tp) }
41
41
  assert_equal [@devise_tp, @warden_tp], filtered
42
42
  end
43
43
 
@@ -45,16 +45,16 @@ module SourceRoute
45
45
  cond = Config.new
46
46
  cond.positives[:defined_class] = 'Devise'
47
47
  cond.positives[:method_id] = 'new'
48
- @tp_filter = TpFilter.new(cond)
49
- filtered = @tps.reject { |tp| @tp_filter.block_it?(tp) }
48
+ @trace_filter = TraceFilter.new(cond)
49
+ filtered = @tps.reject { |tp| @trace_filter.block_it?(tp) }
50
50
  assert_equal [@devise_tp, @user_tp], filtered
51
51
  end
52
52
 
53
53
  def test_filter_class_is_devise_or_warden
54
54
  cond = Config.new
55
55
  cond.positives[:defined_class] = 'Warden|User'
56
- @tp_filter = TpFilter.new(cond)
57
- filtered = @tps.reject { |tp| @tp_filter.block_it?(tp) }
56
+ @trace_filter = TraceFilter.new(cond)
57
+ filtered = @tps.reject { |tp| @trace_filter.block_it?(tp) }
58
58
  assert_equal [@warden_tp, @user_tp], filtered
59
59
  end
60
60
  end
@@ -36,14 +36,14 @@ class SourceRouteTest < Minitest::Test
36
36
  end
37
37
  SampleApp.new.nonsense
38
38
 
39
- assert_includes @proxy.tp_result_chain.first.path, 'test'
39
+ assert_includes @proxy.trace_chain.first.path, 'test'
40
40
  end
41
41
 
42
42
  def test_match_class_name_by_first_parameter
43
43
  @source_route = SourceRoute.enable 'SampleApp'
44
44
  SampleApp.new.nonsense
45
45
 
46
- assert @proxy.tp_result_chain.size > 0
46
+ assert @proxy.trace_chain.size > 0
47
47
  end
48
48
 
49
49
  def test_not_match
@@ -52,7 +52,7 @@ class SourceRouteTest < Minitest::Test
52
52
  method_id_not 'nonsense'
53
53
  end
54
54
  SampleApp.new.nonsense
55
- refute_includes @proxy.tp_result_chain.map(&:method_id).flatten, 'nonsense'
55
+ refute_includes @proxy.trace_chain.map(&:method_id).flatten, 'nonsense'
56
56
  end
57
57
 
58
58
  def test_match_multiple_class_name
@@ -61,8 +61,8 @@ class SourceRouteTest < Minitest::Test
61
61
  end
62
62
 
63
63
  SampleApp.new.nonsense
64
- assert @proxy.tp_result_chain.size > 0
65
- assert_equal SampleApp, @proxy.tp_result_chain.last.defined_class
64
+ assert @proxy.trace_chain.size > 0
65
+ assert_equal SampleApp, @proxy.trace_chain.last.defined_class
66
66
  end
67
67
 
68
68
  def test_source_route_with_one_parameter
@@ -71,19 +71,19 @@ class SourceRouteTest < Minitest::Test
71
71
  end
72
72
  SampleApp.new.nonsense
73
73
 
74
- ret_tp = @proxy.tp_result_chain.last
74
+ ret_tp = @proxy.trace_chain.last
75
75
  assert_equal SampleApp, ret_tp.defined_class
76
76
  end
77
77
 
78
78
  def test_proxy_reset
79
79
  SourceRoute.enable 'nonsense'
80
80
  SampleApp.new.nonsense
81
- assert_equal 1, @proxy.tp_result_chain.size
81
+ assert_equal 1, @proxy.trace_chain.size
82
82
 
83
83
  SourceRoute.reset
84
84
  SampleApp.new.nonsense
85
85
 
86
- assert_equal 0, @proxy.tp_result_chain.size
86
+ assert_equal 0, @proxy.trace_chain.size
87
87
  end
88
88
 
89
89
  def test_source_route_with_block
@@ -96,7 +96,7 @@ class SourceRouteTest < Minitest::Test
96
96
  end
97
97
  SampleApp.new.nonsense
98
98
 
99
- assert_equal 0, @proxy.tp_result_chain.size
99
+ assert_equal 0, @proxy.trace_chain.size
100
100
  assert_equal 1, paths.size
101
101
  assert_includes paths.first, 'sample_app'
102
102
  end
@@ -104,14 +104,14 @@ class SourceRouteTest < Minitest::Test
104
104
  def test_trace_with_c_call
105
105
  SourceRoute.trace(event: :c_call) { 'abc'.upcase }
106
106
 
107
- assert_equal 2, @proxy.tp_result_chain.size
107
+ assert_equal 2, @proxy.trace_chain.size
108
108
  end
109
109
 
110
110
  def test_trace_with_full_feature
111
111
  SourceRoute.trace method_id: 'nonsense', full_feature: 10 do
112
112
  SampleApp.new.nonsense
113
113
  end
114
- first_result = @proxy.tp_result_chain.first
114
+ first_result = @proxy.trace_chain.first
115
115
  assert_equal first_result.tp_self_refer, 0
116
116
  end
117
117
 
@@ -123,20 +123,20 @@ class SourceRouteTest < Minitest::Test
123
123
  # assert @proxy.tp_self_caches.first.is_a? SampleApp
124
124
  # end
125
125
 
126
- def test_stringify_tp_result_chain_only
126
+ def test_stringify_trace_chain_only
127
127
  SourceRoute.trace method_id: 'nonsense', full_feature: true do
128
128
  SampleApp.new.nonsense
129
129
  end
130
- origin_tp_result_chain = @proxy.tp_result_chain
131
- assert @proxy.tp_result_chain.first.stringify.defined_class.is_a? String
132
- assert_equal origin_tp_result_chain, @proxy.tp_result_chain
130
+ origin_trace_chain = @proxy.trace_chain
131
+ assert @proxy.trace_chain.first.stringify.defined_class.is_a? String
132
+ assert_equal origin_trace_chain, @proxy.trace_chain
133
133
  end
134
134
 
135
135
  def test_trace_without_first_hash_option
136
136
  SourceRoute.trace output_format: :test do
137
137
  SampleApp.new.nonsense
138
138
  end
139
- assert @proxy.tp_result_chain.size > 0
139
+ assert @proxy.trace_chain.size > 0
140
140
  refute @proxy.tp.enabled?
141
141
  end
142
142
 
@@ -145,7 +145,7 @@ class SourceRouteTest < Minitest::Test
145
145
  event :call, :return
146
146
  end
147
147
  SampleApp.new.nonsense
148
- assert_equal 2, @proxy.tp_result_chain.size
148
+ assert_equal 2, @proxy.trace_chain.size
149
149
  end
150
150
 
151
151
  # but local var didnt displayed
@@ -157,7 +157,7 @@ class SourceRouteTest < Minitest::Test
157
157
 
158
158
  SampleApp.new.nonsense_with_params(88)
159
159
 
160
- ret_value = @proxy.tp_result_chain.last
160
+ ret_value = @proxy.trace_chain.last
161
161
  end
162
162
 
163
163
  def test_track_local_var_when_event_is_return
@@ -167,9 +167,9 @@ class SourceRouteTest < Minitest::Test
167
167
  end
168
168
 
169
169
  SampleApp.new.nonsense_with_params(88)
170
- assert_equal 1, @proxy.tp_result_chain.size
170
+ assert_equal 1, @proxy.trace_chain.size
171
171
 
172
- ret_value_for_return_event = @proxy.tp_result_chain.last
172
+ ret_value_for_return_event = @proxy.trace_chain.last
173
173
  assert_equal 88, ret_value_for_return_event.local_var[:param1]
174
174
  assert_equal 5, ret_value_for_return_event.local_var[:param2]
175
175
  end
@@ -181,8 +181,8 @@ class SourceRouteTest < Minitest::Test
181
181
  end
182
182
  SampleApp.new('ins sure').nonsense_with_instance_var
183
183
 
184
- assert_equal 4, @proxy.tp_result_chain.size
185
- ret_value = @proxy.tp_result_chain.pop
184
+ assert_equal 4, @proxy.trace_chain.size
185
+ ret_value = @proxy.trace_chain.pop
186
186
 
187
187
  assert_equal 'ins sure', ret_value.instance_var[:@sample]
188
188
  end
@@ -192,8 +192,20 @@ class SourceRouteTest < Minitest::Test
192
192
  full_feature 10
193
193
  end
194
194
  SampleApp.new('cool stuff').init_cool_app
195
- @proxy.tp_result_chain.import_return_value_to_call_chain
196
- assert @proxy.tp_result_chain.call_chain[0].return_value, 'call results should contain return_value'
195
+ @proxy.trace_chain.import_return_value_to_call_chain
196
+ assert @proxy.trace_chain.call_chain[0].return_value, 'call results should contain return_value'
197
+ end
198
+
199
+ def test_trace_when_value_was_passed_to_method
200
+ hash = {x: 1, y: 2}
201
+ SourceRoute.enable do
202
+ track_params hash
203
+ end
204
+ SampleApp.new
205
+ ca = SampleApp::CoolApp.new
206
+ ca.reverse_hash(hash)
207
+ SourceRoute.output_html
208
+ assert_equal 1, @proxy.trace_chain.size
197
209
  end
198
210
 
199
211
  def test_order_call_sequence
@@ -202,8 +214,8 @@ class SourceRouteTest < Minitest::Test
202
214
  end
203
215
  SampleApp.new.nonsense_with_instance_var
204
216
 
205
- @proxy.tp_result_chain.treeize_call_chain
206
- call_results = @proxy.result_builder.tp_result_chain.call_chain
217
+ @proxy.trace_chain.treeize_call_chain
218
+ call_results = @proxy.result_builder.trace_chain.call_chain
207
219
 
208
220
  nonsense_call_tp = call_results.find { |tp| tp.method_id == :nonsense }
209
221
  nonsense_with_instance_var_call_tp = call_results.find do |tp|
@@ -211,7 +223,7 @@ class SourceRouteTest < Minitest::Test
211
223
  end
212
224
  assert_equal [nonsense_with_instance_var_call_tp.order_id], nonsense_call_tp.parent_ids
213
225
  assert_equal 1, nonsense_call_tp.parent_length
214
- assert_equal [0, 1], @proxy.result_builder.tp_result_chain.parent_length_list
226
+ assert_equal [0, 1], @proxy.result_builder.trace_chain.parent_length_list
215
227
  assert_equal [nonsense_call_tp.order_id], nonsense_with_instance_var_call_tp.direct_child_order_ids
216
228
  end
217
229
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: source_route
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - raykin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-03 00:00:00.000000000 Z
11
+ date: 2017-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: awesome_print
@@ -94,20 +94,6 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
- - !ruby/object:Gem::Dependency
98
- name: pry
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '0'
111
97
  - !ruby/object:Gem::Dependency
112
98
  name: pry-byebug
113
99
  requirement: !ruby/object:Gem::Requirement
@@ -187,22 +173,21 @@ files:
187
173
  - lib/source_route/core_ext.rb
188
174
  - lib/source_route/formats/html.rb
189
175
  - lib/source_route/formats/html_semantic.slim
190
- - lib/source_route/formats/html_semantic.slim.failtry
176
+ - lib/source_route/formats/html_semantic_vue.slim
191
177
  - lib/source_route/generate_result.rb
192
178
  - lib/source_route/json_overrides/activerecord_associations_association.rb
193
- - lib/source_route/jsonify.rb
194
179
  - lib/source_route/proxy.rb
195
180
  - lib/source_route/rails_plugins/source_track_middleware.rb
196
- - lib/source_route/tp_filter.rb
197
181
  - lib/source_route/tp_result.rb
198
- - lib/source_route/tp_result_chain.rb
182
+ - lib/source_route/trace_chain.rb
183
+ - lib/source_route/trace_filter.rb
199
184
  - lib/source_route/version.rb
200
185
  - source_route.gemspec
201
186
  - test/fake_app.rb
202
187
  - test/sample_app.rb
203
188
  - test/source_route/config_test.rb
204
189
  - test/source_route/proxy_test.rb
205
- - test/source_route/tp_filter_test.rb
190
+ - test/source_route/trace_filter_test.rb
206
191
  - test/source_route_test.rb
207
192
  - test/test_helper.rb
208
193
  homepage: http://github.com/raykin/source-route
@@ -225,7 +210,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
225
210
  version: '0'
226
211
  requirements: []
227
212
  rubyforge_project:
228
- rubygems_version: 2.5.1
213
+ rubygems_version: 2.6.8
229
214
  signing_key:
230
215
  specification_version: 4
231
216
  summary: Wrapper of TracePoint.
@@ -234,7 +219,6 @@ test_files:
234
219
  - test/sample_app.rb
235
220
  - test/source_route/config_test.rb
236
221
  - test/source_route/proxy_test.rb
237
- - test/source_route/tp_filter_test.rb
222
+ - test/source_route/trace_filter_test.rb
238
223
  - test/source_route_test.rb
239
224
  - test/test_helper.rb
240
- has_rdoc: