putter 0.4.0 → 0.4.1

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: 16b81ce160fb22785ce8b1bdce7822783ba85eec
4
- data.tar.gz: 7bc3095434de6035e590c28a37c8d435216a5e20
3
+ metadata.gz: 72890e1762d7aad4835c7be72838d2409ecfd101
4
+ data.tar.gz: 0e24d52624633980ca47e6f1a2830e037d016a99
5
5
  SHA512:
6
- metadata.gz: 8334e5bea55fae12d4e65fd35d1077069c32895f71fe26c06673b9f006a36e6693de536753d0b13f8158e89a5a4a76d3ec8c486d055389b29ac5c778fb5b373c
7
- data.tar.gz: 30885b70b4ae1d78a1b74a2eaae951c91175e1879abd95e4721ed2c5dffdecc51c83f7b187108c2273ed538ea18f9a2abd728593e6e2f21bbccaa1630d4f184a
6
+ metadata.gz: a575a07d97f70930a795fdfea66d6c14d4ee27a41eee406206fdde067e71f847576e04065702f2ef4611cc6d52b019c4cf32fccaf2fe18628346895de8c44be0
7
+ data.tar.gz: bf1e697ce25b56fd736742b3a15303547249e355c804326a04951c3f36331505b066bbdda01a0097fd01ef248687a16a48ed73c24f272c9def9d2da78a57d865
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # CHANGELOG
2
- ### 0.4.0 - 2016-08-16
2
+ ### 0.4.1 - 2016-10-16
3
+ - By default, Putter will not run in production mode in Rails, this can be allow via configuration
4
+ - By default, Putter will ignore methods from ActiveRecord::Base if it is present
5
+ - Do not re-define a method if it is whitelisted
6
+ - Code cleanup and README updates
7
+
8
+ ### 0.4.0 - 2016-08-18
3
9
  - Add methods option to `Putter.watch`
4
10
  - Refactor watch method to use registry
5
11
  - Refactor method creation specs
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- putter (0.4.0)
4
+ putter (0.4.1)
5
5
  colorize (~> 0)
6
6
 
7
7
  GEM
@@ -42,4 +42,4 @@ DEPENDENCIES
42
42
  rspec (~> 3.0)
43
43
 
44
44
  BUNDLED WITH
45
- 1.12.4
45
+ 1.13.5
data/README.md CHANGED
@@ -9,7 +9,7 @@ Putter is a tool for more easily implementing puts debugging. Instead of litteri
9
9
  Add this line to your application's Gemfile:
10
10
 
11
11
  ```ruby
12
- gem 'putter'
12
+ gem "putter"
13
13
  ```
14
14
 
15
15
  And then execute:
@@ -112,7 +112,7 @@ Putter currently has 3 configuration options:
112
112
 
113
113
  ```ruby
114
114
  Putter.configure do |config|
115
- # 'print_strategy' takes a block that receives a data object with the label, line,
115
+ # "print_strategy" takes a block that receives a data object with the label, line,
116
116
  # method, args string, and result respectively. This block will be used after each method
117
117
  # is called, it must contain puts or logger calls, to print or any other method callbacks
118
118
  # that are helpful.
@@ -121,24 +121,24 @@ Putter.configure do |config|
121
121
  puts "#{data.line} - Label: #{data.label}, Method: #{data.method}, Args: #{data.args}, Result: #{data.result}"
122
122
  end
123
123
 
124
- # 'ignore_methods_from' takes an array of class names and will ignore both class and instance methods
124
+ # "ignore_methods_from" takes an array of class names and will ignore both class and instance methods
125
125
  # from those classes when adding methods to the proxy and adding debug output
126
- # Defaults to [Object]
126
+ # Defaults to [Object] or [Object, ActiveRecord::Based] if defined
127
127
  config.ignore_methods_from = [Object, ActiveRecord::Base]
128
128
 
129
- # 'methods_whitelist' takes an array of methods and will always proxy and debug those methods
129
+ # "methods_whitelist" takes an array of methods and will always proxy and debug those methods
130
130
  # regardless of whether or not the class is ignored and regardless of what methods are passed
131
- # in when running 'Putter.follow' or 'Putter.watch'
131
+ # in when running "Putter.follow" or "Putter.watch"
132
+ # Defaults to []
132
133
  config.methods_whitelist = [:to_s]
134
+
135
+ # "allow_production" takes a boolean and determines whether or not Putter will run if
136
+ # `Rails.env == "production"`
137
+ # Defaults to false
138
+ config.allow_production = false
133
139
  end
134
140
  ```
135
141
 
136
- ## Planned Features
137
- Feel free to open a PR to implement any of these if they are not yet added:
138
-
139
- - Active Record specific printing
140
- - Checking Rails.env to double check that putter is not called in production
141
-
142
142
  ## Contributing
143
143
 
144
144
  Bug reports and pull requests are welcome on GitHub at https://github.com/dewyze/putter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
@@ -1,11 +1,13 @@
1
1
  module Putter
2
2
  class Configuration
3
- attr_accessor :print_strategy
3
+ attr_accessor :print_strategy, :allow_production
4
4
  attr_writer :ignore_methods_from, :methods_whitelist
5
5
 
6
6
  def initialize
7
7
  @ignore_methods_from = [Object]
8
+ @ignore_methods_from << ActiveRecord::Base if defined?(ActiveRecord::Base)
8
9
  @print_strategy = PrintStrategy::Default
10
+ @allow_production = false
9
11
  @methods_whitelist = []
10
12
  end
11
13
 
@@ -2,7 +2,7 @@ module Putter
2
2
  class Follower < BasicObject
3
3
  include MethodCreator
4
4
 
5
- attr_reader :object, :proxied_methods, :proxy
5
+ attr_reader :object, :proxy
6
6
 
7
7
  def initialize(obj, options={})
8
8
  @object = obj
@@ -12,11 +12,11 @@ module Putter
12
12
  rescue ::NoMethodError
13
13
  ::Kernel.raise ::Putter::BasicObjectError
14
14
  end
15
- _set_options(options)
15
+ @data = FollowerData.new(@object, @proxy, options)
16
16
  end
17
17
 
18
18
  def method_missing(method, *args, &blk)
19
- if _add_method?(method)
19
+ if @data.add_method?(method)
20
20
  add_method(method)
21
21
  end
22
22
 
@@ -28,52 +28,9 @@ module Putter
28
28
  end
29
29
 
30
30
  def add_method(method)
31
- data = ProxyMethodData.new({ label: @label, method: method})
31
+ proxy_method_data = ProxyMethodData.new(label: @data.label, method: method)
32
32
 
33
- add_putter_instance_method_to_proxy(@proxy, data)
34
- end
35
-
36
- def _add_method?(method)
37
- return true if _is_whitelisted_method?(method)
38
- return false if _is_ignored_method?(method)
39
- return false if @proxy.instance_methods.include?(method)
40
- return @proxy_all_methods || proxied_methods.include?(method.to_s)
41
- end
42
-
43
- def _is_ignored_method?(method)
44
- ::Putter.configuration.ignore_methods_from.each do |klass|
45
- return true if klass.methods.include?(method.to_sym)
46
- return true if klass.instance_methods.include?(method.to_sym)
47
- end
48
- return false
49
- end
50
-
51
- def _is_whitelisted_method?(method)
52
- ::Putter.configuration.methods_whitelist.map(&:to_sym).include?(method.to_sym)
53
- end
54
-
55
- def _set_label(label)
56
- if !label.nil?
57
- @label = label
58
- elsif @object.class == ::Class
59
- @label = @object.name
60
- else
61
- @label = @object.class.name + " instance"
62
- end
63
- end
64
-
65
- def _set_methods(methods)
66
- if methods.nil?
67
- @proxy_all_methods = true
68
- else
69
- @proxied_methods = methods.map(&:to_s)
70
- @proxy_all_methods = false
71
- end
72
- end
73
-
74
- def _set_options(options)
75
- _set_label(options[:label])
76
- _set_methods(options[:methods])
33
+ add_putter_instance_method_to_proxy(@proxy, proxy_method_data)
77
34
  end
78
35
  end
79
36
  end
@@ -0,0 +1,41 @@
1
+ module Putter
2
+ class FollowerData
3
+ attr_accessor :label
4
+
5
+ def initialize(object, proxy, options)
6
+ @proxy = proxy
7
+ @proxied_methods = options[:methods] || []
8
+ _set_label(options[:label], object)
9
+ end
10
+
11
+ def add_method?(method)
12
+ return false if @proxy.instance_methods.include?(method)
13
+ return true if _is_whitelisted_method?(method)
14
+ return false if _is_ignored_method?(method)
15
+ return true if @proxied_methods.empty?
16
+ return true if @proxied_methods.include?(method)
17
+ end
18
+
19
+ def _set_label(label, object)
20
+ if !label.nil?
21
+ @label = label
22
+ elsif object.class == Class
23
+ @label = object.name
24
+ else
25
+ @label = object.class.name + " instance"
26
+ end
27
+ end
28
+
29
+ def _is_whitelisted_method?(method)
30
+ ::Putter.configuration.methods_whitelist.map(&:to_sym).include?(method.to_sym)
31
+ end
32
+
33
+ def _is_ignored_method?(method)
34
+ ::Putter.configuration.ignore_methods_from.each do |klass|
35
+ return true if klass.methods.include?(method.to_sym)
36
+ return true if klass.instance_methods.include?(method.to_sym)
37
+ end
38
+ return false
39
+ end
40
+ end
41
+ end
@@ -1,3 +1,3 @@
1
1
  module Putter
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
@@ -30,7 +30,7 @@ module Putter
30
30
  proxy = MethodProxy.new
