wtf-tools 1.0.1 → 1.0.2

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: 8b03d032d5d09b5261862248f5cc65b8e4932d16
4
- data.tar.gz: dcfd4c3e52e98bedbd466691a2d06bd4d5b70e51
3
+ metadata.gz: 57c0c8fb5737da67755dd7eb6736617ece8b31b7
4
+ data.tar.gz: 3f53602929c2cbb6a99e69ab2f54ad7f17024dc7
5
5
  SHA512:
6
- metadata.gz: 00326e4785fcbb49bb840148ea29b82f6be03fe967b910f97aea15203a499251873b2603c0b99a62aeffe3fc4c9c69014fb73db3c24fbc778804cd9d61f648fe
7
- data.tar.gz: 9ccf0185977fc76a51582adb8c78fac32db6fa149ca4ed35a65f047b1c3dc4def4a2af0314ca7f1b802d9c370e57fa072af790f6409e7d6662c95973febaa776
6
+ metadata.gz: d3037df787128aee6fff83d936488f9870256f800c188ab935aa473ddb7b5c8d85f2ff225fdf75ecfb1c00e71bacc9b23397580ac5e7212c1b0f97c80b6029a2
7
+ data.tar.gz: 244a0c3410d67d00a1cc62962f80828b25a39a7f414f61a1c6526e06b8bd92721f7af49f039c0b9a7abea2d69a839150ea8e071406e189e0aa17c8b86ae2386b
data/README.md CHANGED
@@ -21,6 +21,7 @@ Usage examples
21
21
  WTF? my_var # basic
22
22
  WTF? my_var, other_var, { some: data }, :pp # more data, option: pretty-print
23
23
  WTF? :label, records, :bare, :time # multiple options given
24
+ data.wtf(:time).some_method # inline call
24
25
  ```
25
26
 
26
27
  Supported options
@@ -43,12 +44,9 @@ Formatting
43
44
  :bare modifier, ActiveRecord with just id attributs: #<MyClass id: 1234>
44
45
 
45
46
  Output control
46
- (default) to the chosen logger (see configuration)
47
- :log to Rails default logger
47
+ :puts to STDOUT (default)
48
48
  :file to a separate file in configured location
49
- :page to a thread variable Thread.current[:wtf]
50
- :redis to a Redis list on key 'wtf', expiring in 30min
51
- :raise raise the string containing data as exception
49
+ :error raise the string containing data as exception
52
50
  ```
53
51
 
54
52
  ---
@@ -7,8 +7,10 @@ require 'active_support/logger'
7
7
  require 'fileutils'
8
8
 
9
9
  WTF.options = {
10
- :default => ActiveSupport::Logger.new('./example.log'),
11
- :files => './wtf',
10
+ files: './wtf',
11
+ output: {
12
+ default: ActiveSupport::Logger.new('./example.log'),
13
+ }
12
14
  }
13
15
 
14
16
  data = { 'some' => { 'nested' => { 'data' => 17 } }, :question => %w(life universe and everything), :answer => 42 }
@@ -24,3 +26,5 @@ WTF? data, :pp, :nl
24
26
  WTF? data, :json, :nl
25
27
 
26
28
  WTF? data, :yaml, :no, :file
