loba 0.2.0 → 1.2.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
- SHA1:
3
- metadata.gz: 140168f9589fcef334a886a988b973ad67a01642
4
- data.tar.gz: d07c018d2ba7208329f6d2bc428d7709411e4e3b
2
+ SHA256:
3
+ metadata.gz: 196b8d0b75303a31baa40559f2ece4bb2e53ef288f78822e2436b95926826532
4
+ data.tar.gz: c06af1fc1450f468abd679b86d35da01464e07d80ea35fc651b9180b1eef8291
5
5
  SHA512:
6
- metadata.gz: 3a6306138c2e1ec190d63d19302c5787ee4e1b617cabf15f88ea2d5699f306fd3f7c94103713b4b8595b5c51dab7ad8990a4d32d1f45844a95a03ff34094b5e5
7
- data.tar.gz: 9131d9074be00bfb1c9cc6804addb48b649e51bb3f1a3b38ce675336e4d506da44bc26647c2d8cbe1e93951b56cbee4e545e118737c3d1394f4f8ba0073634ef
6
+ metadata.gz: 5355b4ffd23f1a7d07ff28a7ecd382f5b8b6b0e103b600fa91b96233483bc60588ab108206a2e798f64084509faab69cb15aad99c87089f3a65fbebe0af48e16
7
+ data.tar.gz: a51c4de30e76e85a45a319b58aa402ea32a2d5b32dd15914f2c409e4261ece721659ce4e4791bb09ac0083398a0766ff622199f1c007f78344370f2d89df92c5
data/CHANGELOG.md ADDED
@@ -0,0 +1,88 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog (1.0.0)](https://keepachangelog.com/en/1.0.0/),
5
+ and, as of version 0.3.0 and later, this project adheres to [Semantic Versioning (2.0.0)](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [1.2.1] - 2021-09-05
10
+ ### Added
11
+ - Optional specs for developers to check Loba performance (no surprising issues found)
12
+
13
+ ### Changed
14
+ - Updated README reference links
15
+ - 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
17
+
18
+ ## [1.2.0] - 2021-09-05 [YANKED]
19
+
20
+ ## [1.1.0] - 2021-08-20
21
+ ### Added
22
+ - CHANGELOG.md to follow "Keep a Changelog" convention
23
+ - minor unit tests
24
+
25
+ ### Changed
26
+ - Replaced colorize gem with rainbow gem for MIT license purity
27
+ - "Changelog" section of README.md to refer to new CHANGELOG.md
28
+ - `Loba.ts`: "(in ...)" of notice now has a space instead of a "=" (matches `Loba.val`)
29
+ - `Loba.ts`: string continuation instead of concatenation for performance
30
+ - `Loba.val`: string continuation instead of concatenation for performance
31
+
32
+ ## [1.0.0] - 2021-08-18
33
+ ### Added
34
+ - `Loba.val`: added `Loba.value` as an alias
35
+
36
+ ### Changed
37
+ - `Loba.ts`: use Ruby 2.x-style keyword arguments instead of an options hash [BREAKING CHANGE]
38
+ - `Loba.val`: use Ruby 2.x-style keyword arguments instead of an options hash [BREAKING CHANGE]
39
+ - Updated for more recent Ruby versions (>= 2.5)
40
+ - Updated gem dependencies
41
+ - Expanded unit testing
42
+ - Refactored code to better compartmentalize for easier maintainability
43
+ - Updated user documentation, including corrections and better `production: true` warning
44
+ - Updated YARD documentation
45
+
46
+ ### Removed
47
+ - Gemnasium link from README
48
+ - Last positional argument as `true` or `false` to control presence in production environments
49
+ - Options hash support to control `production: true` and `inspect: false` [BREAKING CHANGE]
50
+
51
+ ## [0.3.1] - 2017-04-07
52
+ ### Added
53
+ - YARD documentation
54
+ - `Loba.val`: `inspect` option to control use of `.inspect` on an argument value
55
+ - `Loba.ts`: added `Loba.timestamp` as an alias
56
+
57
+ ### Changed
58
+ - Converted to options hash instead of implicit positional arguments
59
+ - `Loba.val`: nil values now display as `-nil-` instead of blank output
60
+
61
+ ### Deprecated
62
+ - Last positional argument as `true` or `false` to control presence in production environments
63
+
64
+ ## [0.3.0] - 2017-04-07 [YANKED]
65
+
66
+ ## [0.2.0] - 2016-12-02
67
+ ### Added
68
+ - Initial publication to RubyGems.org
69
+ - Gem badge to README
70
+ - Code Climate integration
71
+
72
+ ### Changed
73
+ - Improved and corrected documentation
74
+ - Various bug fixes
75
+ - Generalized for Ruby and reduced Rails centricity
76
+
77
+ ## 0.1.0 - 2016-01-06
78
+ ### Added
79
+ - Initial implementation
80
+
81
+ [Unreleased]: https://github.com/rdnewman/loba/compare/v1.2.1...HEAD
82
+ [1.2.1]: https://github.com/rdnewman/loba/compare/v1.2.0...v1.2.1
83
+ [1.2.0]: https://github.com/rdnewman/loba/compare/v1.1.0...v1.2.0
84
+ [1.1.0]: https://github.com/rdnewman/loba/compare/v1.0.0...v1.1.0
85
+ [1.0.0]: https://github.com/rdnewman/loba/compare/v0.3.1...v1.0.0
86
+ [0.3.1]: https://github.com/rdnewman/loba/compare/v0.3.0...v0.3.1
87
+ [0.3.0]: https://github.com/rdnewman/loba/compare/0.2.0...v0.3.0
88
+ [0.2.0]: https://github.com/olivierlacan/keep-a-changelog/releases/tag/0.2.0
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2015 Richard Newman
3
+ Copyright (c) 2015-2021 Richard Newman
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,13 +1,13 @@
1
- [![Dependency Status](https://gemnasium.com/rdnewman/loba.svg)](https://gemnasium.com/rdnewman/loba)
2
- [![Build Status](https://travis-ci.org/rdnewman/loba.svg?branch=master)](https://travis-ci.org/rdnewman/loba)
1
+ [![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)
3
3
  [![Code Climate](https://codeclimate.com/github/rdnewman/loba/badges/gpa.svg)](https://codeclimate.com/github/rdnewman/loba)
4
4
  [![Test Coverage](https://codeclimate.com/github/rdnewman/loba/badges/coverage.svg)](https://codeclimate.com/github/rdnewman/loba/coverage)
5
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/master.svg)](https://hakiri.io/github/rdnewman/loba/master)
6
+ [![security](https://hakiri.io/github/rdnewman/loba/main.svg)](https://hakiri.io/github/rdnewman/loba/main)
7
7
 
8
8
  # Loba
9
9
 
10
- ![Loba is "write" in zulu](readme/zulu.png)
10
+ ![Loba is "write" in Zulu](readme/zulu.png)
11
11
 
12
12
  Easy tracing for debugging: handy methods for adding trace lines to output or Rails logs.
13
13
 
@@ -17,18 +17,18 @@ Easy tracing for debugging: handy methods for adding trace lines to output or Ra
17
17
 
18
18
  There are two kinds of questions I usually want to answer when trying to diagnose code behavior:
19
19
 
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?
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?
22
22
 
23
- Loba statements are intended to be terse to minimize typing.
23
+ Loba statements are intended to be terse to minimize typing.
24
24
 
25
- Loba statements are intended to be minimally invasive and atomic. They should not have any (much) more impact than a regular `puts` or `Rails.logger.debug` statement.
25
+ Loba statements are intended to be minimally invasive and atomic. They should not have any (much) more impact than regular `puts` or `Rails.logger.debug` statements.
26
26
 
27
- Loba statements are expected to be removed when you're done with them. No point in cluttering up production code.
27
+ Loba statements are expected to be removed when you're done with them. No point in cluttering up production code.
28
28
 
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.
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.
30
30
 
31
- Loba uses the [colorize gem](https://rubygems.org/gems/colorize) to help make trace statements more visible.
31
+ Loba uses the [rainbow gem](https://rubygems.org/gems/rainbow) to help make trace statements more visible.
32
32
 
33
33
  ## Usage
34
34
 
@@ -47,9 +47,11 @@ For example,
47
47
  To invoke,
48
48
 
49
49
  ```ruby
50
- Loba.ts # no arguments, generally (see Environment Notes below)
50
+ Loba.ts # or with optional keyword arguments
51
51
  ```
52
52
 
53
+ `Loba.timestamp` is an alias for `Loba.ts`.
54
+
53
55
  You can read [more detail](readme/ts.md) on this command.
54
56
 
55
57
  #### Variable or method return inspection: `Loba.val`
@@ -75,7 +77,7 @@ require 'loba' # not generally necessary in Rails projects
75
77
  class HelloWorld
76
78
  def initialize
77
79
  @x = 42
78
- Loba.ts # see? it's easier to see what to remove later
80
+ Loba.ts # Loba statement put to the far left as a reminder to remove when done
79
81
  @y = "Charlie"
80
82
  end
81
83
 
@@ -90,9 +92,9 @@ HelloWorld.new.hello
90
92
 
91
93
  Output:
92
94
 
93
- ```
95
+ ```
94
96
  [TIMESTAMP] #=0001, diff=0.000463, at=1451615389.505411 (in=/home/usracct/src/lobademo/hello_world.rb:4:in 'initialize'
95
- [HelloWorld#hello] @x: 42 (in /home/richard/src/loba/spec/hello_world.rb:9:in `hello')
97
+ [HelloWorld#hello] @x: 42 (in /home/usracct/src/loba/spec/hello_world.rb:9:in `hello')
96
98
  Hello, Charlie
97
99
  [TIMESTAMP] #=0002, diff=0.000720, at=1451615389.506132 (in=/home/usracct/src/lobademo/hello_world.rb:11:in 'hello'
98
100
  ```
@@ -103,9 +105,14 @@ This section is only relevant in Rails environments.
103
105
 
104
106
  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
107
 
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.
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
+
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.
111
+
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
+ > **Recommended for use only in debugging situations. Do not use this in production apps.**
107
114
 
108
- 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.
115
+ 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 usually a Good Thing, if you never want them left in.
109
116
 
110
117
  If you simply install the gem for all environments, then Loba will be available in production, but you may not notice as easily if some Loba calls are unintentionally left in. Of course, if you want those statements to work in production, then you should install the gem for all environments.
111
118
 
@@ -115,12 +122,12 @@ The following is the code example snippet but always logging even in Rails produ
115
122
  class HelloWorld
116
123
  def initialize
117
124
  @x = 42
118
- Loba.ts(true)
125
+ Loba.ts production: true
119
126
  @y = "Charlie"
120
127
  end
121
128
 
122
129
  def hello
123
- Loba.val :@x, nil, true
130
+ Loba.val :@x, production: true
124
131
  puts "Hello, #{@y}" if @x == 42
125
132
  Loba.ts true
126
133
  end
@@ -132,28 +139,33 @@ HelloWorld.new.hello
132
139
 
133
140
  See above Environment Notes if using with Rails.
134
141
 
135
- Add this line to your application's Gemfile:
142
+
143
+ Install as below to be generally available (recommended to restrict to only local use):
144
+
145
+ ```bash
146
+ $ gem install loba
147
+ ```
148
+
149
+ To bundle, add this line to your application's Gemfile:
136
150
 
137
151
  ```ruby
138
152
  group :development, :test do
139
- gem 'loba', require: false, github: 'rdnewman/loba' # until I publish it on RubyGems
153
+ gem 'loba', require: false
140
154
  end
141
155
  ```
142
156
 
143
- or for all environments:
157
+ or for all environments (for example, if `production: true` used):
144
158
 
145
159
  ```ruby
146
- gem 'loba', require: false, github: 'rdnewman/loba' # until I publish it on RubyGems
160
+ gem 'loba', require: false
147
161
  ```
148
162
 
149
163
 
150
164
  And then execute:
151
165
 
152
- $ bundle
153
-
154
- Or install it yourself as:
155
-
156
- $ gem install loba
166
+ ```bash
167
+ $ bundle
168
+ ```
157
169
 
158
170
  ## Development
159
171
 
@@ -161,10 +173,13 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
161
173
 
162
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).
163
175
 
164
- ## Contributing
176
+ ## Changelog
177
+
178
+ See [CHANGELOG.md](CHANGELOG.md) for details. This gem follows semantic versioning.
165
179
 
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.
180
+ ## Contributing
167
181
 
182
+ 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
183
 
169
184
  ## License
170
185
 
@@ -0,0 +1,34 @@
1
+ module Loba
2
+ module Internal
3
+ # Internal class for managing logging across Rails and non-Rails applications
4
+ class Platform
5
+ class << self
6
+ # Returns true if Rails appears to be available
7
+ def rails?
8
+ defined?(Rails) ? true : false
9
+ end
10
+
11
+ # Returns true if logging is to be allowed
12
+ def logging_ok?(force_true = false)
13
+ return true if force_true
14
+ return true unless rails?
15
+
16
+ begin
17
+ !Rails.env.production?
18
+ rescue StandardError
19
+ true # let it attempt to log anyway
20
+ end
21
+ end
22
+
23
+ # Returns a logging mechanism appropriate for the application
24
+ def logger
25
+ if rails? && Rails.logger.present?
26
+ ->(arg) { Rails.logger.debug arg }
27
+ else
28
+ ->(arg) { puts arg }
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,33 @@
1
+ require 'singleton'
2
+
3
+ module Loba
4
+ module Internal
5
+ # Internal class for tracking time stamps; should not be used directly
6
+ # @!attribute [r] timewas
7
+ # Previous timestamped Time value
8
+ # @!attribute [r] timenum
9
+ # Count of timestamping occurances so far
10
+ class TimeKeeper
11
+ include Singleton
12
+ attr_reader :timewas, :timenum
13
+
14
+ def initialize
15
+ reset!
16
+ end
17
+
18
+ def ping
19
+ @timenum += 1
20
+ now = Time.now
21
+ change = now - @timewas
22
+ @timewas = now
23
+
24
+ { number: @timenum, now: now, change: change }
25
+ end
26
+
27
+ def reset!
28
+ @timewas = Time.now
29
+ @timenum = 0
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,124 @@
1
+ require 'binding_of_caller'
2
+
3
+ module Loba
4
+ module Internal
5
+ module Value
6
+ # Internal helper functions for Value.phrases
7
+ module ValueHelper
8
+ module_function
9
+
10
+ # Assemble display that shows the method invoking Loba
11
+ # @param depth [Integer] depth in call stack to retrieve tag from
12
+ # @return [String] tag for where Loba was invoked, wrapped in "[" and "]"
13
+ def tag(depth: 0)
14
+ delim = { class: '.', instance: '#' }
15
+ '[' \
16
+ "#{class_name(depth: depth + 1)}" \
17
+ "#{delim[method_type(depth: depth + 1)]}" \
18
+ "#{method_name(depth: depth + 1)}" \
19
+ ']'
20
+ end
21
+
22
+ # Identify source code line from where Loba was invoked
23
+ # @param depth [Integer] depth in call stack to retrieve source code line from
24
+ # @return [Integer] source code line number where Loba was invoked
25
+ def line(depth: 0)
26
+ caller[depth]
27
+ end
28
+
29
+ # Prepare display of an argument's value.
30
+ # @param argument [Symbol, Object] the value or variable for which information is
31
+ # to be retrieved
32
+ # @param inspect [Boolean] when true, force #inspect to be called against
33
+ # `argument` when evaluating
34
+ # @param depth [Integer] depth in call stack to start evaluation from
35
+ # @return [String] value representation of argument for display
36
+ def value(argument:, inspect: true, depth: 0)
37
+ val = evaluate(argument: argument, inspect: inspect, depth: depth + 1)
38
+
39
+ # #inspect adds explicit quotes to strings, so strip back off since #inspect
40
+ # always returns a String
41
+ val = Loba::Internal.unquote(val) if inspect
42
+
43
+ # make nils obvious
44
+ val.nil? ? '-nil-' : val.to_s
45
+ end
46
+
47
+ # Builds a label for display based on the argument when instantiated.
48
+ # If the argument (when instantiated) is not a symbol, it may not be possible
49
+ # to infer a label; in that case, '[unknown value]' is returned.
50
+ #
51
+ # @param argument [Symbol, Object] the value or variable for which information is
52
+ # to be retrieved
53
+ # * If a symbol, it is assumed to be a reference to a variable
54
+ # and a label can be inferred.
55
+ # * If any other type, it is assumed to be a literal value to
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
59
+ #
60
+ # @return [String] label
61
+ def label(argument:, explicit_label: nil)
62
+ text = if explicit_label.nil?
63
+ argument.is_a?(Symbol) ? "#{argument}:" : nil
64
+ elsif explicit_label.respond_to?(:strip)
65
+ s = explicit_label.strip
66
+ s += ':' unless s[-1] == ':'
67
+ s
68
+ end
69
+
70
+ text.nil? ? '[unknown value]:' : text.to_s
71
+ end
72
+
73
+ # Evaluate an arguments value from where it's bound.
74
+ # @param argument [Symbol, Object] the value or variable for which information is
75
+ # to be retrieved
76
+ # @param inspect [Boolean] when true, force #inspect to be called against
77
+ # `argument` when evaluating
78
+ # @param depth [Integer] depth in call stack to start evaluation from
79
+ # @return [Object] value of the argument when Loba was invoked
80
+ def evaluate(argument:, inspect: true, depth: 0)
81
+ return inspect ? argument.inspect : argument unless argument.is_a?(Symbol)
82
+
83
+ evaluation = binding.of_caller(depth + 1).eval(argument.to_s)
84
+ inspect ? evaluation.inspect : evaluation
85
+ end
86
+
87
+ # Prepare display of class name from where Loba was invoked
88
+ # @param depth [Integer] depth in call stack to retrieve class name from
89
+ # @return [String] name of class where Loba was invoked
90
+ def class_name(depth: 0)
91
+ m = binding.of_caller(depth + 1).eval('self.class.name')
92
+ if m.nil? || m.empty?
93
+ '<anonymous class>'
94
+ elsif m == 'Class'
95
+ binding.of_caller(depth + 1).eval('self.name')
96
+ else
97
+ m
98
+ end
99
+ end
100
+
101
+ # Prepare display of whether the method from where Loba was invoked is
102
+ # for a class or an instance
103
+ # @param depth [Integer] depth in call stack
104
+ # @return [:class] if method in call stack is a class method
105
+ # @return [:instance] if method in call stack is an instance method
106
+ def method_type(depth: 0)
107
+ if binding.of_caller(depth + 1).eval('self.class.name') == 'Class'
108
+ :class
109
+ else
110
+ :instance
111
+ end
112
+ end
113
+
114
+ # Prepare display of method name from where Loba was invoked
115
+ # @param depth [Integer] depth in call stack to retrieve method name from
116
+ # @return [Symbol, String] name of class where Loba was invoked
117
+ def method_name(depth: 0)
118
+ m = binding.of_caller(depth + 1).eval('self.send(:__method__)')
119
+ m.nil? || m.empty? ? '<anonymous method>' : m
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end