activemodel-logger_attributes 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 03cd8fdb5370f00778fdcf3a7d4952e8857bcacb29e30041014e194f3aac3235
4
+ data.tar.gz: 729af26cecf79d7ab156b9d1154fb8de7ce2c36c96f688b1cce6ad1aaf65ab72
5
+ SHA512:
6
+ metadata.gz: 6c867469fb7c706b018120990b0799d694078807feaa4f44bd6da5ff4e430459b4bd6d2d4d817efe4a4ef2773619d8d9c71a6cd9d1e84c435c58a64d00499ee4
7
+ data.tar.gz: a378e97948ddd807d86696b7ac1536cde072d3fa626b2baed4525a02b4c871cfe4c5157768ddb292255107a74a44da3e972dd2d4ffe8fb489bb91041d9d10879
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
@@ -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 cbielinski@acorns.com. 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
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in activemodel-logger_attributes.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,52 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ activemodel-logger_attributes (0.1.0)
5
+ activemodel (> 4)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.1.4)
11
+ activesupport (= 5.1.4)
12
+ activesupport (5.1.4)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (~> 0.7)
15
+ minitest (~> 5.1)
16
+ tzinfo (~> 1.1)
17
+ awesome_print (1.8.0)
18
+ concurrent-ruby (1.0.5)
19
+ diff-lcs (1.3)
20
+ i18n (0.9.3)
21
+ concurrent-ruby (~> 1.0)
22
+ minitest (5.11.3)
23
+ rake (10.5.0)
24
+ rspec (3.7.0)
25
+ rspec-core (~> 3.7.0)
26
+ rspec-expectations (~> 3.7.0)
27
+ rspec-mocks (~> 3.7.0)
28
+ rspec-core (3.7.1)
29
+ rspec-support (~> 3.7.0)
30
+ rspec-expectations (3.7.0)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.7.0)
33
+ rspec-mocks (3.7.0)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.7.0)
36
+ rspec-support (3.7.1)
37
+ thread_safe (0.3.6)
38
+ tzinfo (1.2.5)
39
+ thread_safe (~> 0.1)
40
+
41
+ PLATFORMS
42
+ ruby
43
+
44
+ DEPENDENCIES
45
+ activemodel-logger_attributes!
46
+ awesome_print (~> 1)
47
+ bundler (~> 1.16)
48
+ rake (~> 10.0)
49
+ rspec (~> 3.0)
50
+
51
+ BUNDLED WITH
52
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Chris Bielinski
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,55 @@
1
+ # ActiveModel::LoggerAttributes
2
+
3
+ This gem provides a convenient way to save `Logger` messages to an attribute on your model. Multiple logger attributes are supported.
4
+
5
+ You may be also be interested in the (ActiveRecord version)[https://github.com/chrisb/activerecord-logger_attributes].
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'activemodel-logger_attributes'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install activemodel-logger_attributes
22
+
23
+ ## Usage
24
+
25
+ Your attribute should ideally be an `Array` or anything that responds to `<<`.
26
+
27
+ Defining the attribute is straightforward with `logger_attr`.
28
+
29
+ ```ruby
30
+ class Book
31
+ include ActiveModel::Model
32
+
33
+ logger_attr :activity_log
34
+
35
+ # expanded version with defaults:
36
+ # logger_attr :activity_log, logger_class: ::Logger, logger_name: :activity_log_logger, logger_init: -> {}
37
+ end
38
+ ```
39
+
40
+ If you prefer selectively mixing in functionality, simply `include ActiveModel::LoggerAttributes`. If you have multiple logger attributes take care to avoid clobbering logger names.
41
+
42
+ ```ruby
43
+ book = Book.new # previous log history is supported: Book.new activity_log: [..]
44
+ book.activity_log_logger.info 'Finished initializing book'
45
+ book.activity_log_logger.error 'Something went wrong!'
46
+ book.activity_log # => ["I, [2018-02-05T09:59:37.871594 #34988] INFO -- : Finished initializing book", "E, [2018-02-05T09:59:37.871960 #34988] ERROR -- : Something went wrong!"]
47
+ ```
48
+
49
+ As you would expect `book.activity_log_logger` is an instance of `Logger`.
50
+
51
+ By default, `logger_attr` will use `Logger` for the log class the logger's name; additionally, the logger `progname` will be set to your model's class name and attribute. Passing a block to `logger_init` is a convenient way to perform custom initialization on your logger's class. You may wish to override the default `progname` or perhaps set a log level:
52
+
53
+ ```ruby
54
+ logger_attr :activity_log, logger_init: ->(l) { l.level = Logger::WARN }
55
+ ```
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
@@ -0,0 +1,21 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "active_model/logger_attributes/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'activemodel-logger_attributes'
7
+ spec.version = ActiveModel::LoggerAttributes::VERSION
8
+ spec.authors = ['Chris Bielinski']
9
+ spec.email = ['chris@shadow.io']
10
+ spec.licenses = ['MIT']
11
+ spec.summary = 'Write Ruby logger messages to an ActiveModel attribute'
12
+ spec.description = 'Provides a Logger::LogDevice that can save Ruby Logger messages to an array in your model'
13
+ spec.homepage = "https://github.com/chrisb/activemodel-logger_attributes"
14
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
15
+ spec.require_paths = ["lib"]
16
+ spec.add_dependency 'activemodel', '> 4', '< 6'
17
+ spec.add_development_dependency 'awesome_print', '~> 1'
18
+ spec.add_development_dependency "bundler", "~> 1.16"
19
+ spec.add_development_dependency "rake", "~> 10.0"
20
+ spec.add_development_dependency "rspec", "~> 3.0"
21
+ end
data/bin/console ADDED
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "active_model/logger_attributes"
5
+ require "awesome_print"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ class TestModel
15
+ include ActiveModel::Model
16
+
17
+ logger_attr :log, logger_class: ::Logger, logger_name: :logger, logger_init: ->(l) { l.level = ::Logger::WARN }
18
+ end
19
+
20
+
21
+ @foo = TestModel.new log: ['D, [2018-02-05T09:22:14.343646 #27967] DEBUG -- : logger_attr lives!']
22
+ @foo.logger.debug "Debug message"
23
+ @foo.logger.info "Info message"
24
+ @foo.logger.warn "Warning message"
25
+ @foo.logger.error "Error message"
26
+
27
+ ap @foo.log
28
+
29
+ require "irb"
30
+ IRB.start(__FILE__)
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
@@ -0,0 +1,59 @@
1
+ require 'logger'
2
+ require 'active_model'
3
+ require 'active_model/logger_attributes/version'
4
+ require 'active_model/logger_attributes/device'
5
+
6
+ module ActiveModel
7
+ module LoggerAttributes
8
+ extend ActiveSupport::Concern
9
+
10
+ DEFAULT_OPTIONS = { logger_class: ::Logger, logger_name: nil, logger_init: -> {} }.freeze
11
+
12
+ InvalidAttributeValue = Class.new(RuntimeError)
13
+
14
+ module ClassMethods
15
+ def logger_attr(attribute, options = {})
16
+ attribute = attribute.to_sym
17
+ @logger_attributes ||= {}
18
+ @logger_attributes[attribute] = DEFAULT_OPTIONS.merge(options)
19
+ @logger_attributes[attribute][:logger_name] ||= "#{attribute}_logger"
20
+ define_logger_attr_initializer(attribute)
21
+ define_logger_attr_accessor(attribute)
22
+ attr_accessor(attribute)
23
+ end
24
+
25
+ def define_logger_attr_initializer(attribute)
26
+ options = @logger_attributes[attribute]
27
+ define_method(:"initialize_#{options[:logger_name]}") do
28
+ define_attr_logger_attribute(attribute)
29
+ logger = logger_for_logger_attribute(attribute, options[:logger_class], &options[:logger_init])
30
+ instance_variable_set :"@#{options[:logger_name]}", logger
31
+ end
32
+ end
33
+
34
+ def define_logger_attr_accessor(attribute)
35
+ options = @logger_attributes[attribute]
36
+ define_method(options[:logger_name]) do
37
+ instance_variable_get(:"@#{options[:logger_name]}") || send(:"initialize_#{options[:logger_name]}")
38
+ end
39
+ end
40
+ end
41
+
42
+ def logger_for_logger_attribute(attribute, logger_class, &block)
43
+ device = ActiveModel::LoggerAttributes::Device.new(attribute, self)
44
+ logger_class.new(device).tap do |l|
45
+ l.progname = "#{self.class.name}.#{attribute}" if l.respond_to?(:progname)
46
+ yield l if block_given?
47
+ end
48
+ end
49
+
50
+ # ensure the attribute itself has been initialized...
51
+ def define_attr_logger_attribute(attribute)
52
+ instance_variable_set(:"@#{attribute}", []) unless instance_variable_get(:"@#{attribute}")
53
+ return if instance_variable_get(:"@#{attribute}").respond_to?(:<<)
54
+ raise InvalidAttributeValue, "invalid type for attribute #{attribute}: #{send(attribute).class}"
55
+ end
56
+ end
57
+ end
58
+
59
+ ActiveModel::Model.send :include, ActiveModel::LoggerAttributes
@@ -0,0 +1,18 @@
1
+ module ActiveModel
2
+ module LoggerAttributes
3
+ class Device < ::Logger::LogDevice
4
+ def initialize(attribute, model)
5
+ @attribute = attribute
6
+ @model = model
7
+ end
8
+
9
+ def write(message)
10
+ @model.send(@attribute) << message.strip
11
+ end
12
+
13
+ def close
14
+ # noop
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveModel
2
+ module LoggerAttributes
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activemodel-logger_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Bielinski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">"
18
+ - !ruby/object:Gem::Version
19
+ version: '4'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">"
28
+ - !ruby/object:Gem::Version
29
+ version: '4'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6'
33
+ - !ruby/object:Gem::Dependency
34
+ name: awesome_print
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1'
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.16'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.16'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '3.0'
89
+ description: Provides a Logger::LogDevice that can save Ruby Logger messages to an
90
+ array in your model
91
+ email:
92
+ - chris@shadow.io
93
+ executables: []
94
+ extensions: []
95
+ extra_rdoc_files: []
96
+ files:
97
+ - ".gitignore"
98
+ - ".rspec"
99
+ - ".travis.yml"
100
+ - CODE_OF_CONDUCT.md
101
+ - Gemfile
102
+ - Gemfile.lock
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - activemodel-logger_attributes.gemspec
107
+ - bin/console
108
+ - bin/setup
109
+ - lib/active_model/logger_attributes.rb
110
+ - lib/active_model/logger_attributes/device.rb
111
+ - lib/active_model/logger_attributes/version.rb
112
+ homepage: https://github.com/chrisb/activemodel-logger_attributes
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.7.4
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: Write Ruby logger messages to an ActiveModel attribute
136
+ test_files: []