29
+
30
+ data.wtf(:file).size
@@ -9,7 +9,9 @@ WTF.time {
9
9
 
10
10
  # logging time with specified precision and options
11
11
  WTF.options = {
12
- :default => ActiveSupport::Logger.new('./example.log'),
12
+ output: {
13
+ default: ActiveSupport::Logger.new('./example.log'),
14
+ }
13
15
  }
14
16
  result = WTF.time(4, :nl) {
15
17
  sleep 3.12346
@@ -13,8 +13,16 @@ module WTF
13
13
  end
14
14
 
15
15
  def files_path
16
- dirs = FileUtils.mkdir_p(options[:files])
17
- dirs.first
16
+ if options[:files]
17
+ dirs = FileUtils.mkdir_p(options[:files])
18
+ dirs.first
19
+ else
20
+ Dir.getwd
21
+ end
22
+ end
23
+
24
+ def output_options
25
+ Hash(options[:output])
18
26
  end
19
27
 
20
28
  # TODO: separately track ActiveRecord finders usage in the related methods
@@ -42,7 +50,7 @@ module WTF
42
50
  before = AbsoluteTime.now
43
51
  result = yield
44
52
  duration = AbsoluteTime.now - before
45
- WTF::Dumper.new(duration.round(precision), *args)
53
+ WTF::Dumper.new(duration.round(precision), *args).call
46
54
  result
47
55
  end
48
56
  end
@@ -50,7 +58,12 @@ end
50
58
 
51
59
  Object.class_eval do
52
60
  def WTF?(*args)
53
- WTF::Dumper.new(*args)
61
+ WTF::Dumper.new(*args).call
54
62
  nil
55
63
  end
64
+
65
+ def wtf(*args)
66
+ WTF::Dumper.new(self, *args).call
67
+ self
68
+ end
56
69
  end
@@ -1,26 +1,32 @@
1
1
  module WTF
2
2
  class Dumper
3
- OPTIONS = [
4
- :time, :nl, :no, # prefix
5
- :pp, :yaml, :json, :text, :line, :csv, # format
6
- :bare, # modify
7
- :page, :file, :raise, :redis, :log, # output
8
- ].freeze
3
+ PREFIX_OPTIONS = [:time, :nl, :no].freeze
4
+ FORMAT_OPTIONS = [:pp, :yaml, :json, :text, :line, :csv].freeze
5
+ MODIFY_OPTIONS = [:bare].freeze
6
+ OUTPUT_OPTIONS = [:puts, :error, :file].freeze
7
+
8
+ OPTIONS = (PREFIX_OPTIONS + FORMAT_OPTIONS + MODIFY_OPTIONS + OUTPUT_OPTIONS).freeze
9
9
 
10
10
  attr_reader :options
11
11
 
12
12
  def initialize(*args)
13
13
  @options = {}
14
- while OPTIONS.include?(args.last)
14
+ while is_option?(args.last)
15
15
  @options[args.pop] = true
16
16
  end
17
+ end
17
18
 
19
+ def call
18
20
  data = prefix(args) << format(args)
19
21
  output(data)
20
22
  end
21
23
 
22
24
  private
23
25
 
26
+ def is_option?(sym)
27
+ OPTIONS.include?(sym) or WTF.output_options.key?(sym)
28
+ end
29
+
24
30
  def prefix(args)
25
31
  data = ''
26
32
  return data if options[:no]
@@ -54,28 +60,6 @@ module WTF
54
60
  end
55
61
  end
56
62
 
57
- def output(data)
58
- case
59
- when options[:page]
60
- (Thread.current[:wtf] ||= []) << data
61
- when options[:file]
62
- time = Time.now.strftime('%m%d_%H%M%S')
63
- file = "#{WTF.files_path}/wtf_#{time}_#{rand(10000)}.txt"
64
- File.write(file, data)
65
- when options[:raise]
66
- raise StandardError, data
67
- when options[:redis]
68
- WTF.options[:redis].rpush('wtf', data)
69
- WTF.options[:redis].expire('wtf', 30*60)
70
- when options[:log]
71
- Rails.logger.info(data)
72
- when WTF.options[:default]
73
- WTF.options[:default].info(data)
74
- else
75
- puts data
76
- end
77
- end
78
-
79
63
  def cleanup(str, bare = false)
80
64
  # remove array parentheses
81
65
  str.gsub!(/^\[|\]$/,'')
@@ -83,5 +67,46 @@ module WTF
83
67
  str.gsub!(/#<[A-Z]\w+ id: \d+\K.*?>/, '>') if bare
84
68
  str
85
69
  end
70
+
71
+ def output(data)
72
+ selected = (OUTPUT_OPTIONS + WTF.output_options.keys).select { |how| options[how] }
73
+ selected << :default if selected.empty?
74
+ selected.each do |how|
75
+ Output.new(data).call(how)
76
+ end
77
+ end
78
+
79
+ class Output
80
+ attr_reader :data
81
+
82
+ def initialize(data)
83
+ @data = data
84
+ end
85
+
86
+ def call(meth)
87
+ if block = WTF.output_options[meth]
88
+ block.call(meth)
89
+ else
90
+ send(meth)
91
+ end
92
+ end
93
+
94
+ private
95
+
96
+ def puts
97
+ STDOUT.puts(data)
98
+ end
99
+ alias_method :default, :puts
100
+
101
+ def file
102
+ time = Time.now.strftime('%m%d_%H%M%S')
103
+ file = File.join(WTF.files_path, "wtf_#{time}_#{rand(10000)}.txt")
104
+ File.write(file, data)
105
+ end
106
+
107
+ def error
108
+ raise StandardError, data
109
+ end
110
+ end
86
111
  end
87
112
  end
@@ -82,7 +82,8 @@ module WTF
82
82
  data.unshift(%w(class method count time heap_mb))
83
83
 
84
84
  time = Time.now.strftime('%m%d_%H%M%S')
85
- File.write("#{WTF.files_path}/track_#{time}_#{rand(10000)}.csv", data.map(&:to_csv).join)
85
+ file = File.join(WTF.files_path, "track_#{time}_#{rand(10000)}.csv")
86
+ File.write(file, data.map(&:to_csv).join)
86
87
  end
87
88
  end
88
89
  end
@@ -28,7 +28,7 @@ module WTF
28
28
  def on_sql(sql)
29
29
  trackables.each do |it|
30
30
  if match(it.pattern, sql)
31
- WTF::Dumper.new(:sql, sql, *caller.take(it.options[:size] || 30), :line)
31
+ WTF::Dumper.new(:sql, sql, *caller.take(it.options[:size] || 30), :line).call
32
32
  end
33
33
  end
34
34
  end
@@ -1,24 +1,24 @@
1
- Gem::Specification.new do |s|
2
- s.name = 'wtf-tools'
3
- s.version = '1.0.1'
4
- s.date = '2014-10-14'
5
- s.platform = Gem::Platform::RUBY
6
- s.required_ruby_version = Gem::Requirement.new(">= 2.0.0")
7
- s.summary = 'tools for debugging and profiling Ruby on Rails projects'
8
- s.license = 'MIT'
9
-
10
- s.description = <<-EOF
11
- WTF-tools offers some flexible options for your puts-style Ruby debugging needs,
12
- and method-level profiling for Ruby on Rails projects.
13
- EOF
14
-
15
- s.files = Dir['{lib/**/*,example/*,test/*}'] + %w(LICENSE Rakefile README.md wtf-tools.gemspec)
16
- s.require_path = 'lib'
17
- s.test_files = Dir['test/*.rb']
18
-
19
- s.author = 'Remigijus Jodelis'
20
- s.email = 'remigijus.jodelis@gmail.com'
21
- s.homepage = 'http://github.com/remigijusj/wtf-tools'
22
-
23
- s.add_runtime_dependency 'absolute_time', '~> 1.0'
24
- end
1
+ Gem::Specification.new do |s|
2
+ s.name = 'wtf-tools'
3
+ s.version = '1.0.2'
4
+ s.date = '2014-10-14'
5
+ s.platform = Gem::Platform::RUBY
6
+ s.required_ruby_version = Gem::Requirement.new(">= 2.0.0")
7
+ s.summary = 'tools for debugging and profiling Ruby on Rails projects'
8
+ s.license = 'MIT'
9
+
10
+ s.description = <<-EOF
11
+ WTF-tools offers some flexible options for your puts-style Ruby debugging needs,
12
+ and method-level profiling for Ruby on Rails projects.
13
+ EOF
14
+
15
+ s.files = Dir['{lib/**/*,example/*,test/*}'] + %w(LICENSE Rakefile README.md wtf-tools.gemspec)
16
+ s.require_path = 'lib'
17
+ s.test_files = Dir['test/*.rb']
18
+
19
+ s.author = 'Remigijus Jodelis'
20
+ s.email = 'remigijus.jodelis@gmail.com'
21
+ s.homepage = 'http://github.com/remigijusj/wtf-tools'
22
+
23
+ s.add_runtime_dependency 'absolute_time', '~> 1.0'
24
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wtf-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Remigijus Jodelis
@@ -42,9 +42,6 @@ files:
42
42
  - lib/wtf/dumper.rb
43
43
  - lib/wtf/method_tracker.rb
44
44
  - lib/wtf/query_tracker.rb
45
- - test/test_dumper.rb
46
- - test/test_method_tracker.rb
47
- - test/test_timing.rb
48
45
  - wtf-tools.gemspec
49
46
  homepage: http://github.com/remigijusj/wtf-tools
50
47
  licenses:
@@ -66,11 +63,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
63
  version: '0'
67
64
  requirements: []
68
65
  rubyforge_project:
69
- rubygems_version: 2.4.5
66
+ rubygems_version: 2.5.1
70
67
  signing_key:
71
68
  specification_version: 4
72
69
  summary: tools for debugging and profiling Ruby on Rails projects
73
- test_files:
74
- - test/test_dumper.rb
75
- - test/test_method_tracker.rb
76
- - test/test_timing.rb
70
+ test_files: []
@@ -1,8 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'wtf-tools'
3
-
4
- class TestWtfDumper < Minitest::Unit::TestCase
5
- def test_dump
6
- # TODO
7
- end
8
- end
@@ -1,8 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'wtf-tools'
3
-
4
- class TestWtfMethodTracker < Minitest::Unit::TestCase
5
- def test_track
6
- # TODO
7
- end
8
- end
@@ -1,8 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'wtf-tools'
3
-
4
- class TestWtfTiming < Minitest::Unit::TestCase
5
- def test_timing
6
- # TODO
7
- end
8
- end