cutter 0.8.7 → 0.8.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -4,15 +4,15 @@ Two-methods-gem I use a lot for simple debugging & performance measuring purpose
4
4
 
5
5
  ```#inspect``` method shines when doing reverse engineering, it is especially useful, when it is needed to quickly hack on someone else's code. Also, it is very easy to become 'someone else' for self, when dealing with own code, if it was written very long time ago.
6
6
 
7
- Besides that ```#stamper``` allows doing some performance measuments in a handy manner, it can be used to create quick and neat demonstrations of how particular pieces of Ruby code perform.
7
+ Besides that ```#stamper``` allows doing performance measuments in a handy manner, it can be used to create quick and neat demonstrations of how particular pieces of Ruby code perform.
8
8
 
9
- The one interesting possible usage of ```#stamper``` is to optimize performance of templates on Rails View layer, because it often takes a large (compared to M and C layers) load impact because of Rails lazy-evaluation mechanisms.
9
+ The one interesting possible usage of ```#stamper``` is performance optimization of templates on Rails View Layer, because it often takes a large load impact (compared to M and C layers) because of Rails lazy-evaluation mechanisms.
10
10
 
11
11
  [![Build Status](https://secure.travis-ci.org/stanislaw/cutter.png)](http://travis-ci.org/stanislaw/cutter)
12
12
 
13
13
  ## Prerequisites
14
14
 
15
- It works on 1.8.7, 1.9.3, JRuby and Rubinius
15
+ It works on 1.8.7, 1.9.3, JRuby and Rubinius.
16
16
 
17
17
  ## Installiation
18
18
 
@@ -25,6 +25,7 @@ end
25
25
  ```
26
26
 
27
27
  ## Cutter::Inspection
28
+
28
29
  ### I) #inspect!
29
30
 
30
31
  Insert ```#inspect!``` method into any of your methods:
@@ -167,24 +168,26 @@ end
167
168
 
168
169
  ### #iii
169
170
 
170
- Instead of ```#inspect!``` you can use ```#iii``` - just an alias more convenient for typing. Finally, you have a group of 4 three-letters methods in your every day debugging workflow.
171
+ Instead of ```#inspect!``` you can use ```#iii``` - just an alias more convenient for typing.
172
+
173
+ Finally, you have a group of 4 three-letters methods in your every day debugging workflow.
171
174
 
172
175
  ## II) Cutter::Stamper
173
176
 
174
- Acts as ```benchmark {}``` in Rails or ```Benchmark.measure {}``` (common Ruby) but with stamps in any position in block executed.
177
+ Acts as ```benchmark {}``` in Rails or ```Benchmark.measure {}``` in common Ruby, but with stamps in any position in block executed.
175
178
 
176
179
  It is much simpler to write Stamper with Stamps than all these Measure-dos.
177
180
 
178
181
  ### Minimal stamper
179
182
 
180
- ```stamp!``` method is just an alias for ```stamp```, use whatever you like.
183
+ ```stamp!``` method is just an alias for ```stamp```, use whatever you like:
181
184
 
182
185
  ```ruby
183
186
  puts "Minimal stamper"
184
- stamper do |s|
187
+ stamper do
185
188
  stamp
186
189
  sleep 0.2
187
- stamp!
190
+ stamp!
188
191
  sleep 0.2
189
192
  stamp!
190
193
  end
@@ -220,7 +223,8 @@ end
220
223
 
221
224
  stamper :testing_method do |tm|
222
225
  sleep 0.3
223
- tm.stamp! :_1
226
+ tm.stamp! :_1 # The old form of calling #stamp! on yielded scope
227
+ variable
224
228
  sleep 0.3
225
229
  stamper :inner_scope do |i|
226
230
  sleep 0.2
@@ -255,7 +259,7 @@ Demonstration of named stamping
255
259
 
256
260
  ## Notes
257
261
 
258
- * Both ```#inspect! {}``` and ```#stamper``` method colorize their output. You can see ```lib/cutter/colored_output.rb``` file to understand how it is done. I will really appreciate any suggestions of how current color scheme can be improved.
262
+ * Both ```#inspect! {}``` and ```#stamper``` methods colorize their output. You can see ```lib/cutter/colored_output.rb``` file to understand how it is done. I will really appreciate any suggestions of how current color scheme can be improved.
259
263
 
260
264
  ## Specs and demos
261
265
 
@@ -4,3 +4,5 @@ require 'cutter/colored_outputs'
4
4
  require 'cutter/stamper'
5
5
  require 'cutter/inspection'
6
6
 
7
+ require 'cutter/kernel'
8
+ require 'cutter/array'
@@ -0,0 +1,5 @@
1
+ class Array
2
+ def extract_options!
3
+ last.is_a?(::Hash) ? pop : {}
4
+ end unless [].respond_to? :extract_options!
5
+ end
@@ -0,0 +1,15 @@
1
+ require 'stringio'
2
+
3
+ # http://thinkingdigitally.com/archive/capturing-output-from-puts-in-ruby/
4
+ module Kernel
5
+ def capture_stdout
6
+ out = StringIO.new
7
+ $stdout = out
8
+
9
+ yield
10
+
11
+ out
12
+ ensure
13
+ $stdout = STDOUT
14
+ end
15
+ end
@@ -1,11 +1,20 @@
1
- class Object
2
- def time_now
3
- Time.now.strftime("%s%L").to_i
1
+ class Time
2
+ def ms_since time
3
+ ((Time.now - time) * 1000).to_i
4
4
  end
5
+ end
5
6
 
6
- def stamper name = nil, &block
7
-
7
+ class Object
8
+ def stamper *args, &block
8
9
  return if stamper_class.off?
10
+
11
+ options = args.extract_options!
12
+
13
+ name = args.first
14
+
15
+ capture = options.delete :capture
16
+
17
+
9
18
  scope = stamper_class[name] || stamper_class[:default]
10
19
  scope.indent = stamper_class.last ? stamper_class.last.indent + 1 : 0
11
20
  stamper_class.push scope
@@ -14,35 +23,42 @@ class Object
14
23
  if scope
15
24
  message = scope.label.values.first
16
25
  end
26
+
27
+ print "\n"
28
+
17
29
  spaces = " " * scope.indent
18
- puts "\n"
30
+
19
31
  log_coloured spaces, "#{message}", __color__(:message_name)
20
32
  log_coloured spaces, "#{'-'*message.length}", __color__(:message_line)
21
33
 
22
- scope.time_initial = time_now
34
+ scope.time_initial = Time.now
23
35
 
24
- self.class.send :define_method, :stamp do |*args|
36
+ (class << self; self end).send :define_method, :stamp do |*args|
25
37
  scope.stamp args.first
26
38
  end
27
- self.class.send :alias_method, :stamp!, :stamp
28
-
29
- yield scope
30
39
 
31
- instance_eval do
32
- undef :stamp if respond_to? :stamp
33
- undef :stamp! if respond_to? :stamp!
40
+ (class << self; self end).send :define_method, :stamp! do |*args|
41
+ scope.stamp args.first
34
42
  end
35
43
 
44
+ capture ? capture_stdout { yield scope } : yield(scope)
45
+
46
+ (class << self; self end).send :remove_method, :stamp if respond_to? :stamp
47
+ (class << self; self end).send :remove_method, :stamp! if respond_to? :stamp!
48
+
36
49
  scope.indent -= 1 if scope.indent > 0
37
50
  stamper_class.pop
38
- time_passed = time_now - scope.time_initial
51
+
52
+ time_passed = Time.now.ms_since scope.time_initial
39
53
 
40
54
  tps = "#{time_passed}ms"
41
55
  offset = message.length - tps.length
42
56
  offset = 0 if offset < 0
43
57
  log_coloured spaces, "#{'-'*message.length}", __color__(:total_line)
44
58
  log_coloured spaces + "#{' ' * (offset)}", tps, __color__(:total_count)
45
- puts "\n"
59
+ print "\n"
60
+
61
+ tps
46
62
  end
47
63
 
48
64
  private
@@ -115,9 +131,9 @@ module Cutter
115
131
  def stamp lbl = nil
116
132
  return if Stamper.off?
117
133
  message = messages[lbl] || lbl.to_s
118
- time_passed = time_now - time_initial
134
+ time_passed = Time.now.ms_since time_initial
119
135
  print " " * nindent
120
- printf("stamp: %7d ms #{message}\n", time_passed)
136
+ printf("stamp: %7dms #{message}\n", time_passed)
121
137
  end
122
138
  alias_method :stamp!, :stamp
123
139
 
@@ -1,3 +1,3 @@
1
1
  module Cutter
2
- VERSION="0.8.7"
2
+ VERSION="0.8.8"
3
3
  end
metadata CHANGED
@@ -1,128 +1,123 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: cutter
3
- version: !ruby/object:Gem::Version
4
- hash: 49
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.8.8
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 8
9
- - 7
10
- version: 0.8.7
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - stanislaw
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-06-30 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
21
- type: :runtime
22
- requirement: &id001 !ruby/object:Gem::Requirement
23
- none: false
24
- requirements:
25
- - - ">="
26
- - !ruby/object:Gem::Version
27
- hash: 1
28
- segments:
29
- - 0
30
- - 5
31
- version: "0.5"
32
- version_requirements: *id001
12
+ date: 2012-07-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
33
15
  name: colorize
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0.5'
22
+ type: :runtime
34
23
  prerelease: false
35
- - !ruby/object:Gem::Dependency
36
- type: :development
37
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
38
25
  none: false
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- hash: 3
43
- segments:
44
- - 0
45
- version: "0"
46
- version_requirements: *id002
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0.5'
30
+ - !ruby/object:Gem::Dependency
47
31
  name: bundler
48
- prerelease: false
49
- - !ruby/object:Gem::Dependency
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
50
38
  type: :development
51
- requirement: &id003 !ruby/object:Gem::Requirement
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
52
41
  none: false
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- hash: 3
57
- segments:
58
- - 0
59
- version: "0"
60
- version_requirements: *id003
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
61
47
  name: jeweler
62
- prerelease: false
63
- - !ruby/object:Gem::Dependency
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
64
54
  type: :development
65
- requirement: &id004 !ruby/object:Gem::Requirement
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
66
57
  none: false
67
- requirements:
68
- - - ">="
69
- - !ruby/object:Gem::Version
70
- hash: 3
71
- segments:
72
- - 0
73
- version: "0"
74
- version_requirements: *id004
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
75
63
  name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
76
71
  prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
77
78
  description: Ruby tracing gem
78
79
  email: s.pankevich@gmail.com
79
80
  executables: []
80
-
81
81
  extensions: []
82
-
83
- extra_rdoc_files:
82
+ extra_rdoc_files:
84
83
  - LICENSE.txt
85
84
  - README.md
86
- files:
85
+ files:
87
86
  - lib/cutter.rb
87
+ - lib/cutter/array.rb
88
88
  - lib/cutter/colored_outputs.rb
89
89
  - lib/cutter/inspection.rb
90
+ - lib/cutter/kernel.rb
90
91
  - lib/cutter/stamper.rb
91
92
  - lib/cutter/version.rb
92
93
  - LICENSE.txt
93
94
  - README.md
94
95
  homepage: http://github.com/stanislaw/cutter
95
- licenses:
96
+ licenses:
96
97
  - MIT
97
98
  post_install_message:
98
99
  rdoc_options: []
99
-
100
- require_paths:
100
+ require_paths:
101
101
  - lib
102
- required_ruby_version: !ruby/object:Gem::Requirement
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
103
  none: false
104
- requirements:
105
- - - ">="
106
- - !ruby/object:Gem::Version
107
- hash: 3
108
- segments:
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ segments:
109
109
  - 0
110
- version: "0"
111
- required_rubygems_version: !ruby/object:Gem::Requirement
110
+ hash: 678323535
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
112
  none: false
113
- requirements:
114
- - - ">="
115
- - !ruby/object:Gem::Version
116
- hash: 3
117
- segments:
118
- - 0
119
- version: "0"
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
120
117
  requirements: []
121
-
122
118
  rubyforge_project:
123
119
  rubygems_version: 1.8.19
124
120
  signing_key:
125
121
  specification_version: 3
126
122
  summary: Ruby tracing gem
127
123
  test_files: []
128
-