informed 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +9 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +100 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/bin/test +14 -0
- data/bin/yard +17 -0
- data/bin/yardoc +17 -0
- data/bin/yri +17 -0
- data/informed.gemspec +30 -0
- data/lib/informed/version.rb +3 -0
- data/lib/informed.rb +242 -0
- metadata +131 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 400cde4d28ec801fa0a3673a51ceee479ccaaf75
|
4
|
+
data.tar.gz: 0a4cf3e450db47d356741563f05e427628567bc2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ead6a13f3a4453df836f2ad3bf11f74265c3d57ef71be3ad56ab7fea4a5d1366c8e09108d6c59e480a1163c5eef73bb9f62f0a2922520148a4e23b84b821dedb
|
7
|
+
data.tar.gz: c50e3655490c233aac89b6f0d02e70bdc40ea535c6c99d753eb2794073cbd4a108bd07b1c0a7bab197b2b0afa1ca953cced1a1733c65a711473d2e3ab062a0ef
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at zee@zincma.de. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Zee Spencer
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
# Informed
|
2
|
+
![build status](https://travis-ci.org/zincmade/informed.svg?branch=primary)
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
gem 'informed'
|
10
|
+
```
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install informed
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Informed, when included, makes it easy to log method calls when they start and
|
23
|
+
finish. It provides a means to log additional data, such as the result of said
|
24
|
+
calls, keyword arguments, or other instance methods.
|
25
|
+
|
26
|
+
```
|
27
|
+
|
28
|
+
class FancyService
|
29
|
+
attr_accessor :fanciness
|
30
|
+
include Informed
|
31
|
+
def initialize(fanciness:)
|
32
|
+
self.fanciness = fanciness
|
33
|
+
end
|
34
|
+
|
35
|
+
def do_something(force: false)
|
36
|
+
if fancy? || force
|
37
|
+
do_it_fancy
|
38
|
+
else
|
39
|
+
do_it_plain
|
40
|
+
end
|
41
|
+
end
|
42
|
+
inform_on :do_something, level: :info,
|
43
|
+
also_log: { result: true, values: [:fancy?, :force, :fanciness]}
|
44
|
+
|
45
|
+
def fancy?
|
46
|
+
fanciness > 10
|
47
|
+
end
|
48
|
+
|
49
|
+
def do_it_plain
|
50
|
+
"so plain"
|
51
|
+
end
|
52
|
+
|
53
|
+
def do_it_fancy
|
54
|
+
"so fancy"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
FancyService.new(fanciness: 12).do_something
|
59
|
+
# I, [2017-04-04T19:46:05.256753 #29957] INFO -- : {:method=>:do_something, :values=>{:fancy?=>true, :fanciness=>12}, :status=>:starting}
|
60
|
+
# I, [2017-04-04T19:46:05.256896 #29957] INFO -- : {:method=>:do_something, :values=>{:fancy?=>true, :fanciness=>12}, :status=>:done, :result=>"so fancy"}
|
61
|
+
# => "so fancy"
|
62
|
+
FancyService.new(fanciness: 12).do_something(force: true)
|
63
|
+
# I, [2017-04-04T19:46:09.043051 #29957] INFO -- : {:method=>:do_something, :values=>{:fancy?=>true, :force=>true, :fanciness=>12}, :status=>:starting}
|
64
|
+
# I, [2017-04-04T19:46:09.043159 #29957] INFO -- : {:method=>:do_something, :values=>{:fancy?=>true, :force=>true, :fanciness=>12}, :status=>:done, :result=>"so fancy"}
|
65
|
+
# => "so fancy"
|
66
|
+
FancyService.new(fanciness: 8).do_something(force: true)
|
67
|
+
# I, [2017-04-04T19:46:17.968960 #29957] INFO -- : {:method=>:do_something, :values=>{:fancy?=>false, :force=>true, :fanciness=>8}, :status=>:starting}
|
68
|
+
# I, [2017-04-04T19:46:17.969066 #29957] INFO -- : {:method=>:do_something, :values=>{:fancy?=>false, :force=>true, :fanciness=>8}, :status=>:done, :result=>"so fancy"}
|
69
|
+
# => "so fancy"
|
70
|
+
FancyService.new(fanciness: 8).do_something(force: false)
|
71
|
+
# I, [2017-04-04T19:49:10.485462 #29957] INFO -- : {:method=>:do_something, :values=>{:fancy?=>false, :force=>false, :fanciness=>8}, :status=>:starting}
|
72
|
+
# I, [2017-04-04T19:49:10.485596 #29957] INFO -- : {:method=>:do_something, :values=>{:fancy?=>false, :force=>false, :fanciness=>8}, :status=>:done, :result=>"so plain"}
|
73
|
+
# => "so plain"
|
74
|
+
```
|
75
|
+
|
76
|
+
## Development
|
77
|
+
|
78
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
79
|
+
`bin/test` to run the tests. You can also run `bin/console` for an interactive
|
80
|
+
prompt that will allow you to experiment.
|
81
|
+
|
82
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
83
|
+
release a new version, update the version number in `version.rb`, and then run
|
84
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
85
|
+
git commits and tags, and push the `.gem` file to
|
86
|
+
[rubygems.org](https://rubygems.org).
|
87
|
+
|
88
|
+
## Contributing
|
89
|
+
|
90
|
+
Bug reports and pull requests are welcome on GitHub at
|
91
|
+
https://github.com/zincmade/informed. This project is intended to be a safe,
|
92
|
+
welcoming space for collaboration, and contributors are expected to adhere to
|
93
|
+
the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
94
|
+
|
95
|
+
|
96
|
+
## License
|
97
|
+
|
98
|
+
The gem is available as open source under the terms of the [MIT
|
99
|
+
License](http://opensource.org/licenses/MIT).
|
100
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "informed"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/bin/test
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
set -e
|
4
|
+
|
5
|
+
for version in $(rbenv versions --bare); do
|
6
|
+
export RBENV_VERSION=$version
|
7
|
+
ruby --version
|
8
|
+
ruby test/informed_test.rb
|
9
|
+
done
|
10
|
+
|
11
|
+
export RBENV_VERSION=''
|
12
|
+
ruby --version
|
13
|
+
ruby test/informed_test.rb --documentation
|
14
|
+
|
data/bin/yard
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
#
|
4
|
+
# This file was generated by Bundler.
|
5
|
+
#
|
6
|
+
# The application 'yard' is installed as part of a gem, and
|
7
|
+
# this file is here to facilitate running it.
|
8
|
+
#
|
9
|
+
|
10
|
+
require "pathname"
|
11
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
12
|
+
Pathname.new(__FILE__).realpath)
|
13
|
+
|
14
|
+
require "rubygems"
|
15
|
+
require "bundler/setup"
|
16
|
+
|
17
|
+
load Gem.bin_path("yard", "yard")
|
data/bin/yardoc
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
#
|
4
|
+
# This file was generated by Bundler.
|
5
|
+
#
|
6
|
+
# The application 'yardoc' is installed as part of a gem, and
|
7
|
+
# this file is here to facilitate running it.
|
8
|
+
#
|
9
|
+
|
10
|
+
require "pathname"
|
11
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
12
|
+
Pathname.new(__FILE__).realpath)
|
13
|
+
|
14
|
+
require "rubygems"
|
15
|
+
require "bundler/setup"
|
16
|
+
|
17
|
+
load Gem.bin_path("yard", "yardoc")
|
data/bin/yri
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
#
|
4
|
+
# This file was generated by Bundler.
|
5
|
+
#
|
6
|
+
# The application 'yri' is installed as part of a gem, and
|
7
|
+
# this file is here to facilitate running it.
|
8
|
+
#
|
9
|
+
|
10
|
+
require "pathname"
|
11
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
12
|
+
Pathname.new(__FILE__).realpath)
|
13
|
+
|
14
|
+
require "rubygems"
|
15
|
+
require "bundler/setup"
|
16
|
+
|
17
|
+
load Gem.bin_path("yard", "yri")
|
data/informed.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'informed/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "informed"
|
8
|
+
spec.version = Informed::VERSION
|
9
|
+
spec.authors = ["Zee Spencer"]
|
10
|
+
spec.email = ["zee@zincma.de"]
|
11
|
+
|
12
|
+
spec.summary = %q{Informs on method calls so you know what your app is doing and why}
|
13
|
+
spec.description = %q{You can't spell Debugging without Logging.}
|
14
|
+
spec.homepage = "https://github.com/zincmade/informed"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.metadata["yard.run"] = "yard" # use "yard" to build full HTML docs.
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
28
|
+
spec.add_development_dependency "yard", "~> 0.9"
|
29
|
+
spec.add_development_dependency "minitest-documentation", "~> 1.0"
|
30
|
+
end
|
data/lib/informed.rb
ADDED
@@ -0,0 +1,242 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require "informed/version"
|
3
|
+
|
4
|
+
# {Informed}, when included, makes it easy to log method calls when they start
|
5
|
+
# and finish. It provides a means to log additional data, such as the result of
|
6
|
+
# said calls, keyword arguments, or other instance methods.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# class FancyService
|
10
|
+
# attr_accessor :fanciness
|
11
|
+
# include Informed
|
12
|
+
# def initialize(fanciness:)
|
13
|
+
# self.fanciness = fanciness
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# def do_something(force: false)
|
17
|
+
# if fancy? || force
|
18
|
+
# do_it_fancy
|
19
|
+
# else
|
20
|
+
# do_it_plain
|
21
|
+
# end
|
22
|
+
# end
|
23
|
+
# inform_on :do_something, level: :info,
|
24
|
+
# also_log: { result: true, values: [:fancy?, :force, :fanciness]}
|
25
|
+
#
|
26
|
+
# def fancy?
|
27
|
+
# fanciness > 10
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# def do_it_plain
|
31
|
+
# "so plain"
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# def do_it_fancy
|
35
|
+
# "so fancy"
|
36
|
+
# end
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# FancyService.new(fanciness: 12).do_something
|
40
|
+
# # I, [2017-04-04T19:46:05.256753 #29957] INFO -- : {:method=>:do_something, :values=>{:fancy?=>true, :fanciness=>12}, :status=>:starting}
|
41
|
+
# # I, [2017-04-04T19:46:05.256896 #29957] INFO -- : {:method=>:do_something, :values=>{:fancy?=>true, :fanciness=>12}, :status=>:done, :result=>"so fancy"}
|
42
|
+
# # => "so fancy"
|
43
|
+
# FancyService.new(fanciness: 12).do_something(force: true)
|
44
|
+
# # I, [2017-04-04T19:46:09.043051 #29957] INFO -- : {:method=>:do_something, :values=>{:fancy?=>true, :force=>true, :fanciness=>12}, :status=>:starting}
|
45
|
+
# # I, [2017-04-04T19:46:09.043159 #29957] INFO -- : {:method=>:do_something, :values=>{:fancy?=>true, :force=>true, :fanciness=>12}, :status=>:done, :result=>"so fancy"}
|
46
|
+
# # => "so fancy"
|
47
|
+
# FancyService.new(fanciness: 8).do_something(force: true)
|
48
|
+
# # I, [2017-04-04T19:46:17.968960 #29957] INFO -- : {:method=>:do_something, :values=>{:fancy?=>false, :force=>true, :fanciness=>8}, :status=>:starting}
|
49
|
+
# # I, [2017-04-04T19:46:17.969066 #29957] INFO -- : {:method=>:do_something, :values=>{:fancy?=>false, :force=>true, :fanciness=>8}, :status=>:done, :result=>"so fancy"}
|
50
|
+
# # => "so fancy"
|
51
|
+
# FancyService.new(fanciness: 8).do_something(force: false)
|
52
|
+
# # I, [2017-04-04T19:49:10.485462 #29957] INFO -- : {:method=>:do_something, :values=>{:fancy?=>false, :force=>false, :fanciness=>8}, :status=>:starting}
|
53
|
+
# # I, [2017-04-04T19:49:10.485596 #29957] INFO -- : {:method=>:do_something, :values=>{:fancy?=>false, :force=>false, :fanciness=>8}, :status=>:done, :result=>"so plain"}
|
54
|
+
# => "so plain"
|
55
|
+
module Informed
|
56
|
+
# Mixes in the DSL to classes which include {Informed}. Defines a `logger`
|
57
|
+
# method which delegates to {Informed.logger} if the object doesn't respond
|
58
|
+
# to logger already.
|
59
|
+
# @returns nil
|
60
|
+
def self.included(informed_upon_class)
|
61
|
+
informed_upon_class.extend(DSL)
|
62
|
+
|
63
|
+
unless informed_upon_class.method_defined?(:logger)
|
64
|
+
define_method(:logger) do
|
65
|
+
Informed.logger
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# @return [Logger] The configured logger. If a logger isn't set, creates one that logs
|
71
|
+
# to STDOUT
|
72
|
+
def self.logger
|
73
|
+
@logger ||= Logger.new(STDOUT)
|
74
|
+
end
|
75
|
+
|
76
|
+
# Sets the global {Informed.logger} to whatever logger you desire.
|
77
|
+
# @param logger [Logger] An object that responds to #debug, #info, #warn, #error, #fatal and #unkonwn
|
78
|
+
# @return [Logger] Returns what it's given.
|
79
|
+
def self.logger=(logger)
|
80
|
+
@logger = logger
|
81
|
+
end
|
82
|
+
|
83
|
+
# When extended, this adds the `inform_on` class method to the class it's
|
84
|
+
# mixed in to.
|
85
|
+
module DSL
|
86
|
+
# Logs method calls to the named method
|
87
|
+
# @param method [Symbol] Name of method to inform upon
|
88
|
+
# @param level [:debug, :info, :warn, :error, :fatal, :unknown] which level
|
89
|
+
# calls to this method at.
|
90
|
+
# @param also_log [Hash] See {Informant#also_log}
|
91
|
+
def inform_on(method, level:, also_log: {})
|
92
|
+
alias_method :"unwatched_#{method}", method
|
93
|
+
informant = Informant.new(method: method, also_log: also_log, level: level)
|
94
|
+
define_method method do |*arguments, **keyword_arguments|
|
95
|
+
informant.inform_on(informee: self, logger: logger, arguments: arguments, keyword_arguments: keyword_arguments)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# Informs on method calls!
|
101
|
+
class Informant
|
102
|
+
# Which level to log informed method calls at
|
103
|
+
# @return [:debug,:info,:warn,:error,:fatal,:unknown]
|
104
|
+
attr_accessor :level
|
105
|
+
|
106
|
+
# The name of the method being informed on. This is included in all log
|
107
|
+
# messages.
|
108
|
+
# @return [Symbol]
|
109
|
+
attr_accessor :method
|
110
|
+
|
111
|
+
# What, if any, additional data to log when informing on a method.
|
112
|
+
#
|
113
|
+
# If this hash has `{ result: true }`, done messages will be logged
|
114
|
+
# with the result of the method.
|
115
|
+
#
|
116
|
+
# If the hash has `{ values: [:a_method_name, :a_keyword_arg_name] }` the
|
117
|
+
# values of the method `a_method_name` and the passed in keyword argument
|
118
|
+
# `a_keyword_arg_name` will be logged as well.
|
119
|
+
#
|
120
|
+
# @example
|
121
|
+
# inform_on :a_method, level: :debug, also_log: { result: true,
|
122
|
+
# values: [:some_method, :some_keyword_arg] }
|
123
|
+
# # This will log the result of `a_method` at the debug level, as well as
|
124
|
+
# # the value of `some_method` and the passed in keyword argumented
|
125
|
+
# # `some_keyword_arg`
|
126
|
+
#
|
127
|
+
# inform_on :another_method, level: :info
|
128
|
+
# # This will merely log that another_method was called at the info
|
129
|
+
# # level, with no additional context.
|
130
|
+
# @return [Hash]
|
131
|
+
attr_accessor :also_log
|
132
|
+
|
133
|
+
# @param method [Symbol] See {#method}
|
134
|
+
# @param also_log [Hash] See {#also_log}
|
135
|
+
# @param level [Hash] See {#level}
|
136
|
+
def initialize(method:, also_log:, level:)
|
137
|
+
self.level = level
|
138
|
+
self.method = method
|
139
|
+
# Somehow, nils are slipping in here...
|
140
|
+
self.also_log = also_log || {}
|
141
|
+
end
|
142
|
+
|
143
|
+
# @param informee [Object] The object that had the method called on it.
|
144
|
+
# @param keyword_arguments [Hash] The keyword arguments passed into the method being informed on. These may
|
145
|
+
# be logged if specified in the :values
|
146
|
+
# array in {#also_log}.
|
147
|
+
# @return the result of the informed upon method.
|
148
|
+
def inform_on(logger:, arguments:, keyword_arguments:, informee:)
|
149
|
+
method_context = { keyword_arguments: keyword_arguments, method: method, also_log: also_log, informee: informee }
|
150
|
+
log(logger: logger, type: StartingMessage, method_context: method_context)
|
151
|
+
result = if arguments.empty? && keyword_arguments.empty?
|
152
|
+
informee.send(:"unwatched_#{method}")
|
153
|
+
elsif arguments.empty? && !keyword_arguments.empty?
|
154
|
+
informee.send(:"unwatched_#{method}", **keyword_arguments)
|
155
|
+
elsif !arguments.empty? && keyword_arguments.empty?
|
156
|
+
informee.send(:"unwatched_#{method}", *arguments)
|
157
|
+
elsif !arguments.empty? && !keyword_arguments.empty?
|
158
|
+
informee.send(:"unwatched_#{method}", *arguments, **keyword_arguments)
|
159
|
+
end
|
160
|
+
log(logger: logger, type: DoneMessage, method_context: method_context.merge(result: result))
|
161
|
+
result
|
162
|
+
end
|
163
|
+
|
164
|
+
private def log(logger:, type:, method_context:)
|
165
|
+
logger.send(level, type.new(**method_context).to_h)
|
166
|
+
end
|
167
|
+
|
168
|
+
# Standard message that is logged when informed upon methods are executed.
|
169
|
+
class Message
|
170
|
+
# @return [Hash] from {Informant#inform_on}
|
171
|
+
attr_accessor :keyword_arguments
|
172
|
+
|
173
|
+
# @return [Object] from {Informant#inform_on}
|
174
|
+
attr_accessor :informee
|
175
|
+
|
176
|
+
# @return [Hash] from {Informant#also_log}
|
177
|
+
attr_accessor :also_log
|
178
|
+
|
179
|
+
# @return [Symbol] from {Informant#method}
|
180
|
+
attr_accessor :method
|
181
|
+
def initialize(method:, keyword_arguments:, informee:, also_log:)
|
182
|
+
self.keyword_arguments = keyword_arguments
|
183
|
+
self.informee = informee
|
184
|
+
self.also_log = also_log
|
185
|
+
self.method = method
|
186
|
+
end
|
187
|
+
|
188
|
+
# The message for the informant to log, in hash form. Actual return value
|
189
|
+
# depends on {#also_log}
|
190
|
+
# @return [Hash]
|
191
|
+
def to_h
|
192
|
+
message = { method: method }
|
193
|
+
if also_log[:values]
|
194
|
+
message[:values] = {}
|
195
|
+
also_log[:values].each do |local|
|
196
|
+
message[:values][local] = keyword_arguments[local] if keyword_arguments.key?(local)
|
197
|
+
message[:values][local] = informee.send(local) if informee.respond_to?(local, true)
|
198
|
+
end
|
199
|
+
end
|
200
|
+
message
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
|
205
|
+
# Message logged when an informed upon method begins to be executed.
|
206
|
+
class StartingMessage < Message
|
207
|
+
# @return [Hash] a Hash with `status: :starting` merged with {Message#to_h}
|
208
|
+
def to_h
|
209
|
+
super.merge(status: :starting)
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
# Message that's logged when an informed upon method is completed
|
214
|
+
class DoneMessage < Message
|
215
|
+
attr_accessor :result
|
216
|
+
|
217
|
+
# @param result [Object] Result of the called method. may be logged if
|
218
|
+
# {#also_log} has `result: true`
|
219
|
+
# @see Message#initialize
|
220
|
+
def initialize(result:, **kwargs)
|
221
|
+
super(**kwargs)
|
222
|
+
self.result = result
|
223
|
+
end
|
224
|
+
|
225
|
+
# @return [Hash] a Hash with `status: :done` and the result of {#message}
|
226
|
+
def to_h
|
227
|
+
done_message = { status: :done }
|
228
|
+
done_message[:result] = result if also_log[:result]
|
229
|
+
super.merge(done_message)
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
|
234
|
+
# The default data to include in a logged message. This will always include
|
235
|
+
# the {#method_name}, but may also include additional context from when the
|
236
|
+
# method was executed based upon the values of {#also_log}.
|
237
|
+
# @see #also_log
|
238
|
+
# @return [Hash]
|
239
|
+
private def message
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: informed
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Zee Spencer
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-04-22 00:00:00.000000000 Z
|
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: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: yard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.9'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.9'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: minitest-documentation
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.0'
|
83
|
+
description: You can't spell Debugging without Logging.
|
84
|
+
email:
|
85
|
+
- zee@zincma.de
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".travis.yml"
|
92
|
+
- CODE_OF_CONDUCT.md
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- bin/test
|
100
|
+
- bin/yard
|
101
|
+
- bin/yardoc
|
102
|
+
- bin/yri
|
103
|
+
- informed.gemspec
|
104
|
+
- lib/informed.rb
|
105
|
+
- lib/informed/version.rb
|
106
|
+
homepage: https://github.com/zincmade/informed
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
metadata:
|
110
|
+
yard.run: yard
|
111
|
+
post_install_message:
|
112
|
+
rdoc_options: []
|
113
|
+
require_paths:
|
114
|
+
- lib
|
115
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
requirements: []
|
126
|
+
rubyforge_project:
|
127
|
+
rubygems_version: 2.6.8
|
128
|
+
signing_key:
|
129
|
+
specification_version: 4
|
130
|
+
summary: Informs on method calls so you know what your app is doing and why
|
131
|
+
test_files: []
|