kazus 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 12fcb9c3694e8635ad45b26bafd7faac57ec8d3a
4
+ data.tar.gz: a1cc670ce8c4228839773e9a7ab558e0134eb349
5
+ SHA512:
6
+ metadata.gz: 0822f449140aa93522664c37ba76f9c85872b5c944a432ce758decfd67bda44b33f3eaf6801391bcb6f46c619e740d07090449bb23a437d078b8b04e39beb570
7
+ data.tar.gz: b244668c9b2dae2c5e400e71104d6e05b0df9b5c2e8593ca2b69857a558a301aa6f6b2c7d6dd24fa4b59ac18e64777dbc754558b8d0cb43bbfde163e8c779446
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at sebastianbitzer@posteo.de. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kazus.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 esBeee
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,174 @@
1
+ # Kazus gem
2
+
3
+ Kazus provides a simple logging helper. It accepts any amount or type of arguments and logs everything well readable, while including eventually given information like backtraces, validation errors of ActiveModel instances (the gem doesn't depend on rails, though) or the object's length (of an array or a hash). It is designed to not throw errors whatever the arguments are.
4
+
5
+ For example, lines like
6
+
7
+ ```ruby
8
+ Rails.logger.error "@statement has unexpected validation errors: #{@statement.errors.full_messages.inspect}"
9
+ ```
10
+
11
+ can be replaced by
12
+
13
+ ```ruby
14
+ Kazus.log :error, "Unexpected validation errors", "@statement" => @statement
15
+ ```
16
+
17
+ which logs
18
+
19
+ ```
20
+ [KAZUS|error] Unexpected validation errors -- RELATED OBJECTS: 0.0| TITLE: @statement | CLASS: Statement | ERRORS: User must exist, body can't be blank | INSPECT: #<Statement id: nil, user_id: nil, body: nil, created_at: nil, updated_at: nil> | TO_S: #<Statement:0x007f9a971683c8> |0.0
21
+ ```
22
+
23
+ or simply
24
+
25
+ ```ruby
26
+ Kazus.log @statement
27
+ ```
28
+
29
+ to log
30
+
31
+ ```
32
+ [KAZUS|debug] RELATED OBJECTS: 0| CLASS: Statement | ERRORS: User must exist, body can't be blank | INSPECT: #<Statement id: nil, user_id: nil, body: nil, created_at: nil, updated_at: nil> | TO_S: #<Statement:0x007faffcd962d8> |0
33
+ ```
34
+
35
+ ## Installation
36
+
37
+ Add this line to your application's Gemfile:
38
+
39
+ ```ruby
40
+ gem 'kazus', git: 'git://github.com/esBeee/kazus.git'
41
+ ```
42
+
43
+ And then execute:
44
+
45
+ $ bundle
46
+
47
+ ## Configuration
48
+
49
+ ### In general
50
+
51
+ You can configure kazus to use another logger than the default [Logger](http://ruby-doc.org/stdlib-2.1.0/libdoc/logger/rdoc/Logger.html). To do so, make sure to call `Kazus.configure` with a block, like
52
+
53
+ ```ruby
54
+ Kazus.configure do |config|
55
+ config.logger = Rails.logger
56
+ end
57
+ ```
58
+
59
+ to use [Rails.logger](http://guides.rubyonrails.org/debugging_rails_applications.html#what-is-the-logger-questionmark), for example.
60
+
61
+ ### In rails
62
+
63
+ If you are using rails, you can simply run
64
+
65
+ ```sh
66
+ $ rails generate kazus:install
67
+ ```
68
+
69
+ to copy an initialzer file into `config/initializers/`.
70
+
71
+ ## Usage
72
+
73
+ The provisioned methods are:
74
+
75
+ ### #Kazus.log
76
+
77
+ Can be called with an arbitrary amount of arguments without raising an exception. It doesn't make sense to call it without arguments, though. It'll log a warning in this case.
78
+
79
+ If there's only one argument given, it will be interpreted as the incident description if it's a string or else as an object to be inspected. The log level will be set to `debug` in this case.
80
+
81
+ Else, if the first argument is a valid log level, which is one of
82
+
83
+ * 0, 1, 2, 3, 4 or 5
84
+
85
+ or
86
+
87
+ * :debug, :info, :warn, :error, :fatal or :unknown
88
+
89
+ or
90
+
91
+ * "debug", "info", "warn", "error", "fatal" or "unknown"
92
+
93
+ or
94
+
95
+ * "DEBUG", "INFO", "WARN", "ERROR", "FATAL" or "UNKNOWN"
96
+
97
+ it will be interpreted as - surprise - log level. If it's anything else, every argument, including the first, will be interpreted as objects to be inspected. The log level will then default to :debug.
98
+
99
+ If the second argument is a string (while the first argument is a valid log level), it will be interpreted as the incident description (which only has a meaning for formatting of the log message). If it's anything else, it will be interpreted as an object to be inspected as all following arguments are, too.
100
+
101
+ The third and any following argument will always be interpreted as objects to be inspected.
102
+
103
+ #### Pattern
104
+
105
+ All messages include the sequence of letters ```[KAZUS|```. I use this to parse the logs for important messages. If you want to parse for certain log levels, for example ```fatal```, the pattern is ```[KAZUS|fatal]```. Same goes for any other log level named above.
106
+
107
+ ### #Kazus.s
108
+
109
+ Does almost the same as ```Kazus.log``` but instead of using the configured logger simply uses ```puts```, to log the inspection to STDOUT. Ideal for quick debugging purposes.
110
+
111
+ ## Examples
112
+
113
+ You can call ```Kazus.log``` with just an array for quick debugging purposes:
114
+
115
+ ```ruby
116
+ Kazus.log [1, "A"]
117
+ ```
118
+
119
+ will output
120
+
121
+ ```
122
+ [KAZUS|debug] RELATED OBJECTS: 0| CLASS: Array | COUNT: 2 | INSPECT: [1, "A"] |0
123
+ ```
124
+
125
+
126
+ Or you can log an unexpected exception. Kazus includes the backtrace in such cases:
127
+
128
+ ```ruby
129
+ Kazus.log :fatal, "An exception was thrown unexpectedly", exception
130
+ ```
131
+
132
+ will output
133
+
134
+ ```
135
+ [KAZUS|fatal] An exception was thrown unexpectedly -- RELATED OBJECTS: 0| CLASS: Exception | INSPECT: #<Exception: ThisException> | TO_S: ThisException | BACKTRACE: /A/k/o/llad.rb ### /B/k/o/llad.rb |0
136
+ ```
137
+
138
+
139
+ If the last given argument is a hash, kazus titles each value with its key:
140
+
141
+ ```ruby
142
+ Kazus.log "Number A" => 54, "Number B" => 32
143
+ ```
144
+
145
+ will output
146
+
147
+ ```
148
+ [KAZUS|debug] RELATED OBJECTS: 0.0| TITLE: Number A | CLASS: Fixnum | INSPECT: 54 |0.0 0.1| TITLE: Number B | CLASS: Fixnum | INSPECT: 32 |0.1
149
+ ```
150
+
151
+
152
+ If you are using rails and you call ```Kazus.log``` with a model instance, its error messages get included in the logs:
153
+
154
+ ```ruby
155
+ statement = Statement.new
156
+ statement.valid? # Generate error messages
157
+
158
+ Kazus.log :info, nil, statement
159
+ ```
160
+
161
+ will output
162
+
163
+ ```
164
+ [KAZUS|info] RELATED OBJECTS: 0| CLASS: NilClass | INSPECT: nil |0 1| CLASS: Statement | ERRORS: User must exist, body can't be blank | INSPECT: #<Statement id: nil, user_id: nil, body: nil, created_at: nil, updated_at: nil> | TO_S: #<Statement:0x007fb108353318> |1
165
+ ```
166
+
167
+ ## Contributing
168
+
169
+ Bug reports and pull requests are welcome on GitHub at https://github.com/esBeee/kazus. 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.
170
+
171
+
172
+ ## License
173
+
174
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "kazus"
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
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/kazus.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kazus/constants'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kazus"
8
+ spec.version = Kazus::VERSION
9
+ spec.authors = ["esBeee"]
10
+ spec.email = ["sebastianbitzer@posteo.de"]
11
+
12
+ spec.summary = %q{Provides a method that logs a given message along with detailed inspections of all further given objects in a well readable format.}
13
+ spec.description = %q{Kazus provides a method to log a given message along with detailed inspections of all further given objects in a well readable format. Any logger can be configured for that job. The method is designed to not throw exceptions under any circumstances, to prevent the including app from breaking even if the method wasn't used as arranged.}
14
+ spec.homepage = "https://github.com/esBeee/kazus"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Copies an initializer file into config/initializers/
3
+
4
+ Example:
5
+ rails generate kazus:install
6
+
7
+ This will create:
8
+ config/initializers/kazus.rb
@@ -0,0 +1,11 @@
1
+ module Kazus
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ def copy_initializer
7
+ template "kazus.rb", "config/initializers/kazus.rb"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ Kazus.configure do |config|
2
+ # Configure any logger you want
3
+ config.logger = Rails.logger
4
+ end
data/lib/kazus.rb ADDED
@@ -0,0 +1,121 @@
1
+ require "kazus/constants"
2
+ require "kazus/configuration"
3
+ require "kazus/incident"
4
+
5
+ module Kazus
6
+ class << self
7
+ # Logs a nicely formatted message with the configured logger or the default
8
+ # logger, respectively.
9
+ def log *args
10
+ # Make sure a logger is available if gem wasn't configured until now.
11
+ # The Configuration class defaults to the ruby Logger (STDOUT). Learn more
12
+ # in kazus/configuration.rb.
13
+ ensure_presence_of_configuration
14
+
15
+ # Check if function was called without arguments and return in that case.
16
+ if args.length == 0
17
+ error_message = %q{You are calling #Kazus.log without arguments.}
18
+ return log(:warn, error_message)
19
+ end
20
+
21
+ # Let #map_args decide what the log level will be, what incident description
22
+ # will be logged, and what objects will be inspected.
23
+ log_level, incident_description, objects_to_inspect = map_args(args)
24
+
25
+ puts_only = __callee__ == :s
26
+
27
+ # Return if the current logger has set a log level and the current
28
+ # incident's log level is beyond the logger's log level.
29
+ return if !puts_only && severity_beyond_log_level(log_level)
30
+
31
+ incident = Incident.new(
32
+ log_level: log_level,
33
+ description: incident_description,
34
+ objects_to_inspect: objects_to_inspect
35
+ )
36
+
37
+ if puts_only
38
+ puts incident.inspection
39
+ else
40
+ incident.log
41
+ end
42
+ end
43
+
44
+ alias_method :s, :log
45
+
46
+
47
+ private
48
+
49
+ # Takes an array and decides based on it, what will be the log level,
50
+ # the incident description and the objects to be inspected.
51
+ # Returns an array with those 3 parts:
52
+ # => [log_level, incident_description, objects_to_inspect]
53
+ def map_args args
54
+ # Set log level, incident description, and objects to be inspected.
55
+ #
56
+ # Note: if standardize_log_level(args[0]) is nil, the first argument isn't a valid log level.
57
+ #
58
+ # Note also, that within this line the log level gets set (for use in
59
+ # the else-block).
60
+ if args.length == 1 || (log_level=standardize_log_level(args[0])).nil?
61
+ # Log as :debug
62
+ log_level = :debug
63
+
64
+ # If the first argument is a string, use it as description.
65
+ if args[0].class == String
66
+ incident_description = args[0]
67
+
68
+ # Inspect all further objects.
69
+ objects_to_inspect = args[1..args.length] || []
70
+ else
71
+ # No incident description in this case
72
+ incident_description = nil
73
+
74
+ # Assume all objects are to be inspected.
75
+ objects_to_inspect = args
76
+ end
77
+ else
78
+ # The first argument is a valid log level. Let's check if the second argument
79
+ # is a string. It will then be interpreted as the incident description, potentially followed
80
+ # by objects to be inspected.
81
+ # If it's something else, interpret this and all following arguments as objects
82
+ # to be inspected.
83
+ if args[1].class.to_s == "String"
84
+ incident_description = args[1]
85
+ objects_to_inspect = args[2..args.length] || []
86
+ else
87
+ incident_description = nil
88
+ objects_to_inspect = args[1..args.length] || []
89
+ end
90
+ end
91
+
92
+ [log_level, incident_description, objects_to_inspect]
93
+ end
94
+
95
+ # Takes the log level as integer (0, 1, ... , 5) or as string/symbol
96
+ # (:debug, :info, :warn, :error, :fatal, :unknown) and returns the log level
97
+ # as symbol.
98
+ #
99
+ # Returns nil if given log_level is not valid.
100
+ def standardize_log_level log_level
101
+ if log_level.class == Fixnum
102
+ return nil if log_level < 0 # Prevent the next line from handing out anything in this case
103
+ LOG_LEVELS[log_level] # Returns nil if log_level > 5
104
+ else
105
+ requested_log_level_symbol = log_level.class == String ? log_level.downcase.to_sym : log_level
106
+ LOG_LEVELS.include?(requested_log_level_symbol) ? requested_log_level_symbol : nil
107
+ end
108
+ end
109
+
110
+ # Returns true if the current logger has set a log level and the given
111
+ # log level is beyond the logger's log level.
112
+ def severity_beyond_log_level log_level
113
+ loggers_log_level = standardize_log_level(Kazus.configuration.logger.level)
114
+ if loggers_log_level && LOG_LEVELS.index(log_level) < LOG_LEVELS.index(loggers_log_level)
115
+ return true
116
+ end
117
+ rescue Exception => e
118
+ # Just ignore if this logger hasn't defined a log level.
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,32 @@
1
+ # Require the default ruby logger 'Logger' in case the user didn't configure
2
+ # a logger.
3
+ require "logger"
4
+
5
+ module Kazus
6
+ # This class holds the configuration for the kazus gem.
7
+ # At the moment only the logger is configurable.
8
+ class Configuration
9
+ attr_accessor :logger
10
+
11
+ def initialize
12
+ @logger = Logger.new(STDOUT)
13
+ end
14
+ end
15
+
16
+ class << self
17
+ attr_accessor :configuration
18
+
19
+ # This method can be called by the user with a block
20
+ # so he can configure the gem.
21
+ def configure
22
+ ensure_presence_of_configuration
23
+ yield(configuration)
24
+ end
25
+
26
+ # If gem wasn't configured yet (i.e. Kazus.configuration is nil),
27
+ # initialize it with a new Configuration instance.
28
+ def ensure_presence_of_configuration
29
+ self.configuration ||= Configuration.new
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,4 @@
1
+ module Kazus
2
+ VERSION = "0.1.0"
3
+ LOG_LEVELS = [:debug, :info, :warn, :error, :fatal, :unknown] # Note that the order is important! (Ensures e.g. :warn corresponds to 2).
4
+ end
@@ -0,0 +1,60 @@
1
+ require "kazus/inspectable"
2
+
3
+ module Kazus
4
+ # Class that represents an incident to be logged. Its attributes are
5
+ #
6
+ # @log_level <-> Must be one of :debug, :info, :warn, :error, :fatal, :unknown. (To be exact: it
7
+ # has to be included in the list of log levels accesible as Kazus::LOG_LEVELS)
8
+ # @description <-> A string describing the incident or nil, if no description exists.
9
+ # @objects_to_inspect <-> Must be an array of any length that holds objects that will be inspected and logged.
10
+ #
11
+ # You might call it like this:
12
+ # Incident.new(log_level: 1, description: "Some error occured", objects_to_inspect: [])
13
+ class Incident
14
+ def initialize hsh = {}
15
+ @log_level = hsh[:log_level]
16
+ @description = hsh[:description]
17
+ @objects_to_inspect = hsh[:objects_to_inspect]
18
+ end
19
+
20
+ # Comissions the inspections of all given information and merges them
21
+ # into one string to be logged.
22
+ def log
23
+ # Log the message with the given logger.
24
+ Kazus.configuration.logger.send(@log_level, inspection)
25
+ rescue
26
+ # TODO: Whatever can be done in this case
27
+ end
28
+
29
+ def inspection
30
+ inspection = "[KAZUS|#{@log_level}]"
31
+ inspection += " " + @description if @description
32
+ inspection += objects_to_inspect_inspection # Can be a blank string if no objects are given.
33
+ end
34
+
35
+
36
+ private
37
+
38
+ # Returns the inspection of the various objects to be logged.
39
+ def objects_to_inspect_inspection
40
+ return "" if @objects_to_inspect.empty?
41
+
42
+ inspection = " "
43
+ inspection += "-- " if @description
44
+ inspection += "RELATED OBJECTS:"
45
+
46
+ # Create an inspection for each given object and add it to the collective inspection.
47
+ @objects_to_inspect.each_with_index do |object, index|
48
+ # If it's the last argument, set detailed true. In case it's a hash,
49
+ # it'll get a special treatment.
50
+ detailed = (@objects_to_inspect.length - 1 == index)
51
+
52
+ # Add individual inspection to collectice inspection.
53
+ inspection += Inspectable.new(object, index, detailed: detailed).inspection
54
+ end
55
+
56
+ # Return the collective inspection.
57
+ inspection
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,160 @@
1
+ module Kazus
2
+ # Class that represents a single object that should be inspected. Has two attributes:
3
+ #
4
+ # @object <-> The object to be inspected. Mandatory.
5
+ # @id <-> An ID in the overall collection for this object. Can be anything that implements to_s.
6
+ # @options <-> The options hash. See below for a list of implemented options.
7
+ # @data <-> A hash used internal to store all debug information to be logged in the end.
8
+ #
9
+ # Available options:
10
+ # :detailed <-> Boolean. If true, titles every key-value-pair of a hash with the key and adds a
11
+ # full inspection of the value.
12
+ #
13
+ # An instances #inspection method returns a human readable string that gives details about the object.
14
+ class Inspectable
15
+ def initialize object, id, options = {}
16
+ @object = object
17
+ @id = id
18
+ @options = options
19
+ @data = {}
20
+ end
21
+
22
+ # Returns a formatted string that gives a good insight on the object.
23
+ # Takes a title as first argument optionally, currently used in case
24
+ # this object is a value to a key of a hash (then the key is the title).
25
+ def inspection title=nil
26
+ # If this object is a hash and options[:detailed] is true,
27
+ # treat each value as another Inspectable and name the values
28
+ # with the respective keys.
29
+ if @options[:detailed] && @object.class == Hash
30
+ return detailed_hash_values
31
+ end
32
+
33
+ # The following methods all store potentially given
34
+ # information in @data.
35
+ collect_title(title)
36
+ collect_class
37
+ collect_count
38
+ collect_errors # Errors here refers to ActiveModel errors.
39
+ collect_inspect
40
+ collect_to_s
41
+ collect_stack_trace
42
+
43
+ # Return the full inspection.
44
+ " " + id_string(true) + print_data + id_string(false)
45
+ end
46
+
47
+
48
+ private
49
+
50
+ # A simple helper that stores a key-value-pair in the @data hash.
51
+ def pick_up_data name, information
52
+ @data[name] = information
53
+ end
54
+
55
+ # Prints all key-value-pairs contained in @data into a well readable string.
56
+ def print_data
57
+ printout = ""
58
+ @data.each { |name, value| printout += name + ": " + value + " | " }
59
+
60
+ # Remove superflous last 3 chars.
61
+ printout.gsub(/ \| $/, "")
62
+ end
63
+
64
+ # Treats every value as an Inspectable and titles it with its key.
65
+ # Returns the collective inspection-string of all contained key-value-pairs.
66
+ def detailed_hash_values
67
+ inspection = ""
68
+
69
+ index = 0
70
+ @object.each do |key, value|
71
+ inspection += self.class.new(value, "#{@id}.#{index}").inspection(key)
72
+ index += 1
73
+ end
74
+
75
+ inspection
76
+ end
77
+
78
+ # Hands out the given @id for the left or the right
79
+ # end of the inspection string, respectively.
80
+ def id_string right
81
+ id = @id.to_s
82
+ right ? id + "| " : " |" + id
83
+ end
84
+
85
+ # Collect the given title.
86
+ def collect_title title
87
+ if title
88
+ # If this is a symbol turn it into a well readable string
89
+ # For example ":some_value" should become "Some value".
90
+ message = title.class == Symbol ? title.to_s.gsub("_", " ").capitalize : title.to_s
91
+ pick_up_data("TITLE", message)
92
+ end
93
+ end
94
+
95
+ # Collect the object's class name.
96
+ def collect_class
97
+ pick_up_data("CLASS", @object.class.to_s)
98
+ end
99
+
100
+ # Collect the object's response to #count, if given.
101
+ def collect_count
102
+ # Rescue in case this object doesn't implement a :count method
103
+ begin
104
+ count = @object.count
105
+ pick_up_data("COUNT", @object.count.to_s)
106
+ rescue
107
+ # Don't really care if @object doesn't implement :count
108
+ end
109
+ end
110
+
111
+ # COLLECT methods - Each tries to get an information from @object
112
+ # and - if available - stores it in the @data hash.
113
+ #
114
+ # If this is an ActiveRecord instance, it might have errors that
115
+ # should be printed.
116
+ def collect_errors
117
+ # Rescue in case this object doesn't implement the required methods.
118
+ begin
119
+ error_messages = @object.errors.full_messages
120
+ message = error_messages.length == 0 ? "[]" : error_messages.join(", ")
121
+ pick_up_data("ERRORS", message)
122
+ rescue
123
+ # Don't really care.
124
+ end
125
+ end
126
+
127
+ # Collect the object's response to #to_s.
128
+ def collect_to_s
129
+ s = @object.to_s
130
+
131
+ # Don't collect if string is blank or it's the same as #inspect
132
+ # delivered.
133
+ unless s.length == 0 || @data["INSPECT"] == s
134
+ pick_up_data("TO_S", @object.to_s)
135
+ end
136
+ end
137
+
138
+ # Collect the object's response to #inspect.
139
+ def collect_inspect
140
+ s = @object.inspect
141
+
142
+ # Don't collect if string is blank or it's the same as #to_s
143
+ # delivered.
144
+ unless s.length == 0 || @data["TO_S"] == s
145
+ pick_up_data("INSPECT", @object.inspect)
146
+ end
147
+ end
148
+
149
+ # If this object has a backtrace, collect it.
150
+ def collect_stack_trace
151
+ # Rescue in case this object doesn't implement the required methods.
152
+ begin
153
+ backtrace = @object.backtrace
154
+ pick_up_data("BACKTRACE", backtrace.join(" ### "))
155
+ rescue
156
+ # Don't really care.
157
+ end
158
+ end
159
+ end
160
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kazus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - esBeee
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-07-20 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Kazus provides a method to log a given message along with detailed inspections
56
+ of all further given objects in a well readable format. Any logger can be configured
57
+ for that job. The method is designed to not throw exceptions under any circumstances,
58
+ to prevent the including app from breaking even if the method wasn't used as arranged.
59
+ email:
60
+ - sebastianbitzer@posteo.de
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".gitignore"
66
+ - ".rspec"
67
+ - ".travis.yml"
68
+ - CODE_OF_CONDUCT.md
69
+ - Gemfile
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - bin/console
74
+ - bin/setup
75
+ - kazus.gemspec
76
+ - lib/generators/kazus/USAGE
77
+ - lib/generators/kazus/install_generator.rb
78
+ - lib/generators/kazus/templates/kazus.rb
79
+ - lib/kazus.rb
80
+ - lib/kazus/configuration.rb
81
+ - lib/kazus/constants.rb
82
+ - lib/kazus/incident.rb
83
+ - lib/kazus/inspectable.rb
84
+ homepage: https://github.com/esBeee/kazus
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.5.2
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Provides a method that logs a given message along with detailed inspections
108
+ of all further given objects in a well readable format.
109
+ test_files: []
110
+ has_rdoc: