loba 0.2.0 → 0.3.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: 140168f9589fcef334a886a988b973ad67a01642
4
- data.tar.gz: d07c018d2ba7208329f6d2bc428d7709411e4e3b
3
+ metadata.gz: b9de2bca8c9af5a371942f7ddf2f60d29cf6eaa7
4
+ data.tar.gz: 463649f58af8da129580719b0d9cf9cf07ca1a4b
5
5
  SHA512:
6
- metadata.gz: 3a6306138c2e1ec190d63d19302c5787ee4e1b617cabf15f88ea2d5699f306fd3f7c94103713b4b8595b5c51dab7ad8990a4d32d1f45844a95a03ff34094b5e5
7
- data.tar.gz: 9131d9074be00bfb1c9cc6804addb48b649e51bb3f1a3b38ce675336e4d506da44bc26647c2d8cbe1e93951b56cbee4e545e118737c3d1394f4f8ba0073634ef
6
+ metadata.gz: 8fa11f7116da2e56106805244ffed2b37f94f99a394d3391a3d39b689357a5708a76691f751ed4ad60ce1cc5b5cfab4d283a87a1b4d63b72ae6dcab7c94f0277
7
+ data.tar.gz: 2bd9887db8c466e8bc2b8970014b67437045c472ce0a8ef3fab7cc49f9cfb53f8b8c876e29cd65911a31dad42490a8acea12b8936329827d43c488ccf89be19b
data/README.md CHANGED
@@ -1,3 +1,4 @@
1
+ [![Gem Version](https://badge.fury.io/rb/loba.svg)](https://badge.fury.io/rb/loba)
1
2
  [![Dependency Status](https://gemnasium.com/rdnewman/loba.svg)](https://gemnasium.com/rdnewman/loba)
2
3
  [![Build Status](https://travis-ci.org/rdnewman/loba.svg?branch=master)](https://travis-ci.org/rdnewman/loba)
3
4
  [![Code Climate](https://codeclimate.com/github/rdnewman/loba/badges/gpa.svg)](https://codeclimate.com/github/rdnewman/loba)
@@ -17,8 +18,8 @@ Easy tracing for debugging: handy methods for adding trace lines to output or Ra
17
18
 
18
19
  There are two kinds of questions I usually want to answer when trying to diagnose code behavior:
19
20
 
20
- 1. Is this spot of code being reached (or is it reached in the order I think it is)?
21
- 1. What is the value of this variable?
21
+ 1. Is this spot of code being reached (or is it reached in the order I think it is)?
22
+ 1. What is the value of this variable?
22
23
 
23
24
  Loba statements are intended to be terse to minimize typing.
24
25
 
@@ -47,9 +48,11 @@ For example,
47
48
  To invoke,
48
49
 
49
50
  ```ruby
50
- Loba.ts # no arguments, generally (see Environment Notes below)
51
+ Loba.ts # or with optional arguments
51
52
  ```
52
53
 
54
+ `Loba.timestamp` is an alias for `Loba.ts`.
55
+
53
56
  You can read [more detail](readme/ts.md) on this command.
54
57
 
55
58
  #### Variable or method return inspection: `Loba.val`
@@ -103,7 +106,7 @@ This section is only relevant in Rails environments.
103
106
 
104
107
  The expectation is that Loba statements are just for development or test trace statements. Generally, it's a bad idea to leave diagnostic code in Rails production; still, it can happen. And, occasionally, it can be useful to have trace statements in production too if you have an issue that is difficult to reproduce.
105
108
 
106
- `Loba.ts` and `Loba.val` try to protect against timestamp or value notice requests being accidentally left in the code by checking for the Rails environment Loba is being invoked under. If in production, `Loba.ts` and `Loba.val` will normally just return immediately without rendering anything to help minimize any impact on production code. However, that behavior can be overridden with a `true` as an additional last argument to output a notice even when in the production environment. In general, this should be done sparingly if at all.
109
+ `Loba.ts` and `Loba.val` try to protect against timestamp or value notice requests being accidentally left in the code by checking for the Rails environment Loba is being invoked under. If in production, `Loba.ts` and `Loba.val` will normally just return immediately without rendering anything to help minimize any impact on production code. However, that behavior can be overridden by using the options hash with `:production => true` as an additional last argument to output a notice even when in the production environment. In general, this should be done sparingly if at all.
107
110
 
108
111
  These considerations also have an impact on how you install the Loba gem when using `bundler`. If you only install the gem for :development and :test, then any Loba statements left in the code when it goes to production will cause an error because the statements wouldn't be recognized. That's perhaps a Good Thing, if you never want them left in.
109
112
 
@@ -115,12 +118,12 @@ The following is the code example snippet but always logging even in Rails produ
115
118
  class HelloWorld
116
119
  def initialize
117
120
  @x = 42
118
- Loba.ts(true)
121
+ Loba.ts production: true
119
122
  @y = "Charlie"
120
123
  end
121
124
 
122
125
  def hello
123
- Loba.val :@x, nil, true
126
+ Loba.val :@x, nil, production: true
124
127
  puts "Hello, #{@y}" if @x == 42
125
128
  Loba.ts true
126
129
  end
@@ -136,24 +139,28 @@ Add this line to your application's Gemfile:
136
139
 
137
140
  ```ruby
138
141
  group :development, :test do
139
- gem 'loba', require: false, github: 'rdnewman/loba' # until I publish it on RubyGems
142
+ gem 'loba', require: false
140
143
  end
141
144
  ```
142
145
 
143
146
  or for all environments:
144
147
 
145
148
  ```ruby
146
- gem 'loba', require: false, github: 'rdnewman/loba' # until I publish it on RubyGems
149
+ gem 'loba', require: false
147
150
  ```
148
151
 
149
152
 
150
153
  And then execute:
151
154
 
152
- $ bundle
155
+ ```bash
156
+ $ bundle
157
+ ```
153
158
 
154
159
  Or install it yourself as:
155
160
 
156
- $ gem install loba
161
+ ```bash
162
+ $ gem install loba
163
+ ```
157
164
 
158
165
  ## Development
159
166
 
@@ -161,10 +168,26 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
161
168
 
162
169
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
163
170
 
164
- ## Contributing
171
+ ## Changelog
172
+ |version|notes|
173
+ |-------|-----|
174
+ |0.3.1|updated dependences; added inspect to Loba.val; amended parameters*|
175
+ |0.3.0|(yanked)|
176
+ |0.2.0|release on RubyGems.org|
177
+ |0.1.0|initial development|
178
+
179
+ ### Deprecations
165
180
 
166
- Bug reports and pull requests are welcome on GitHub at https://github.com/rdnewman/loba. 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.
181
+ #### 0.3.0
182
+ In prior versions, to allow Loba notes to write to the log while in :production, an optional third parameter could be supplied as merely a boolean value. In 0.3.0 and later versions, this must now be specified as part of an options hash as `{:production => true}`
183
+
184
+ ### Semantic versions
185
+
186
+ As of version 0.3.0 and later, this gem follows semantic versioning [2.0.0](http://semver.org/spec/v2.0.0.html) conventions.
187
+
188
+ ## Contributing
167
189
 
190
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/rdnewman/loba>. 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.
168
191
 
169
192
  ## License
170
193
 
@@ -1,4 +1,5 @@
1
1
  require 'loba/version'
2
+
2
3
  require 'singleton'
3
4
  require 'binding_of_caller'
4
5
  require 'colorize'
@@ -10,14 +11,21 @@ module Loba
10
11
 
11
12
  # Outputs a timestamped notice, useful for quick traces to see the code path.
12
13
  # Also does a simple elapsed time check since the previous timestamp notice to help with quick, minimalist profiling.
13
- # @param production_is_ok [Boolean] true if this timestamp notice is enabled when running in :production environment
14
+ # @param options [Hash] options for use
15
+ # @option options [Boolean] :production (false) true if this timestamp notice is enabled when running in :production environment
14
16
  # @return [NilClass] nil
15
17
  # @example Basic use
16
18
  # def hello
17
19
  # Loba.ts
18
20
  # end
19
21
  # #=> [TIMESTAMP] #=0001, diff=0.000463, at=1451615389.505411, in=/path/to/file.rb:2:in 'hello'
20
- def ts(production_is_ok = false)
22
+ def ts(options = {})
23
+
24
+ # evaluate options
25
+ filtered_options = Internal.filter_options(options, [:production])
26
+ production_is_ok = filtered_options[:production]
27
+
28
+ # log if possible
21
29
  if Internal::Platform.logging_ok?(production_is_ok)
22
30
  @loba_logger ||= Internal::Platform.logger
23
31
  @loba_timer ||= Internal::TimeKeeper.instance
@@ -45,9 +53,16 @@ module Loba
45
53
  end
46
54
  module_function :ts
47
55
 
56
+ # Alias for Loba.ts
57
+ alias_method :timestamp, :ts
58
+ module_function :timestamp
59
+
48
60
  # Outputs a value notice showing value of provided argument including method and class identification
49
61
  # @param argument [various] the value to be evaluated and shown; if given as a Symbol, a label based on the argument will proceed the value the argument refers to
50
62
  # @param label [String] an optional, explicit label to be used instead of attempting to infer from the argument
63
+ # @param options [Hash] options for use
64
+ # @option options [Boolean] :inspect (true) true if this value notice is to use #inspect against the content being evaluated
65
+ # @option options [Boolean] :production (false) true if this value notice is enabled when running in :production environment
51
66
  # @return [NilClass] nil
52
67
  # @example Using Symbol as argument
53
68
  # class HelloWorld
@@ -79,7 +94,14 @@ module Loba
79
94
  # HelloWorld.new.hello("Charlie")
80
95
  # #=> [HelloWorld#hello] Name: Charlie (at /path/to/file/hello_world.rb:3:in `hello')
81
96
  # #=> Hello, Charlie!
82
- def val(argument = :nil, label = nil, production_is_ok = false)
97
+ def val(argument = :nil, label = nil, options = {inspect: true})
98
+
99
+ # evaluate options
100
+ filtered_options = Internal.filter_options(options, [:production, :inspect])
101
+ production_is_ok = filtered_options[:production]
102
+ will_inspect = filtered_options[:inspect]
103
+
104
+ # log if possible
83
105
  if Internal::Platform.logging_ok?(production_is_ok)
84
106
  depth = 0
85
107
  @loba_logger ||= Internal::Platform.logger
@@ -95,9 +117,13 @@ module Loba
95
117
  end
96
118
 
97
119
  result = if argument.is_a?(Symbol)
98
- binding.of_caller(depth+1).eval(argument.to_s)
120
+ if will_inspect
121
+ binding.of_caller(depth+1).eval(argument.to_s).inspect
122
+ else
123
+ binding.of_caller(depth+1).eval(argument.to_s)
124
+ end
99
125
  else
100
- argument
126
+ will_inspect ? argument.inspect : argument
101
127
  end
102
128
 
103
129
  source_line = Internal.calling_source_line(depth+1)
@@ -115,9 +141,11 @@ module Loba
115
141
  module Internal
116
142
 
117
143
  class << self
118
- LOBA_CLASS_NAME = 'self.class.name'
144
+
145
+ # Prepare display of class name from where Loba was invoked
146
+ # @param depth [integer] internal tracking of call stack depth
119
147
  def calling_class_name(depth = 0)
120
- m = binding.of_caller(depth+1).eval(LOBA_CLASS_NAME)
148
+ m = binding.of_caller(depth+1).eval('self.class.name')
121
149
  if m.nil? || m.empty?
122
150
  '<anonymous class>'
123
151
  elsif m == 'Class'
@@ -127,12 +155,15 @@ module Loba
127
155
  end
128
156
  end
129
157
 
130
- LOBA_METHOD_NAME = 'self.send(:__method__)'
158
+ # Prepare display of method name from where Loba was invoked
159
+ # @param depth [integer] internal tracking of call stack depth
131
160
  def calling_method_name(depth = 0)
132
- m = binding.of_caller(depth+1).eval(LOBA_METHOD_NAME)
161
+ m = binding.of_caller(depth+1).eval('self.send(:__method__)')
133
162
  (m.nil? || m.empty?) ? '<anonymous method>' : m
134
163
  end
135
164
 
165
+ # Prepare display of whether the method from where Loba was invoked is for a class or an instance
166
+ # @param depth [integer] internal tracking of call stack depth
136
167
  def calling_method_type(depth = 0)
137
168
  if binding.of_caller(depth+1).eval('self.class.name') == 'Class'
138
169
  :class
@@ -141,17 +172,63 @@ module Loba
141
172
  end
142
173
  end
143
174
 
175
+ # Prepare display of line number from where Loba was invoked [UNUSED]
176
+ # @param depth [integer] internal tracking of call stack depth
144
177
  def calling_line_number(depth = 0)
145
178
  binding.of_caller(depth+1).eval('__LINE__')
146
179
  end
147
180
 
181
+ # Assemble display that shows the method invoking Loba
182
+ # @param depth [integer] internal tracking of call stack depth
183
+ def calling_tag(depth = 0)
184
+ delim = {class: '.', instance: '#'}
185
+ "[#{calling_class_name(depth+1)}#{delim[calling_method_type(depth+1)]}#{calling_method_name(depth+1)}]"
186
+ end
187
+
188
+ # Identify source code line from where Loba was invoked
189
+ # @param depth [integer] internal tracking of call stack depth
148
190
  def calling_source_line(depth = 0)
149
191
  caller[depth]
150
192
  end
151
193
 
152
- def calling_tag(depth = 0)
153
- delim = {class: '.', instance: '#'}
154
- "[#{calling_class_name(depth+1)}#{delim[calling_method_type(depth+1)]}#{calling_method_name(depth+1)}]"
194
+ # Filters options argument for deprecated or unexpected use
195
+ # @param options [various] options argument to filter
196
+ # @param allowed_keys [array] array of expected keys in options
197
+ def filter_options(options, allowed_keys = [])
198
+ result = {}
199
+ allowed_keys.each { |key| result[key] = false }
200
+
201
+ case options
202
+ when Hash
203
+ allowed_keys.each do |key|
204
+ result[key] = !!options[key] unless options[key].nil?
205
+ end
206
+ when TrueClass
207
+ if allowed_keys.include? :production
208
+ Internal::Deprecated._0_3_0(true)
209
+ result[:production] = true
210
+ end
211
+ when FalseClass
212
+ Internal::Deprecated._0_3_0(false)
213
+ else # to be safe, treat as false
214
+ Internal::Deprecated._0_3_0(false)
215
+ end
216
+
217
+ result
218
+ end
219
+
220
+ end
221
+
222
+ # Internal class for deprecation warnings.
223
+ class Deprecated
224
+ class << self
225
+ # Deprecations as of version 0.3.0
226
+ # @param value [boolean] deprecated value supplied in original call to use in deprecation message
227
+ def _0_3_0(value)
228
+ bool = value ? "true" : "false"
229
+ verb = value ? "enabled" : "disabled"
230
+ warn "DEPRECATION WARNING: use {:production => #{bool}} instead to indicate notice is #{verb} in production"
231
+ end
155
232
  end
156
233
  end
157
234
 
@@ -1,4 +1,3 @@
1
- #class Loba
2
1
  module Loba
3
- VERSION = "0.2.0"
2
+ VERSION = "0.3.1"
4
3
  end
@@ -21,9 +21,9 @@ Gem::Specification.new do |spec|
21
21
  spec.require_paths = ["lib"]
22
22
  spec.required_ruby_version = '>= 2.1.0'
23
23
 
24
- spec.add_development_dependency "bundler", "~> 1.11"
24
+ spec.add_development_dependency 'bundler', '~> 1.11'
25
25
  spec.add_development_dependency "rake", "~> 10.0"
26
- spec.add_development_dependency "rspec-rails", "~> 3.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
27
 
28
28
  spec.add_dependency "binding_of_caller", "~> 0.7"
29
29
  spec.add_dependency "colorize", "~> 0.7"
@@ -14,6 +14,8 @@ To invoke,
14
14
  Loba.ts # no arguments
15
15
  ```
16
16
 
17
+ `Loba.timestamp` is an alias for `Loba.ts`.
18
+
17
19
  The resulting notice output format is
18
20
 
19
21
  ```
@@ -21,9 +23,23 @@ The resulting notice output format is
21
23
  ```
22
24
 
23
25
  where
24
- * `nnn` ("#=") is a sequential numbering (1, 2, 3, ...) of timestamp notices,
25
- * `ss.ssssss` ("diff=") is number of seconds since the last timestamp notice was output (i.e., relative time),
26
- * `tttttttttt.tttttt` ("at=") is Time.now (as seconds) (i.e., absolute time),
27
- * `/path/to/code/somecode.rb` ("in=") is the source code file that invoked `Loba.ts`,
28
- * `LL` ("in=...:") is the line number of the source code file that invoked `Loba.ts`, and
29
- * `some_method`is the method in which `Loba.ts` was invoked.
26
+ * `nnn` ("#=") is a sequential numbering (1, 2, 3, ...) of timestamp notices,
27
+ * `ss.ssssss` ("diff=") is number of seconds since the last timestamp notice was output (i.e., relative time),
28
+ * `tttttttttt.tttttt` ("at=") is Time.now (as seconds) (i.e., absolute time),
29
+ * `/path/to/code/somecode.rb` ("in=") is the source code file that invoked `Loba.ts`,
30
+ * `LL` ("in=...:") is the line number of the source code file that invoked `Loba.ts`, and
31
+ * `some_method`is the method in which `Loba.ts` was invoked.
32
+
33
+
34
+ ##### Options
35
+
36
+ As the last argument, an options hash may be provided:
37
+ * `production`: true if this value notice is enabled when running in :production environment (see ["Environment Notes"](README.md#environment-notes)) \[_default: `false`_\]
38
+
39
+ ###### Example 5: Using options hash
40
+ ```ruby
41
+ Loba.ts production: true
42
+ ```
43
+ ```ruby
44
+ Loba.ts {production: true}
45
+ ```
@@ -15,7 +15,7 @@ Loba.val some_identifier # directly give a variable or method name instead of a
15
15
  or
16
16
 
17
17
  ```
18
- Loba.val some_identifier "My label:" # same as direct variable, but allows a custom label
18
+ Loba.val some_identifier, "My label:" # same as direct variable, but allows a custom label
19
19
  ```
20
20
 
21
21
  Will produce a notice similar to the following:
@@ -87,17 +87,31 @@ The resulting notice output format is
87
87
  ```
88
88
 
89
89
  where
90
- * `ccccc` is the name of the class from where `Loba.val` was invoked,
91
- * `mmmmm` is the name of the method from where `Loba.val` was invoked,
92
- * `vvvvv` is generally the name of the variable for which `Loba.val` is inspecting, or any custom label given,
93
- * `rrrrr` is the result of inspecting what `Loba.val` was invoked against,
94
- * `/path/to/code/somecode.rb` is the source code file that invoked `Loba.val`,
95
- * `LL` is the line number of the source code file that invoked `Loba.val`, and
96
- * `some_method`is the method in which `Loba.val` was invoked.
90
+ * `ccccc` is the name of the class from where `Loba.val` was invoked,
91
+ * `mmmmm` is the name of the method from where `Loba.val` was invoked,
92
+ * `vvvvv` is generally the name of the variable for which `Loba.val` is inspecting, or any custom label given,
93
+ * `rrrrr` is the result of inspecting what `Loba.val` was invoked against,
94
+ * `/path/to/code/somecode.rb` is the source code file that invoked `Loba.val`,
95
+ * `LL` is the line number of the source code file that invoked `Loba.val`, and
96
+ * `some_method`is the method in which `Loba.val` was invoked.
97
97
 
98
98
 
99
99
  Notes:
100
- * `ccccc`: Ruby supports anonymous classes (e.g., `= Class.new`). If an anonymous class, "<anonymous class>" will be output here.
101
- * `mmmmm`: Ruby supports anonymous methods, procs, and lambdas. If an anonymous method, et al, "<anonymous method>" will be output here.
102
- * `vvvvv`: This depends on the argument being provided: if a symbol, then this field will use that symbol to determine the name and present it here. If not, nothing will appear for this field.
103
- * `rrrrr`: The value of the variable given to `Loba.val`. `inspect` may be used.
100
+ * `ccccc`: Ruby supports anonymous classes (e.g., `= Class.new`). If an anonymous class, "<anonymous class>" will be output here.
101
+ * `mmmmm`: Ruby supports anonymous methods, procs, and lambdas. If an anonymous method, et al, "<anonymous method>" will be output here.
102
+ * `vvvvv`: This depends on the argument being provided: if a symbol, then this field will use that symbol to determine the name and present it here. If not, nothing will appear for this field.
103
+ * `rrrrr`: The value of the variable given to `Loba.val`. `inspect` may be used (see [options](#options) below).
104
+
105
+ ##### Options
106
+
107
+ As the last argument, an options hash may be provided:
108
+ * `inspect`: true if this value notice is to use #inspect against the content being evaluated \[_default: `true`_\]
109
+ * `production`: true if this value notice is enabled when running in :production environment (see ["Environment Notes"](README.md#environment-notes)) \[_default: `false`_\]
110
+
111
+ ###### Example 5: Using options hash
112
+ ```ruby
113
+ Loba.val name, "Name:", inspect: false, production: true
114
+ ```
115
+ ```ruby
116
+ Loba.val :name, nil, {production: true}
117
+ ```
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loba
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Newman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-03 00:00:00.000000000 Z
11
+ date: 2017-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rspec-rails
42
+ name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
@@ -102,7 +102,6 @@ files:
102
102
  - lib/loba.rb
103
103
  - lib/loba/version.rb
104
104
  - loba.gemspec
105
- - readme/environments.md
106
105
  - readme/ts.md
107
106
  - readme/val.md
108
107
  - readme/zulu.png
@@ -126,9 +125,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
126
125
  version: '0'
127
126
  requirements: []
128
127
  rubyforge_project:
129
- rubygems_version: 2.5.2
128
+ rubygems_version: 2.6.11
130
129
  signing_key:
131
130
  specification_version: 4
132
131
  summary: 'Loba: Easy tracing for debugging.'
133
132
  test_files: []
134
- has_rdoc:
File without changes