errorio 0.1.1

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
+ SHA256:
3
+ metadata.gz: 502cae76ac9417bc9e015ce437234a58a26474e97529d93382ec71f392d04239
4
+ data.tar.gz: 220e4104a31586d23be4ac26a778c0f528eea61642fddce1068319c314d9757c
5
+ SHA512:
6
+ metadata.gz: a3d9bd34ac75ed7a812ae791e50b6c335f12a7afc58797fcd620bd24b54fc37dce0bb8d9aa8c29bad483d0f7948a6ec4f6f7a03f6baff7a760f670b2db94ae8f
7
+ data.tar.gz: 562ca8487183bffab92272d0e816b9e13228af3bc9a2a30c6d5c2ba278854703f7c79d8fe2eea31ba0694dfb89bdcb745f76dbe0e23a92f0b55e962d12a0de7c
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.idea
data/.rubocop.yml ADDED
@@ -0,0 +1,28 @@
1
+ # The behavior of RuboCop can be controlled via the .rubocop.yml
2
+ # configuration file. It makes it possible to enable/disable
3
+ # certain cops (checks) and to alter their behavior if they accept
4
+ # any parameters. The file can be placed either in your home
5
+ # directory or in some project directory.
6
+ #
7
+ # RuboCop will start looking for the configuration file in the directory
8
+ # where the inspected file is and continue its way up to the root directory.
9
+ #
10
+ # See https://docs.rubocop.org/rubocop/configuration
11
+
12
+ Layout/ExtraSpacing:
13
+ AllowForAlignment: true
14
+ AllowBeforeTrailingComments: true
15
+
16
+ #Layout/HashAlignment:
17
+ # Enabled: false
18
+
19
+ # Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
20
+ # URISchemes: http, https
21
+ Layout/LineLength:
22
+ Max: 120
23
+
24
+ # Configuration parameters: EnforcedStyle.
25
+ # SupportedStyles: final_newline, final_blank_line
26
+ Layout/TrailingEmptyLines:
27
+ EnforcedStyle: final_newline
28
+ Enabled: true
data/Gemfile ADDED
@@ -0,0 +1,8 @@
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 errorio.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 12.0'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 vadym lukavyi
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,150 @@
1
+ # Errorio
2
+
3
+ Extend your models and classes with errors, warnings and other notices
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'errorio'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install errorio
20
+
21
+ ## Usage
22
+
23
+ Extend ordinary AR model with special collection of +warnings+ and +notices+
24
+
25
+ ```ruby
26
+ class Task < ApplicationRecord
27
+ include Errorio
28
+ errorionize :errors, :warnings, :notices # :errors is initialized by ActiveRecord, so it is redundant in this case
29
+
30
+ validates :name, presence: { code: :E0230 }
31
+ validate :special_characters_validation
32
+
33
+ private
34
+
35
+ def special_characters_validation
36
+ return if name =~ /^[a-z0-9]*$/i
37
+ exceptions = name.gsub(/[^a-z0-9]/i).map{ |a| "'#{a}'" }.join(','),
38
+ warnings.add(:name, :special_characters, code: :E0231,
39
+ chars: exceptions,
40
+ message: 'Special characters are not recommended for name')
41
+ end
42
+ end
43
+ ```
44
+
45
+ ```ruby
46
+ result = Task.create
47
+ result.errors.to_e
48
+ ```
49
+
50
+ returns
51
+
52
+ ```
53
+ [
54
+ {
55
+ :code=>:E0230,
56
+ :key=>:name,
57
+ :type=>:blank,
58
+ :message=>"Task Name can't be blank"
59
+ }
60
+ ]
61
+ ```
62
+
63
+ ```ruby
64
+ result = Task.create name: 'Do * now!'
65
+ result.errors.to_e # => []
66
+ ```
67
+
68
+ ```ruby
69
+ result.warnings.to_e
70
+ ```
71
+
72
+ returns
73
+
74
+ ```
75
+ [
76
+ {
77
+ :code=>:E0231,
78
+ :key=>:name,
79
+ :type=>:special_characters,
80
+ :message=>"Special characters ('*', '!') are not recommended for name"
81
+ }
82
+ ]
83
+ ```
84
+
85
+ Message for code `E0231` should be described in en.yml file
86
+
87
+ ```yaml
88
+ errorio:
89
+ messages:
90
+ E0231: "Special characters (%{chars}) are not recommended for name"
91
+ ```
92
+
93
+ Implement errors and warnings to service class
94
+
95
+ ```ruby
96
+ class Calculate
97
+ include Errorio
98
+ errorionize :errors, :warnings
99
+
100
+ def initialize(a, b)
101
+ @a = a
102
+ @b = b
103
+ end
104
+
105
+ def sum
106
+ return unless valid?
107
+ a + b
108
+ end
109
+
110
+ def valid?
111
+ return true if @a.is_a?(Numeric) && @b.is_a?(Numeric)
112
+ errors.add :base, :not_a_numeric, Errorio.by_code(:E1000A)
113
+ end
114
+ end
115
+
116
+ calc = Calculate.new(3, '1')
117
+ if (result = calc.sum)
118
+ puts result
119
+ else
120
+ puts calc.errors.to_e
121
+ end
122
+ ```
123
+
124
+ returns
125
+
126
+ ```
127
+ [
128
+ {
129
+ :code=>:E1000A,
130
+ :key=>:base,
131
+ :type=>:not_a_numeric,
132
+ :message=>"Special characters are not recommended for name"
133
+ }
134
+ ]
135
+ ```
136
+
137
+
138
+ ## Development
139
+
140
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
141
+
142
+ 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).
143
+
144
+ ## Contributing
145
+
146
+ Bug reports and pull requests are welcome on GitHub at https://github.com/p9436/errorio.
147
+
148
+ ## License
149
+
150
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler/gem_tasks'
2
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'errorio'
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
@@ -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/errorio.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'errorio/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'errorio'
7
+ spec.version = Errorio::VERSION
8
+ spec.authors = ['Vadym Lukavyi']
9
+ spec.email = ['vadym.lukavyi@gmail.com']
10
+
11
+ spec.summary = 'Extend standart functionality of ActiveRecord errors'
12
+ spec.homepage = 'https://github.com/p9436/errorio'
13
+ spec.license = 'MIT'
14
+
15
+ if spec.respond_to?(:metadata)
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = 'https://github.com/p9436/errorio'
18
+ # spec.metadata['changelog_uri'] = 'TODO: Put your gem's CHANGELOG.md URL here.'
19
+ else
20
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
21
+ 'public gem pushes.'
22
+ end
23
+
24
+ # Specify which files should be added to the gem when it is released.
25
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
27
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
28
+ end
29
+ spec.bindir = 'exe'
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ['lib']
32
+
33
+ spec.add_development_dependency 'bundler', '~> 1.17'
34
+ spec.add_development_dependency 'rake', '~> 12.0'
35
+ spec.add_development_dependency 'rubocop', '~> 12.0'
36
+ end
data/lib/errorio.rb ADDED
@@ -0,0 +1,189 @@
1
+ require 'errorio/version'
2
+ require 'errorio/details'
3
+ require 'errorio/errors'
4
+ require 'errorio/error'
5
+
6
+ # Errorio
7
+ #
8
+ # Extend your models and classes with errors, warnings and other notices
9
+ #
10
+ # Examples:
11
+ #
12
+ # Extend ordinary AR model with special collection of +warnings+ and +notices+
13
+ #
14
+ # class Task < ApplicationRecord
15
+ # include Errorio
16
+ # errorionize :errors, :warnings, :notices # :errors is initialized by ActiveRecord, so it is redundant in this case
17
+ #
18
+ # validates :name, presence: { code: :E0230 }
19
+ # validate :special_characters_validation
20
+ #
21
+ # private
22
+ #
23
+ # def special_characters_validation
24
+ # return if name =~ /^[a-z0-9]*$/i
25
+ # exceptions = name.gsub(/[^a-z0-9]/i).map{ |a| "'#{a}'" }.join(','),
26
+ # warnings.add(:name, :special_characters, code: :E0231,
27
+ # chars: exceptions,
28
+ # message: 'Special characters are not recommended for name')
29
+ # end
30
+ # end
31
+ #
32
+ # result = Task.create
33
+ # result.errors.to_e
34
+ # =>
35
+ # [
36
+ # {
37
+ # :code=>:E0230,
38
+ # :key=>:name,
39
+ # :type=>:blank,
40
+ # :message=>"Task Name can't be blank"
41
+ # }
42
+ # ]
43
+ #
44
+ # result = Task.create 'Do * now!'
45
+ # result.errors.to_e
46
+ # =>
47
+ # []
48
+ # result.warnings.to_e
49
+ # =>
50
+ # [
51
+ # {
52
+ # :code=>:E0231,
53
+ # :key=>:name,
54
+ # :type=>:special_characters,
55
+ # :message=>"Special characters ('*', '!') are not recommended for name"
56
+ # }
57
+ # ]
58
+ #
59
+ # Message should be described in en.yml file
60
+ #
61
+ # errorio:
62
+ # messages:
63
+ # E0231: "Special characters (%{chars}) are not recommended for name"
64
+ #
65
+ # Implement errors and warnings to service class
66
+ #
67
+ # class Calculate
68
+ # include Errorio
69
+ # errorionize :errors, :warnings
70
+ #
71
+ # def initialize(a, b)
72
+ # @a = a
73
+ # @b = b
74
+ # end
75
+ #
76
+ # def sum
77
+ # return unless valid?
78
+ # a + b
79
+ # end
80
+ #
81
+ # def valid?
82
+ # return true if @a.is_a?(Numeric) && @b.is_a?(Numeric)
83
+ # errors.add :base, :not_a_numeric, Errorio.by_code(:E1000A)
84
+ # end
85
+ # end
86
+ #
87
+ # calc = Calculate.new(3, '1')
88
+ # if (result = calc.sum)
89
+ # puts result
90
+ # else
91
+ # puts calc.errors.to_e
92
+ # end
93
+ #
94
+ # => # [
95
+ # {
96
+ # :code=>:E1000A,
97
+ # :key=>:base,
98
+ # :type=>:not_a_numeric,
99
+ # :message=>"Special characters are not recommended for name"
100
+ # }
101
+ # ]
102
+ #
103
+ module Errorio
104
+ class << self
105
+ # Error details with code
106
+ #
107
+ # errors.add :base, :invalid, Errorio.by_code(:E0001, user_id: 129)
108
+ def by_code(*args)
109
+ Details.by_code(*args)
110
+ end
111
+ end
112
+
113
+ def self.included(base)
114
+ base.extend ClassMethods
115
+ base.send :prepend, InstanceMethods
116
+ end
117
+
118
+ # Class-level methods
119
+ module ClassMethods
120
+ def errorionize(*collection_types)
121
+ raise unless collection_types.is_a?(Array)
122
+ @errorio_collection_types = collection_types.map(&:to_sym)
123
+ end
124
+
125
+ def errorio_collection_types
126
+ @errorio_collection_types
127
+ end
128
+ end
129
+
130
+ # Methods for targeted objects
131
+ module InstanceMethods
132
+ def initialize(*args)
133
+ errorio_initializer
134
+ super(*args)
135
+ end
136
+
137
+ def errorio_initializer
138
+ @errorio_repo = {}
139
+ self.class.errorio_collection_types.each do |e|
140
+ init_errors_variable(e)
141
+ if send(e).nil?
142
+ # Initialize
143
+ init_errors(e)
144
+ else
145
+ # Already initialized for instance, import objects to errorio
146
+ @errorio_repo[e] = send(e)
147
+ end
148
+
149
+ # Extend existing message handler
150
+ @errorio_repo[e].class.send :include, ErrorObjectsMethods unless @errorio_repo[e].class.respond_to?(:to_e)
151
+ end
152
+ end
153
+
154
+ # Add accessor if collection wasn't initialized
155
+ def init_errors_variable(e)
156
+ self.class.attr_accessor e unless respond_to?(e)
157
+ end
158
+
159
+ # Init errors class for errorio repo
160
+ def init_errors(e)
161
+ errors = Errors.new(self)
162
+ @errorio_repo[e] = errors
163
+ instance_variable_set("@#{e}", errors)
164
+ end
165
+
166
+ def errorio
167
+ @errorio_repo
168
+ end
169
+ end
170
+
171
+ # Methods for Error object
172
+ module ErrorObjectsMethods
173
+ def to_e
174
+ result = []
175
+
176
+ @errors.each do |err|
177
+ err_obj = err.options.merge(key: err.attribute, type: err.type, message: err.message)
178
+ result << err_obj
179
+ end
180
+ result
181
+ end
182
+
183
+ private
184
+
185
+ def err_to_object(err)
186
+ err.is_a?(Hash) ? err : { message: err.to_s }
187
+ end
188
+ end
189
+ end
@@ -0,0 +1,20 @@
1
+ module Errorio
2
+ # Some helpers
3
+ class Details
4
+ # Error details by code:
5
+ #
6
+ # errors.add :base, :invalid, Errorio.by_code(:E0001, user_id: 129)
7
+ # => {
8
+ # code: :E0001,
9
+ # message: "Invitation from user with ID 1823 was expired"
10
+ # invited_by: 1823
11
+ # }
12
+ def self.by_code(code, args = {})
13
+ msg = I18n.t("errorio.messages.#{code}", args)
14
+ {
15
+ code: code,
16
+ message: msg
17
+ }.merge(args)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,30 @@
1
+ module Errorio
2
+ # Error object for errors collection
3
+ class Error
4
+ def initialize(base, attribute, type, options)
5
+ @base = base
6
+ @attribute = attribute
7
+ @type = type
8
+ @options = options.symbolize_keys
9
+ end
10
+
11
+ attr_reader :base
12
+ attr_reader :attribute
13
+ attr_reader :type
14
+ attr_reader :options
15
+
16
+ def message
17
+ options[:message]
18
+ end
19
+
20
+ # Returns a full message for a given attribute.
21
+ # person.errors.full_message(:name, 'is invalid') # => "Name is invalid"
22
+ def full_message(attribute, message)
23
+ attr_name = attribute.to_s.tr('.', '_').humanize
24
+ return "#{attr_name} #{message}" if @base.nil?
25
+
26
+ attr_name = @base.class.human_attribute_name(attribute, default: attr_name)
27
+ I18n.t(:"errors.format", default: '%{attribute} %{message}', attribute: attr_name, message: message)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,46 @@
1
+ module Errorio
2
+ # Collection of error objects
3
+ class Errors
4
+ extend Forwardable
5
+ def_delegators :@errors, :size, :clear, :blank?, :empty?, :uniq!, :any?
6
+
7
+ attr_reader :errors
8
+ alias objects errors
9
+
10
+ def initialize(base = nil)
11
+ @base = base
12
+ @errors = []
13
+ end
14
+
15
+ def add(attribute, type = :invalid, **options)
16
+ error = Error.new(@base, attribute, type, **options)
17
+
18
+ @errors.append(error)
19
+
20
+ error
21
+ end
22
+
23
+ # Returns all error attribute names
24
+ #
25
+ # person.errors.messages # => {:name=>["cannot be nil", "must be specified"]}
26
+ # person.errors.attribute_names # => [:name]
27
+ def attribute_names
28
+ @errors.map(&:attribute).uniq.freeze
29
+ end
30
+ alias keys attribute_names
31
+
32
+ # Returns a full message for a given attribute.
33
+ # person.errors.full_message(:name, 'is invalid') # => "Name is invalid"
34
+ def full_message(attribute, message)
35
+ attr_name = attribute.to_s.tr('.', '_').humanize
36
+ return "#{attr_name} #{message}" if @base.nil?
37
+
38
+ attr_name = @base.class.human_attribute_name(attribute, default: attr_name)
39
+ I18n.t(:"errors.format", default: '%{attribute} %{message}', attribute: attr_name, message: message)
40
+ end
41
+
42
+ def each(&block)
43
+ @errors.each(&block)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module Errorio
2
+ VERSION = '0.1.1'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: errorio
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Vadym Lukavyi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-07-07 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.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.0'
55
+ description:
56
+ email:
57
+ - vadym.lukavyi@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rubocop.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - errorio.gemspec
71
+ - lib/errorio.rb
72
+ - lib/errorio/details.rb
73
+ - lib/errorio/error.rb
74
+ - lib/errorio/errors.rb
75
+ - lib/errorio/version.rb
76
+ homepage: https://github.com/p9436/errorio
77
+ licenses:
78
+ - MIT
79
+ metadata:
80
+ homepage_uri: https://github.com/p9436/errorio
81
+ source_code_uri: https://github.com/p9436/errorio
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubygems_version: 3.0.8
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Extend standart functionality of ActiveRecord errors
101
+ test_files: []