loba 1.2.1 → 2.0.0

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
  SHA256:
3
- metadata.gz: 196b8d0b75303a31baa40559f2ece4bb2e53ef288f78822e2436b95926826532
4
- data.tar.gz: c06af1fc1450f468abd679b86d35da01464e07d80ea35fc651b9180b1eef8291
3
+ metadata.gz: fd37694b2e7fc3a573237e959410b0bb388e4b4873f16ebb75724c8b3c2db359
4
+ data.tar.gz: c26f5ea9b333bf0bef43d19f0cd139aebe88420eb800daa344b363361fc05a05
5
5
  SHA512:
6
- metadata.gz: 5355b4ffd23f1a7d07ff28a7ecd382f5b8b6b0e103b600fa91b96233483bc60588ab108206a2e798f64084509faab69cb15aad99c87089f3a65fbebe0af48e16
7
- data.tar.gz: a51c4de30e76e85a45a319b58aa402ea32a2d5b32dd15914f2c409e4261ece721659ce4e4791bb09ac0083398a0766ff622199f1c007f78344370f2d89df92c5
6
+ metadata.gz: 5529034ea281db641c3105b0aba812de2b7d05933816f690a9f3cade786b9a6b7c42004ce5567ec767902538f817a17fb976c8a0f20e72ce9842f8ef53b7c4d3
7
+ data.tar.gz: cfcff516d957181281939ed46af92027d2c673dd6b9035a421fda4c04118c8e7d47300727cbab8230b6fef93e8ec23c368f689244e843bb87dccf20e85791b85
data/CHANGELOG.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # Changelog
2
+
2
3
  All notable changes to this project will be documented in this file.
3
4
 