31
31
 
32
32
  Putter::Watcher.methods_for(klass).each do |method|
33
- data = ProxyMethodData.new({ label: Putter::Watcher.label_for(klass), method: method })
33
+ data = ProxyMethodData.new(label: Putter::Watcher.label_for(klass), method: method)
34
34
  add_putter_class_method_to_proxy(proxy, data)
35
35
  end
36
36
 
@@ -3,25 +3,25 @@ module Putter
3
3
  attr_accessor :label, :proxy_methods
4
4
 
5
5
  def initialize(options, klass)
6
- _set_label(options[:label], klass)
7
- _set_methods(options[:methods], klass)
6
+ @label = _set_label(options[:label], klass)
7
+ @proxy_methods = _set_methods(options[:methods], klass.singleton_class)
8
8
  end
9
9
 
10
10
  def _set_label(label, klass)
11
11
  if !label.nil? && label != ""
12
- @label = label
12
+ label
13
13
  else
14
- @label = klass.name
14
+ klass.name
15
15
  end
16
16
  end
17
17
 
18
- def _set_methods(methods, klass)
18
+ def _set_methods(methods, singleton_klass)
19
19
  if methods.nil?
20
- @proxy_methods = _methods_to_proxy(klass.singleton_class)
20
+ _methods_to_proxy(singleton_klass)
21
21
  elsif !methods.is_a?(Array)
22
- @proxy_methods = [methods]
22
+ [methods]
23
23
  else
24
- @proxy_methods = methods
24
+ methods
25
25
  end
26
26
  end
27
27
 
data/lib/putter.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "putter/configuration"
2
2
  require "putter/errors"
3
+ require "putter/follower_data"
3
4
  require "putter/instance_follower"
4
5
  require "putter/method_creator"
5
6
  require "putter/method_proxy"
@@ -16,25 +17,37 @@ module Putter
16
17
 
17
18
  class << self
18
19
  attr_writer :configuration
19
- end
20
-
21
- def self.follow(obj, options={})
22
- Putter::Follower.new(obj, options)
23
- end
24
-
25
- def self.watch(obj, options={})
26
- Putter::Watcher.watch(obj, options)
27
- end
28
-
29
- def self.configuration
30
- @configuration ||= Configuration.new
31
- end
32
-
33
- def self.configure
34
- yield configuration
35
- end
36
20
 
37
- def self.reset_configuration
38
- @configuration = Configuration.new
21
+ def follow(obj, options={})
22
+ with_production_check do
23
+ Putter::Follower.new(obj, options)
24
+ end
25
+ end
26
+
27
+ def watch(obj, options={})
28
+ with_production_check do
29
+ Putter::Watcher.watch(obj, options)
30
+ end
31
+ end
32
+
33
+ def configuration
34
+ @configuration ||= Configuration.new
35
+ end
36
+
37
+ def configure
38
+ yield configuration
39
+ end
40
+
41
+ def reset_configuration
42
+ @configuration = Configuration.new
43
+ end
44
+
45
+ def with_production_check
46
+ if !configuration.allow_production && defined?(Rails) && Rails.env == "production"
47
+ puts "Putter cannot be run in production unless the 'allow_production' option is configured to true".colorize(:red)
48
+ else
49
+ yield
50
+ end
51
+ end
39
52
  end
40
53
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: putter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - John DeWyze
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-19 00:00:00.000000000 Z
11
+ date: 2016-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -109,6 +109,7 @@ files:
109
109
  - lib/putter/configuration.rb
110
110
  - lib/putter/errors.rb
111
111
  - lib/putter/follower.rb
112
+ - lib/putter/follower_data.rb
112
113
  - lib/putter/instance_follower.rb
113
114
  - lib/putter/method_creator.rb
114
115
  - lib/putter/method_proxy.rb