attr_statements 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.codeclimate.yml +19 -0
- data/.gitignore +12 -0
- data/.reek +19 -0
- data/.rspec +2 -0
- data/.rubocop.yml +32 -0
- data/.travis.yml +24 -0
- data/CHANGELOG.md +4 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +14 -0
- data/LICENSE.txt +21 -0
- data/README.md +144 -0
- data/Rakefile +6 -0
- data/attr_statements.gemspec +35 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/attr_statements/attribute.rb +35 -0
- data/lib/attr_statements/error_manager.rb +69 -0
- data/lib/attr_statements/validation.rb +25 -0
- data/lib/attr_statements/validations/error.rb +18 -0
- data/lib/attr_statements/validations/length.rb +35 -0
- data/lib/attr_statements/validations/presence.rb +18 -0
- data/lib/attr_statements/validations/type.rb +22 -0
- data/lib/attr_statements/version.rb +3 -0
- data/lib/attr_statements.rb +79 -0
- metadata +138 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f42f0c55d7f8fdf8f923475d25cac6177a4efb7f
|
4
|
+
data.tar.gz: a206c41b88e71118dba6de2d8a75c98faa89572d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9e7099442f5054d1fd73b35a8cc6216711b0e9b4cbcebf52462c5499efe7c263f01bedd88848c29c7833eb190d190f03387dd398d0e7b1e05df2ea00d94d3abc
|
7
|
+
data.tar.gz: ec2faf15046ab9c7d7b220a3234e1f49fa33c08ea2a82fe9ddc330e1f5631d1e400acfd22cdaeb8adbea0bf10b911393456acbfb9ac7b3374eb1e4e511244c54
|
data/.codeclimate.yml
ADDED
data/.gitignore
ADDED
data/.reek
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
---
|
2
|
+
TooManyStatements:
|
3
|
+
enabled: true
|
4
|
+
max_statements: 10
|
5
|
+
|
6
|
+
Attribute:
|
7
|
+
exclude:
|
8
|
+
- TrackerHub::Request::Config
|
9
|
+
|
10
|
+
# Directories below will not be scanned at all
|
11
|
+
exclude_paths:
|
12
|
+
- spec
|
13
|
+
|
14
|
+
IrresponsibleModule:
|
15
|
+
enabled: false
|
16
|
+
|
17
|
+
DuplicateMethodCall:
|
18
|
+
allow_calls:
|
19
|
+
- 'self.class'
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- 'spec/**/*'
|
4
|
+
|
5
|
+
Metrics/LineLength:
|
6
|
+
Max: 100
|
7
|
+
# To make it possible to copy or click on URIs in the code, we allow lines
|
8
|
+
# containing a URI to be longer than Max.
|
9
|
+
AllowHeredoc: true
|
10
|
+
AllowURI: true
|
11
|
+
URISchemes:
|
12
|
+
- http
|
13
|
+
- https
|
14
|
+
|
15
|
+
Metrics/MethodLength:
|
16
|
+
CountComments: false # count full line comments?
|
17
|
+
Max: 15
|
18
|
+
|
19
|
+
Metrics/BlockLength:
|
20
|
+
Exclude:
|
21
|
+
- '*.gemspec'
|
22
|
+
|
23
|
+
Style/Documentation:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
# fix for codeclimate
|
27
|
+
Style/PercentLiteralDelimiters:
|
28
|
+
PreferredDelimiters:
|
29
|
+
default: ()
|
30
|
+
'%i': '[]'
|
31
|
+
'%w': '[]'
|
32
|
+
'%W': '[]'
|
data/.travis.yml
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
sudo: false
|
2
|
+
language: ruby
|
3
|
+
|
4
|
+
rvm:
|
5
|
+
- 2.0.0
|
6
|
+
- 2.1.0
|
7
|
+
- 2.2.3
|
8
|
+
- 2.3.1
|
9
|
+
- 2.4.1
|
10
|
+
|
11
|
+
addons:
|
12
|
+
code_climate:
|
13
|
+
repo_token: 1c269914bbd66d1c3524fd9c13991b82d92b2902701a921954b97fbdd334df3d
|
14
|
+
|
15
|
+
before_install: gem install bundler -v 1.14.6
|
16
|
+
|
17
|
+
cache:
|
18
|
+
- bundler
|
19
|
+
|
20
|
+
script:
|
21
|
+
- bundle exec rake spec
|
22
|
+
|
23
|
+
after_success:
|
24
|
+
- bundle exec codeclimate-test-reporter
|
data/CHANGELOG.md
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 maxime.chaisseleal@gmail.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,14 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in attr_statements.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
group :development, :test do
|
7
|
+
gem 'reek', RUBY_VERSION >= '2.1.0' ? '>= 4' : '< 4', require: false
|
8
|
+
gem 'rubocop', RUBY_VERSION >= '2.0.0' ? '>= 0.48.1' : '< 0.48.1', require: false
|
9
|
+
end
|
10
|
+
|
11
|
+
group :test do
|
12
|
+
gem 'codeclimate-test-reporter', require: false if RUBY_VERSION >= '2.0'
|
13
|
+
gem 'simplecov', require: false
|
14
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Maxime Chaisse-Leal
|
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,144 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/attr_statements.svg)](http://badge.fury.io/rb/attr_statements)
|
2
|
+
[![Build Status](https://travis-ci.org/SparkHub/attr_statements.svg?branch=master)](https://travis-ci.org/SparkHub/attr_statements)
|
3
|
+
[![Code Climate](https://codeclimate.com/github/SparkHub/attr_statements/badges/gpa.svg)](https://codeclimate.com/github/SparkHub/attr_statements)
|
4
|
+
[![Test Coverage](https://codeclimate.com/github/SparkHub/attr_statements/badges/coverage.svg)](https://codeclimate.com/github/SparkHub/attr_statements/coverage)
|
5
|
+
[![Issue Count](https://codeclimate.com/github/SparkHub/attr_statements/badges/issue_count.svg)](https://codeclimate.com/github/SparkHub/attr_statements)
|
6
|
+
[![Dependency Status](https://gemnasium.com/SparkHub/attr_statements.svg)](https://gemnasium.com/SparkHub/attr_statements)
|
7
|
+
|
8
|
+
# AttrStatements
|
9
|
+
|
10
|
+
AttrStatements is a simple strong typed attributes generator on classes. It also add the ability to validate these attributes. These validations are extended with ActiveModel::Errors, so you can have a full integration with ActiveRecord.
|
11
|
+
A typical usecase will be extending a serializer with AttrStatements in order to have automatic ActiveModel::Errors management.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'attr_statements', '~> 0.1.0'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install attr_statements
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
require 'attr_statements'
|
33
|
+
|
34
|
+
class Profile
|
35
|
+
extend AttrStatements
|
36
|
+
|
37
|
+
attr_statement :email, String, presence: true, length: { maximum: 15 }
|
38
|
+
attr_statement :city, String
|
39
|
+
end
|
40
|
+
|
41
|
+
Profile.attr_statements
|
42
|
+
# => [:email, :city]
|
43
|
+
|
44
|
+
profile = Profile.new
|
45
|
+
# => #<Profile:0x007fc83511e8b0>
|
46
|
+
|
47
|
+
profile.valid?
|
48
|
+
# => false
|
49
|
+
|
50
|
+
profile.errors.any?
|
51
|
+
# => true
|
52
|
+
|
53
|
+
profile.errors
|
54
|
+
# => #<ActiveModel::Errors:0x007fc82bec5d88 @base=#<Profile:0x007fc83511e8b0 @errors=#<ActiveModel::Errors:0x007fc82bec5d88 ...>>, @messages={:email=>["can't be blank"]}>
|
55
|
+
|
56
|
+
profile.errors.full_messages
|
57
|
+
# => ["Email can't be blank"]
|
58
|
+
|
59
|
+
profile.email = 'mylongtest@email.com'
|
60
|
+
# => "mylongtest@email.com"
|
61
|
+
|
62
|
+
profile.valid?
|
63
|
+
# => false
|
64
|
+
|
65
|
+
profile.errors.full_messages
|
66
|
+
# => ["Email is too long (maximum is 15 characters)"]
|
67
|
+
|
68
|
+
profile.email = 'test@email.com'
|
69
|
+
# => 'test@email.com'
|
70
|
+
|
71
|
+
profile.valid?
|
72
|
+
# => true
|
73
|
+
|
74
|
+
profile.email = 42
|
75
|
+
# => 42
|
76
|
+
|
77
|
+
profile.valid?
|
78
|
+
# => false
|
79
|
+
|
80
|
+
profile.errors.full_messages
|
81
|
+
# => ["Email must be a String"]
|
82
|
+
```
|
83
|
+
|
84
|
+
## Bonuses
|
85
|
+
|
86
|
+
### Add translations
|
87
|
+
|
88
|
+
The `type` validation for example would need a translation (at least a default `:en`). Translation is compatible with I18n. So all we need, is a yml with:
|
89
|
+
|
90
|
+
```yaml
|
91
|
+
en:
|
92
|
+
errors:
|
93
|
+
messages:
|
94
|
+
type: 'must be a %{class}'
|
95
|
+
```
|
96
|
+
|
97
|
+
### Inject AttrStatements errors within an ActiveRecord validation process
|
98
|
+
|
99
|
+
AttrStatements errors can be easily injected within an ActiveRecord validation processus. Here is an example:
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
Class User < ActiveRecord::Base
|
103
|
+
serialize :profile_attributes, Profile
|
104
|
+
|
105
|
+
after_validation :validate_profile_attributes
|
106
|
+
|
107
|
+
private
|
108
|
+
|
109
|
+
def validate_profile_attributes
|
110
|
+
if profile_attributes && !profile_attributes.valid?
|
111
|
+
# merge child errors with parent errors
|
112
|
+
profile_attributes.errors.each do |k, v|
|
113
|
+
errors.add("profile_attribute_#{k}", v)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
```
|
119
|
+
|
120
|
+
User will now have the `:profile_attributes` errors generated by AttrStatements on `create`, `update`. (see [ActiveRecord::Validations#valid?](https://apidock.com/rails/ActiveRecord/Validations/valid%3F))
|
121
|
+
|
122
|
+
## Running tests
|
123
|
+
|
124
|
+
To run tests:
|
125
|
+
|
126
|
+
$ bundle exec rake spec
|
127
|
+
|
128
|
+
## Running CI tools
|
129
|
+
|
130
|
+
To run rubocop:
|
131
|
+
|
132
|
+
$ rubocop
|
133
|
+
|
134
|
+
To run reek:
|
135
|
+
|
136
|
+
$ reek
|
137
|
+
|
138
|
+
## Contributing
|
139
|
+
|
140
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/SparkHub/attr_statements. 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.
|
141
|
+
|
142
|
+
## License
|
143
|
+
|
144
|
+
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,35 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'attr_statements/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'attr_statements'
|
9
|
+
spec.version = AttrStatements::VERSION
|
10
|
+
spec.authors = ['Maxime Chaisse-Leal']
|
11
|
+
spec.email = ['maxime.chaisseleal@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = 'Generate attributes with strong types and validations.'
|
14
|
+
spec.description = 'Generate attributes with strong types and validations.'
|
15
|
+
spec.homepage = 'https://github.com/SparkHub/attr_statements'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.bindir = 'exe'
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ['lib']
|
24
|
+
|
25
|
+
if RUBY_VERSION >= '2.2.2'
|
26
|
+
spec.add_dependency 'activesupport', '>= 5'
|
27
|
+
else
|
28
|
+
spec.add_dependency 'activesupport', '< 5'
|
29
|
+
end
|
30
|
+
spec.add_dependency 'activemodel', '>= 4'
|
31
|
+
|
32
|
+
spec.add_development_dependency 'bundler', '~> 1.14'
|
33
|
+
spec.add_development_dependency 'rake', '~> 12.0'
|
34
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
35
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'attr_statements'
|
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,35 @@
|
|
1
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
2
|
+
require 'active_support/core_ext/hash/slice'
|
3
|
+
|
4
|
+
module AttrStatements
|
5
|
+
class Attribute
|
6
|
+
attr_reader :key
|
7
|
+
attr_reader :class_type
|
8
|
+
attr_reader :options
|
9
|
+
|
10
|
+
def presence?
|
11
|
+
options[:presence] == true
|
12
|
+
end
|
13
|
+
|
14
|
+
def length
|
15
|
+
options.fetch(:length) { {} }
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
OPTIONS = %i[presence length].freeze
|
21
|
+
private_constant :OPTIONS
|
22
|
+
|
23
|
+
attr_writer :key
|
24
|
+
attr_writer :class_type
|
25
|
+
attr_writer :options
|
26
|
+
|
27
|
+
def initialize(key, class_type, options = {})
|
28
|
+
raise ArgumentError, ':options must be a Hash' unless options.is_a?(Hash)
|
29
|
+
|
30
|
+
self.key = key.to_sym
|
31
|
+
self.class_type = class_type.is_a?(String) ? Object.const_get(class_type) : class_type
|
32
|
+
self.options = options.with_indifferent_access.slice(*Validation::VALIDATORS)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'active_support/core_ext/module/delegation'
|
2
|
+
require 'active_model/naming'
|
3
|
+
require 'active_model/translation'
|
4
|
+
require 'active_model/errors'
|
5
|
+
|
6
|
+
require_relative 'validation'
|
7
|
+
require_relative 'validations/error'
|
8
|
+
require_relative 'validations/length'
|
9
|
+
require_relative 'validations/presence'
|
10
|
+
require_relative 'validations/type'
|
11
|
+
|
12
|
+
module AttrStatements
|
13
|
+
module ErrorManager
|
14
|
+
class << self
|
15
|
+
def included(base)
|
16
|
+
base.class_eval do
|
17
|
+
extend ActiveModel::Naming
|
18
|
+
extend ActiveModel::Translation
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def errors
|
24
|
+
@errors ||= ActiveModel::Errors.new(self)
|
25
|
+
end
|
26
|
+
|
27
|
+
def valid?
|
28
|
+
errors.clear
|
29
|
+
|
30
|
+
self.class.attr_statements.each do |statement_key|
|
31
|
+
object = self.class.__send__(:get_statement_object, statement_key)
|
32
|
+
value = get_statement_value(statement_key)
|
33
|
+
|
34
|
+
validate(object, value)
|
35
|
+
end
|
36
|
+
|
37
|
+
errors.empty?
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
attr_writer :errors
|
43
|
+
|
44
|
+
# :reek:FeatureEnvy
|
45
|
+
def validate(object, value)
|
46
|
+
Validation::VALIDATORS.each do |error_type|
|
47
|
+
class_type = "AttrStatements::Validations::#{error_type.classify}"
|
48
|
+
validation_class = Object.const_get(class_type)
|
49
|
+
validation = validation_class.new(object)
|
50
|
+
|
51
|
+
add_errors(object.key, validation.errors) unless validation.valid?(value)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def add_errors(key, arr_errors)
|
56
|
+
arr_errors.each { |error| add_error(key, error) }
|
57
|
+
end
|
58
|
+
|
59
|
+
# :reek:FeatureEnvy
|
60
|
+
def add_error(key, error)
|
61
|
+
errors.add(key, error.message, error.options)
|
62
|
+
end
|
63
|
+
|
64
|
+
# The following method is needed to be minimally implemented for error management
|
65
|
+
def read_attribute_for_validation(attr)
|
66
|
+
get_statement_value(attr)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module AttrStatements
|
2
|
+
class Validation
|
3
|
+
VALIDATORS = %w[type presence length].freeze
|
4
|
+
|
5
|
+
attr_reader :errors
|
6
|
+
|
7
|
+
def valid?(value)
|
8
|
+
self.errors = validate_each(value)
|
9
|
+
errors.empty?
|
10
|
+
end
|
11
|
+
|
12
|
+
protected
|
13
|
+
|
14
|
+
attr_reader :statement
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
attr_writer :statement
|
19
|
+
attr_writer :errors
|
20
|
+
|
21
|
+
def initialize(statement)
|
22
|
+
self.statement = statement
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module AttrStatements
|
2
|
+
module Validations
|
3
|
+
class Error
|
4
|
+
attr_reader :message
|
5
|
+
attr_reader :options
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
attr_writer :message
|
10
|
+
attr_writer :options
|
11
|
+
|
12
|
+
def initialize(message, options = {})
|
13
|
+
self.message = message.to_sym
|
14
|
+
self.options = options
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module AttrStatements
|
2
|
+
module Validations
|
3
|
+
class Length < Validation
|
4
|
+
private
|
5
|
+
|
6
|
+
ERROR_TYPES = {
|
7
|
+
maximum: :too_long
|
8
|
+
}.freeze
|
9
|
+
private_constant :ERROR_TYPES
|
10
|
+
|
11
|
+
def validate_each(value)
|
12
|
+
type_errors = []
|
13
|
+
|
14
|
+
ERROR_TYPES.each do |check_key, error_message|
|
15
|
+
check_value = statement.length[check_key]
|
16
|
+
|
17
|
+
if __send__(check_key, check_value, value)
|
18
|
+
type_errors << Error.new(error_message, options(check_value))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
type_errors
|
23
|
+
end
|
24
|
+
|
25
|
+
def options(value)
|
26
|
+
{ count: value }
|
27
|
+
end
|
28
|
+
|
29
|
+
# :reek:UtilityFunction (because of metaprogramming)
|
30
|
+
def maximum(max, value)
|
31
|
+
max.is_a?(Integer) && value.is_a?(String) && value.size > max
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module AttrStatements
|
2
|
+
module Validations
|
3
|
+
class Presence < Validation
|
4
|
+
private
|
5
|
+
|
6
|
+
ERROR_KEY = :blank
|
7
|
+
private_constant :ERROR_KEY
|
8
|
+
|
9
|
+
def validate_each(value)
|
10
|
+
type_errors = []
|
11
|
+
if statement.presence? && value.blank?
|
12
|
+
type_errors << Error.new(ERROR_KEY)
|
13
|
+
end
|
14
|
+
type_errors
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module AttrStatements
|
2
|
+
module Validations
|
3
|
+
class Type < Validation
|
4
|
+
private
|
5
|
+
|
6
|
+
ERROR_KEY = :type
|
7
|
+
private_constant :ERROR_KEY
|
8
|
+
|
9
|
+
def validate_each(value)
|
10
|
+
type_errors = []
|
11
|
+
if value.present? && statement.class_type != value.class
|
12
|
+
type_errors << Error.new(ERROR_KEY, options)
|
13
|
+
end
|
14
|
+
type_errors
|
15
|
+
end
|
16
|
+
|
17
|
+
def options
|
18
|
+
{ class: statement.class_type }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'attr_statements/version'
|
2
|
+
require 'attr_statements/error_manager'
|
3
|
+
require 'attr_statements/attribute'
|
4
|
+
|
5
|
+
module AttrStatements
|
6
|
+
class << self
|
7
|
+
def included(base)
|
8
|
+
base.class_eval do
|
9
|
+
extend ClassMethods
|
10
|
+
include InstanceMethods
|
11
|
+
include AttrStatements::ErrorManager
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module InstanceMethods
|
17
|
+
private
|
18
|
+
|
19
|
+
def get_statement_value(key)
|
20
|
+
instance_variable_get("@#{key}")
|
21
|
+
end
|
22
|
+
|
23
|
+
def set_statement_value(key, value)
|
24
|
+
instance_variable_set("@#{key}", value)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
module ClassMethods
|
29
|
+
def attr_statements
|
30
|
+
@attr_statements ||= []
|
31
|
+
end
|
32
|
+
|
33
|
+
protected
|
34
|
+
|
35
|
+
attr_writer :attr_statements
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def attr_statement(key, class_type, options = {})
|
40
|
+
attr_statements << key.to_sym
|
41
|
+
|
42
|
+
define_writer_object_method(key, Attribute.new(key, class_type, options))
|
43
|
+
define_reader_object_method(key)
|
44
|
+
define_writer_instance_method(key)
|
45
|
+
define_reader_instance_method(key)
|
46
|
+
end
|
47
|
+
|
48
|
+
def define_writer_object_method(key, attribute)
|
49
|
+
set_statement_object(key, attribute)
|
50
|
+
end
|
51
|
+
|
52
|
+
def define_reader_object_method(key)
|
53
|
+
# define as singleton method
|
54
|
+
(class << self; self end).send(:define_method, key) do
|
55
|
+
get_statement_object(key)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def set_statement_object(key, value)
|
60
|
+
class_variable_set("@@#{key}", value)
|
61
|
+
end
|
62
|
+
|
63
|
+
def get_statement_object(key)
|
64
|
+
class_variable_get("@@#{key}")
|
65
|
+
end
|
66
|
+
|
67
|
+
def define_reader_instance_method(key)
|
68
|
+
define_method(key) do
|
69
|
+
get_statement_value(key)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def define_writer_instance_method(key)
|
74
|
+
define_method("#{key}=") do |value|
|
75
|
+
set_statement_value(key, value)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: attr_statements
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Maxime Chaisse-Leal
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-04-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activemodel
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.14'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.14'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '12.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '12.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
description: Generate attributes with strong types and validations.
|
84
|
+
email:
|
85
|
+
- maxime.chaisseleal@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".codeclimate.yml"
|
91
|
+
- ".gitignore"
|
92
|
+
- ".reek"
|
93
|
+
- ".rspec"
|
94
|
+
- ".rubocop.yml"
|
95
|
+
- ".travis.yml"
|
96
|
+
- CHANGELOG.md
|
97
|
+
- CODE_OF_CONDUCT.md
|
98
|
+
- Gemfile
|
99
|
+
- LICENSE.txt
|
100
|
+
- README.md
|
101
|
+
- Rakefile
|
102
|
+
- attr_statements.gemspec
|
103
|
+
- bin/console
|
104
|
+
- bin/setup
|
105
|
+
- lib/attr_statements.rb
|
106
|
+
- lib/attr_statements/attribute.rb
|
107
|
+
- lib/attr_statements/error_manager.rb
|
108
|
+
- lib/attr_statements/validation.rb
|
109
|
+
- lib/attr_statements/validations/error.rb
|
110
|
+
- lib/attr_statements/validations/length.rb
|
111
|
+
- lib/attr_statements/validations/presence.rb
|
112
|
+
- lib/attr_statements/validations/type.rb
|
113
|
+
- lib/attr_statements/version.rb
|
114
|
+
homepage: https://github.com/SparkHub/attr_statements
|
115
|
+
licenses:
|
116
|
+
- MIT
|
117
|
+
metadata: {}
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options: []
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
requirements: []
|
133
|
+
rubyforge_project:
|
134
|
+
rubygems_version: 2.6.11
|
135
|
+
signing_key:
|
136
|
+
specification_version: 4
|
137
|
+
summary: Generate attributes with strong types and validations.
|
138
|
+
test_files: []
|