4
5
  The format is based on [Keep a Changelog (1.0.0)](https://keepachangelog.com/en/1.0.0/),
@@ -6,6 +7,19 @@ and, as of version 0.3.0 and later, this project adheres to [Semantic Versioning
6
7
 
7
8
  ## [Unreleased]
8
9
 
10
+ ## [2.0.0] - 2023-07-07
11
+ ### Added
12
+ - `Loba.ts`: `log` option to allow logging to Rails.logger (ignored if unavailable)
13
+ - `Loba.val`: `log` option to allow logging to Rails.logger (ignored if unavailable)
14
+ - SECURITY.md to specify security policy
15
+
16
+ ### Changed
17
+ - Dropped support for Ruby prior to 3.0.6
18
+ - Changed output to always write to STDOUT (regardless whether Rails is present)
19
+ - Changed to only write to Rails.logger (when present) when `log` option is set to `true`
20
+ - Updated YARD documentation for improved completeness
21
+ - Refreshed gems and dependencies
22
+
9
23
  ## [1.2.1] - 2021-09-05
10
24
  ### Added
11
25
  - Optional specs for developers to check Loba performance (no surprising issues found)
@@ -13,7 +27,7 @@ and, as of version 0.3.0 and later, this project adheres to [Semantic Versioning
13
27
  ### Changed
14
28
  - Updated README reference links
15
29
  - Updated to support Ruby 2.2.2 as minimum (retreated from 2.5 for broader support)
16
- - Refactored to seperate stripping quotes from .inspect-generated strings
30
+ - Refactored to separate stripping quotes from .inspect-generated strings
17
31
 
18
32
  ## [1.2.0] - 2021-09-05 [YANKED]
19
33
 
@@ -78,7 +92,8 @@ and, as of version 0.3.0 and later, this project adheres to [Semantic Versioning
78
92
  ### Added
79
93
  - Initial implementation
80
94
 
81
- [Unreleased]: https://github.com/rdnewman/loba/compare/v1.2.1...HEAD
95
+ [Unreleased]: https://github.com/rdnewman/loba/compare/v2.0.0...HEAD
96
+ [2.0.0]: https://github.com/rdnewman/loba/compare/v1.2.1...v2.0.0
82
97
  [1.2.1]: https://github.com/rdnewman/loba/compare/v1.2.0...v1.2.1
83
98
  [1.2.0]: https://github.com/rdnewman/loba/compare/v1.1.0...v1.2.0
84
99
  [1.1.0]: https://github.com/rdnewman/loba/compare/v1.0.0...v1.1.0
data/README.md CHANGED
@@ -1,11 +1,9 @@
1
+ # Loba
2
+
1
3
  [![Gem Version](https://badge.fury.io/rb/loba.svg)](https://badge.fury.io/rb/loba)
2
- [![Build Status](https://app.travis-ci.com/rdnewman/loba.svg?branch=main)](https://app.travis-ci.com/rdnewman/loba)
4
+ [![Build Status](https://github.com/rdnewman/loba/actions/workflows/ruby.yml/badge.svg?branch=main)](https://github.com/rdnewman/loba/actions?query=branch%3Amain++)
3
5
  [![Code Climate](https://codeclimate.com/github/rdnewman/loba/badges/gpa.svg)](https://codeclimate.com/github/rdnewman/loba)
4
6
  [![Test Coverage](https://codeclimate.com/github/rdnewman/loba/badges/coverage.svg)](https://codeclimate.com/github/rdnewman/loba/coverage)
5
- [![Inline docs](http://inch-ci.org/github/rdnewman/loba.svg?branch=master)](http://inch-ci.org/github/rdnewman/loba)
6
- [![security](https://hakiri.io/github/rdnewman/loba/main.svg)](https://hakiri.io/github/rdnewman/loba/main)
7
-
8
- # Loba
9
7
 
10
8
  ![Loba is "write" in Zulu](readme/zulu.png)
11
9
 
@@ -17,8 +15,8 @@ Easy tracing for debugging: handy methods for adding trace lines to output or Ra
17
15
 
18
16
  There are two kinds of questions I usually want to answer when trying to diagnose code behavior:
19
17
 
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?
18
+ 1. Is this spot of code being reached (or is it reached in the order I think it is)?
19
+ 1. What is the value of this variable?
22
20
 
23
21
  Loba statements are intended to be terse to minimize typing.
24
22
 
@@ -26,7 +24,9 @@ Loba statements are intended to be minimally invasive and atomic. They should no
26
24
 
27
25
  Loba statements are expected to be removed when you're done with them. No point in cluttering up production code.
28
26
 
29
- Loba will check for presence of Rails. If it's there, it'll write to `Rails.logger.debug`. If not, it'll write to STDOUT (i.e., `puts`). Loba will work equally well with or without Rails.
27
+ Loba will always write to STDOUT (i.e., `puts`).
28
+
29
+ Loba will work equally well with or without Rails. If Rails is present, in addition to STDOUT, Loba will also always write to `Rails.logger.debug`.
30
30
 
31
31
  Loba uses the [rainbow gem](https://rubygems.org/gems/rainbow) to help make trace statements more visible.
32
32
 
@@ -40,7 +40,7 @@ Outputs a timestamped notice, useful for quick traces to see the code path and e
40
40
 
41
41
  For example,
42
42
 
43
- ```
43
+ ```text
44
44
  [TIMESTAMP] #=0002, diff=93.478016, at=1451444972.970602 (in=/home/usracct/src/myapp/app/models/target.rb:55:in `some_calculation')
45
45
  ```
46
46
 
@@ -64,7 +64,7 @@ Loba.val :var_sym # the :var_sym argument is the variable or method name given
64
64
 
65
65
  For example,
66
66
 
67
- ```
67
+ ```text
68
68
  [Target.some_calculation] my_var: 54 (in /home/usracct/src/myapp/app/models/target.rb:55:in `some_calculation')
69
69
  ```
70
70
 
@@ -92,7 +92,7 @@ HelloWorld.new.hello
92
92
 
93
93
  Output:
94
94
 
95
- ```
95
+ ```text
96
96
  [TIMESTAMP] #=0001, diff=0.000463, at=1451615389.505411 (in=/home/usracct/src/lobademo/hello_world.rb:4:in 'initialize'
97
97
  [HelloWorld#hello] @x: 42 (in /home/usracct/src/loba/spec/hello_world.rb:9:in `hello')
98
98
  Hello, Charlie
@@ -107,7 +107,7 @@ The expectation is that Loba statements are just for development or test trace s
107
107
 
108
108
  `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 attempting to render anything to help minimize any impact on production code.
109
109
 
110
- 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 avoided.
110
+ 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. Note also behavior of the `log` option which defaults to `false` (introduced in v2.0.0). In general, enabling in production should be avoided. We're consenting adults.
111
111
 
112
112
  WARNING: this gem depends on the [binding_of_caller gem](https://rubygems.org/gems/binding_of_caller) -- use `:production => true` with their warning in mind:
113
113
  > **Recommended for use only in debugging situations. Do not use this in production apps.**
@@ -139,11 +139,10 @@ HelloWorld.new.hello
139
139
 
140
140
  See above Environment Notes if using with Rails.
141
141
 
142
-
143
142
  Install as below to be generally available (recommended to restrict to only local use):
144
143
 
145
144
  ```bash
146
- $ gem install loba
145
+ gem install loba
147
146
  ```
148
147
 
149
148
  To bundle, add this line to your application's Gemfile:
@@ -160,18 +159,19 @@ or for all environments (for example, if `production: true` used):
160
159
  gem 'loba', require: false
161
160
  ```
162
161
 
163
-
164
162
  And then execute:
165
163
 
166
164
  ```bash
167
- $ bundle
165
+ bundle
168
166
  ```
169
167
 
170
168
  ## Development
171
169
 
172
170
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
173
171
 
174
- 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).
172
+ To install this gem onto your local machine, run `bundle exec rake install`.
173
+
174
+ 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).
175
175
 
176
176
  ## Changelog
177
177
 
data/SECURITY.md ADDED
@@ -0,0 +1,24 @@
1
+ # Security Policy
2
+
3
+ Loba is intended for diagnostic use in development and test environments.
4
+
5
+ Loba assumes consenting adults: it provides an explicit mechanism for temporary use in
6
+ production environments. Use in production environments is NOT recommended
7
+ and should be treated as a security vulnerability. Please see the README.md for
8
+ additional information.
9
+
10
+ ## Supported Versions
11
+
12
+ | Version | Supported |
13
+ | ------- | ------------------ |
14
+ | 2.x.x | :white_check_mark: |
15
+ | < 2.0 | :x: |
16
+
17
+ ## Reporting a Vulnerability
18
+
19
+ Please report suspected security vulnerabilities via
20
+ [Github issues][def].
21
+ For urgent concerns, write to
22
+ **[richard@newmanworks.com](mailto:richard@newmanworks.com)**.
23
+
24
+ [def]: https://github.com/rdnewman/loba/issues
@@ -3,12 +3,14 @@ module Loba
3
3
  # Internal class for managing logging across Rails and non-Rails applications
4
4
  class Platform
5
5
  class << self
6
- # Returns true if Rails appears to be available
6
+ # Checks if Rails is present
7
+ # @return [Boolean] true if Rails appears to be available; otherwise, false
7
8
  def rails?
8
9
  defined?(Rails) ? true : false
9
10
  end
10
11
 
11
- # Returns true if logging is to be allowed
12
+ # Checks if logging output is permitted.
13
+ # @return [Boolean] true if logging is to be allowed; otherwise, false
12
14
  def logging_ok?(force_true = false)
13
15
  return true if force_true
14
16
  return true unless rails?
@@ -20,12 +22,23 @@ module Loba
20
22
  end
21
23
  end
22
24
 
23
- # Returns a logging mechanism appropriate for the application
25
+ # Provides logging mechanism appropriate in the application.
26
+ #
27
+ # Returned lambda takes 2 arguments:
28
+ # * arg [String] value to be output (and potentially logged)
29
+ # * force_log [Boolean] when false (default), never logs to Rails.logger;
30
+ # when true, logs to Rails.logger if present
31
+ # @return [Lambda] procedure for logging output
24
32
  def logger
25
33
  if rails? && Rails.logger.present?
26
- ->(arg) { Rails.logger.debug arg }
34
+ lambda do |arg, force_log = false|
35
+ puts arg
36
+ return unless force_log
37
+
38
+ Rails.logger.debug arg
39
+ end
27
40
  else
28
- ->(arg) { puts arg }
41
+ ->(arg, _force_log = false) { puts arg }
29
42
  end
30
43
  end
31
44
  end
@@ -15,6 +15,11 @@ module Loba
15
15
  reset!
16
16
  end
17
17
 
18
+ # Increments timestamping, including attributes `timenum` and `timewas`
19
+ # @return [Hash] timestamp details
20
+ # * :number => [Integer] incremented count of pings so far (attribute `timenum`)
21
+ # * :now => [Time] current date and time
22
+ # * :change => [Float] difference in seconds from any previous ping or reset
18
23
  def ping
19
24
  @timenum += 1
20
25
  now = Time.now
@@ -24,9 +29,13 @@ module Loba
24
29
  { number: @timenum, now: now, change: change }
25
30
  end
26
31
 
32
+ # Resets timestamping
33
+ # @return [NilClass] nil
27
34
  def reset!
28
35
  @timewas = Time.now
29
36
  @timenum = 0
37
+
38
+ nil
30
39
  end
31
40
  end
32
41
  end
@@ -54,8 +54,8 @@ module Loba
54
54
  # and a label can be inferred.
55
55
  # * If any other type, it is assumed to be a literal value to
56
56
  # and a label should be supplied when instantiated.
57
- # @param label [String] when provided, an explicit label to use; will override any
58
- # possible inferred label
57
+ # @param explicit_label [String] when provided, an explicit label to use; will
58
+ # override any possible inferred label
59
59
  #
60
60
  # @return [String] label
61
61
  def label(argument:, explicit_label: nil)
data/lib/loba/internal.rb CHANGED
@@ -3,11 +3,12 @@ require_relative 'internal/time_keeper'
3
3
  require_relative 'internal/value'
4
4
 
5
5
  module Loba
6
+ # Internal functionality support
6
7
  # @api private
7
8
  module Internal
8
9
  # Remove wrapping quotes on a string (produced by .inspect)
9
10
  #
10
- # @param argument [String] the string (assumed to be produced from calling .inspect)
11
+ # @param content [String] the string (assumed to be produced from calling .inspect)
11
12
  # to remove quotes (") that wrap a string
12
13
  #
13
14
  # @return [String, Object]
data/lib/loba/version.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  module Loba
2
- VERSION = '1.2.1'.freeze
2
+ # Semantic version number
3
+ VERSION = '2.0.0'.freeze
3
4
  end
data/lib/loba.rb CHANGED
@@ -13,6 +13,8 @@ module Loba
13
13
  # help with quick, minimalist profiling.
14
14
  # @param production [Boolean] set to true if this timestamp notice is
15
15
  # to be recorded when running in :production environment
16
+ # @param log [Boolean] when false, will not write to Rails.logger if present;
17
+ # when true, will write to Rails.logger if present
16
18
  # @return [NilClass] nil
17
19
  # @example Basic use
18
20
  # def hello
@@ -24,15 +26,21 @@ module Loba
24
26
  # Loba.ts production: true # Loba.ts is a shorthand alias for Loba.timestamp
25
27
  # end
26
28
  # #=> [TIMESTAMP] #=0001, diff=0.000463, at=1451615389.505411, in=/path/to/file.rb:2:in 'hello'
27
- def timestamp(production: false)
29
+ # @example Forced to output to log (if Rails.logger present) in addition to $STDOUT
30
+ # def hello
31
+ # Loba.timestamp log: true
32
+ # end
33
+ # #=> [TIMESTAMP] #=0001, diff=0.000463, at=1451615389.505411, in=/path/to/file.rb:2:in 'hello'
34
+ def timestamp(production: false, log: false) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
28
35
  return unless Internal::Platform.logging_ok?(production)
29
36
 
30
- # produce timestamp notice
31
- @loba_logger ||= Internal::Platform.logger
37
+ # NOTE: while tempting, memoizing loba_logger can lead to surprises if
38
+ # Rails presence isn't constant
39
+ loba_logger = Internal::Platform.logger
32
40
 
33
41
  begin
34
42
  stats = Internal::TimeKeeper.instance.ping
35
- @loba_logger.call(
43
+ loba_logger.call(
36
44
  # 60: light_black / grey
37
45
  "#{Rainbow('[TIMESTAMP]').black.bg(60)}" \
38
46
  "#{Rainbow(' #=').yellow.bg(:default)}" \
@@ -41,10 +49,11 @@ module Loba
41
49
  "#{format('%.6f', stats[:change])}" \
42
50
  "#{Rainbow(', at=').yellow}" \
43
51
  "#{format('%.6f', stats[:now].round(6).to_f)}" \
44
- "#{Rainbow(" \t(in #{caller(1..1).first})").color(60)}" # warning: nested interpolation
52
+ "#{Rainbow(" \t(in #{caller(1..1).first})").color(60)}", # warning: nested interpolation
53
+ !!log
45
54
  )
46
55
  rescue StandardError => e
47
- @loba_logger.call Rainbow("[TIMESTAMP] #=FAIL, in=#{caller(1..1).first}, err=#{e}").red
56
+ loba_logger.call Rainbow("[TIMESTAMP] #=FAIL, in=#{caller(1..1).first}, err=#{e}").red
48
57
  end
49
58
 
50
59
  nil
@@ -52,6 +61,7 @@ module Loba
52
61
  module_function :timestamp
53
62
 
54
63
  # Shorthand alias for Loba.timestamp.
64
+ # @!method ts(production: false, log: false)
55
65
  alias ts timestamp
56
66
  module_function :ts
57
67
 
@@ -65,6 +75,8 @@ module Loba
65
75
  # content being evaluated; otherwise, false
66
76
  # @param production [Boolean] set to true if this timestamp notice is
67
77
  # to be recorded when running in :production environment
78
+ # @param log [Boolean] when false, will not write to Rails.logger if present;
79
+ # when true, will write to Rails.logger if present
68
80
  # @return [NilClass] nil
69
81
  # @example Using Symbol as argument
70
82
  # class HelloWorld
@@ -96,25 +108,28 @@ module Loba
96
108
  # HelloWorld.new.hello("Charlie")
97
109
  # #=> [HelloWorld#hello] Name: Charlie (at /path/to/file/hello_world.rb:3:in `hello')
98
110
  # #=> Hello, Charlie!
99
- def value(argument, label: nil, inspect: true, production: false)
111
+ def value(argument, label: nil, inspect: true, production: false, log: false) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
100
112
  return nil unless Internal::Platform.logging_ok?(production)
101
113
 
102
- @loba_logger ||= Internal::Platform.logger
103
-
104
114
  text = Internal::Value.phrases(
105
115
  argument: (argument.nil? ? :nil : argument),
106
116
  label: label,
107
117
  inspect: inspect,
108
118
  depth_offset: 1
109
119
  )
110
- @loba_logger.call(
120
+
121
+ Internal::Platform.logger.call(
122
+ # NOTE: while tempting, memoizing Internal::Platform.logger can lead to surprises
123
+ # if Rails presence isn't constant
124
+ #
111
125
  # warning: nested interpolation below (slight help to performance)
112
126
  # 60: light_black
113
127
  # 62: light_green
114
128
  "#{Rainbow("#{text[:tag]} ").green.bg(:default)}" \
115
129
  "#{Rainbow("#{text[:label]} ").color(62)}" \
116
130
  "#{text[:value]}" \
117
- "#{Rainbow(" \t(in #{text[:line]})").color(60)}"
131
+ "#{Rainbow(" \t(in #{text[:line]})").color(60)}",
132
+ !!log
118
133
  )
119
134
 
120
135
  nil
@@ -122,6 +137,7 @@ module Loba
122
137
  module_function :value
123
138
 
124
139
  # Shorthand alias for Loba.value.
140
+ # @!method val(argument, label: nil, inspect: true, production: false, log: false)
125
141
  alias val value
126
142
  module_function :val
127
143
  end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loba
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Newman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-09-05 00:00:00.000000000 Z
11
+ date: 2023-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '2.2'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '2.2'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: binding_of_caller
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -44,14 +30,14 @@ dependencies:
44
30
  requirements:
45
31
  - - "~>"
46
32
  - !ruby/object:Gem::Version
47
- version: '3.0'
33
+ version: '3.1'
48
34
  type: :runtime
49
35
  prerelease: false
50
36
  version_requirements: !ruby/object:Gem::Requirement
51
37
  requirements:
52
38
  - - "~>"
53
39
  - !ruby/object:Gem::Version
54
- version: '3.0'
40
+ version: '3.1'
55
41
  description: Handy methods for adding trace lines to output or Rails logs.
56
42
  email:
57
43
  - richard@newmanworks.com
@@ -63,6 +49,7 @@ files:
63
49
  - CODE_OF_CONDUCT.md
64
50
  - LICENSE
65
51
  - README.md
52
+ - SECURITY.md
66
53
  - lib/loba.rb
67
54
  - lib/loba/internal.rb
68
55
  - lib/loba/internal/platform.rb
@@ -78,6 +65,7 @@ metadata:
78
65
  bug_tracker_uri: https://github.com/rdnewman/loba/issues
79
66
  changelog_uri: https://github.com/rdnewman/loba/blob/main/CHANGELOG.md
80
67
  documentation_uri: https://www.rubydoc.info/gems/loba
68
+ rubygems_mfa_required: 'true'
81
69
  post_install_message:
82
70
  rdoc_options: []
83
71
  require_paths:
@@ -86,14 +74,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
86
74
  requirements:
87
75
  - - ">="
88
76
  - !ruby/object:Gem::Version
89
- version: 2.2.2
77
+ version: 3.0.6
90
78
  required_rubygems_version: !ruby/object:Gem::Requirement
91
79
  requirements:
92
80
  - - ">="
93
81
  - !ruby/object:Gem::Version
94
82
  version: '0'
95
83
  requirements: []
96
- rubygems_version: 3.2.23
84
+ rubygems_version: 3.2.33
97
85
  signing_key:
98
86
  specification_version: 4
99
87
  summary: 'Loba: Easy tracing for debugging.'