unique_item_attributes_validator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4674aaffca4b2e58420502a8938a6aa249c23d52
4
+ data.tar.gz: ecdeb2d50586d7571ba78201f28d007e4ccfba34
5
+ SHA512:
6
+ metadata.gz: 8b761fb73f39dc4012fd3b099bcc2f5396a20488a92abbfe17617c7eaa5cd40c8153e03963834a9c2c4ed43ad7f312ad4806fc966add6b66d92d4e09525a612b
7
+ data.tar.gz: 71578f3d5ecffd277b04b53ee7da8a29f5414dcb4b131db5262b671764e17c96971c9bf77778c927b900b1108a52e1499e23c73a2624af765f54149497e8f9a1
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,11 @@
1
+ ruby:
2
+ enabled: true
3
+ config_file: .rubocop.yml
4
+
5
+ scss:
6
+ enabled: false
7
+
8
+ jshint:
9
+ enabled: false
10
+
11
+ fail_on_violations: true
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,275 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'db/schema.rb'
4
+ - 'config/**/*'
5
+ - 'script/**/*'
6
+ - 'db/migrate/*'
7
+ TargetRubyVersion: 2.3
8
+
9
+ AccessorMethodName:
10
+ Enabled: false
11
+
12
+ ActionFilter:
13
+ Enabled: false
14
+
15
+ Alias:
16
+ Enabled: false
17
+
18
+ ArrayJoin:
19
+ Enabled: false
20
+
21
+ AsciiComments:
22
+ Enabled: false
23
+
24
+ AsciiIdentifiers:
25
+ Enabled: false
26
+
27
+ Attr:
28
+ Enabled: false
29
+
30
+ BlockNesting:
31
+ Enabled: false
32
+
33
+ CaseEquality:
34
+ Enabled: false
35
+
36
+ CharacterLiteral:
37
+ Enabled: false
38
+
39
+ ClassAndModuleChildren:
40
+ Enabled: false
41
+
42
+ ClassLength:
43
+ Enabled: false
44
+
45
+ ClassVars:
46
+ Enabled: false
47
+
48
+ CollectionMethods:
49
+ PreferredMethods:
50
+ find: detect
51
+ reduce: inject
52
+ collect: map
53
+ find_all: select
54
+
55
+ ColonMethodCall:
56
+ Enabled: false
57
+
58
+ CommentAnnotation:
59
+ Enabled: false
60
+
61
+ CyclomaticComplexity:
62
+ Enabled: false
63
+
64
+ Delegate:
65
+ Enabled: false
66
+
67
+ Documentation:
68
+ Enabled: false
69
+
70
+ DotPosition:
71
+ EnforcedStyle: trailing
72
+
73
+ DoubleNegation:
74
+ Enabled: false
75
+
76
+ EachWithObject:
77
+ Enabled: false
78
+
79
+ EmptyLiteral:
80
+ Enabled: false
81
+
82
+ Encoding:
83
+ Enabled: false
84
+
85
+ EvenOdd:
86
+ Enabled: false
87
+
88
+ FileName:
89
+ Enabled: false
90
+
91
+ FlipFlop:
92
+ Enabled: false
93
+
94
+ FormatString:
95
+ Enabled: false
96
+
97
+ GlobalVars:
98
+ Enabled: false
99
+
100
+ GuardClause:
101
+ Enabled: false
102
+
103
+ IfUnlessModifier:
104
+ Enabled: false
105
+
106
+ IfWithSemicolon:
107
+ Enabled: false
108
+
109
+ InlineComment:
110
+ Enabled: false
111
+
112
+ Lambda:
113
+ Enabled: false
114
+
115
+ LambdaCall:
116
+ Enabled: false
117
+
118
+ LineEndConcatenation:
119
+ Enabled: false
120
+
121
+ LineLength:
122
+ Max: 120
123
+
124
+ MethodLength:
125
+ Enabled: false
126
+
127
+ ModuleFunction:
128
+ Enabled: false
129
+
130
+ NegatedIf:
131
+ Enabled: false
132
+
133
+ NegatedWhile:
134
+ Enabled: false
135
+
136
+ Next:
137
+ Enabled: false
138
+
139
+ NilComparison:
140
+ Enabled: false
141
+
142
+ Not:
143
+ Enabled: false
144
+
145
+ NumericLiterals:
146
+ Enabled: false
147
+
148
+ OneLineConditional:
149
+ Enabled: false
150
+
151
+ OpMethod:
152
+ Enabled: false
153
+
154
+ ParameterLists:
155
+ Enabled: false
156
+
157
+ PercentLiteralDelimiters:
158
+ Enabled: false
159
+
160
+ PerlBackrefs:
161
+ Enabled: false
162
+
163
+ PredicateName:
164
+ NamePrefixBlacklist:
165
+ - is_
166
+
167
+ Proc:
168
+ Enabled: false
169
+
170
+ RaiseArgs:
171
+ Enabled: false
172
+
173
+ RegexpLiteral:
174
+ Enabled: false
175
+
176
+ SelfAssignment:
177
+ Enabled: false
178
+
179
+ SingleLineBlockParams:
180
+ Enabled: false
181
+
182
+ SingleLineMethods:
183
+ Enabled: false
184
+
185
+ SignalException:
186
+ Enabled: false
187
+
188
+ SpecialGlobalVars:
189
+ Enabled: false
190
+
191
+ StringLiterals:
192
+ Enabled: false
193
+
194
+ VariableInterpolation:
195
+ Enabled: false
196
+
197
+ TrailingCommaInLiteral:
198
+ Enabled: false
199
+
200
+ TrailingCommaInArguments:
201
+ Enabled: false
202
+
203
+ TrivialAccessors:
204
+ Enabled: false
205
+
206
+ VariableInterpolation:
207
+ Enabled: false
208
+
209
+ WhenThen:
210
+ Enabled: false
211
+
212
+ WhileUntilModifier:
213
+ Enabled: false
214
+
215
+ WordArray:
216
+ Enabled: false
217
+
218
+ # Lint
219
+
220
+ AmbiguousOperator:
221
+ Enabled: false
222
+
223
+ AmbiguousRegexpLiteral:
224
+ Enabled: false
225
+
226
+ AssignmentInCondition:
227
+ Enabled: false
228
+
229
+ ConditionPosition:
230
+ Enabled: false
231
+
232
+ DeprecatedClassMethods:
233
+ Enabled: false
234
+
235
+ ElseLayout:
236
+ Enabled: false
237
+
238
+ HandleExceptions:
239
+ Enabled: false
240
+
241
+ InvalidCharacterLiteral:
242
+ Enabled: false
243
+
244
+ LiteralInCondition:
245
+ Enabled: false
246
+
247
+ LiteralInInterpolation:
248
+ Enabled: false
249
+
250
+ Loop:
251
+ Enabled: false
252
+
253
+ ParenthesesAsGroupedExpression:
254
+ Enabled: false
255
+
256
+ RequireParentheses:
257
+ Enabled: false
258
+
259
+ UnderscorePrefixedVariableName:
260
+ Enabled: false
261
+
262
+ Void:
263
+ Enabled: false
264
+
265
+ # Style
266
+
267
+ Style/IndentationWidth:
268
+ # Number of spaces for each indentation level.
269
+ Width: 2
270
+
271
+ Style/FrozenStringLiteralComment:
272
+ Enabled: false
273
+
274
+ Style/PreferredHashMethods:
275
+ Enabled: true
@@ -0,0 +1 @@
1
+ 2.3.0
@@ -0,0 +1,3 @@
1
+ SimpleCov.start do
2
+ add_group "lib", "lib"
3
+ end
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ sudo: false
3
+ cache:
4
+ bundler: true
5
+ notifications:
6
+ email:
7
+ - diego.domingues16@gmail.com, rogerio.alcantara@gmail.com
8
+ slack:
9
+ secure: IPOo1MyOQqAHAemn+Y0KwchRn0858HiZit/ixrLGKhEDmyZtHWK2FIqJzKuaOYRqKYnG5hlml/dIE2R+Sbe5fPTke1xzPg1V1LSFk+55y3ypqHGKU43POnipXc7HF7w6v2INktodOwrTDucC7ABD7MXeOSJ7QJvv+aRvK8RmL56RC4uAJiPs6EqxRGYR8LymH6AOmnDiioe1pywgp47T/7X2svLBl0D98CYrJ9SiKxK/VyJlx2dZhlhaoDQHkYk0khYzxlAt3TevTGzsaIIlbVBOLsBHni4iq8n1HSANXaVrLBGVqYRCGBJBmLDWjGdsHW7YhT7XUoeIW/c4uy02pmPKfIxJyRj5Gr1kxhMkpJvS8EDyssFo9n7I/MUrGOwa+coaucz7GrO3RcmnNB5WdBEtTKP9zJo0FIkkBtOMmZdMKxlXUgwz1tBsYj7ovV3939n51yEK6w68w2Vg1pBAM3J4RYRudEO4+3LhJZ6b6w+XQgXf+ybYvN+lrAgleQobbwRopH7Cqn5Ej0r4WrONpZrxvtsQ1BGojPPz7UpE3ZZab7hDUMgLNOTiRgw7GK6oqqsC8EGlnfpfGtUSjuneawmTRozIk7DOIMek3mCY36+rJGNqs21kZf+KBIDT9sNUTsgpsG/XKOtHhw5cxEXYE/7qdIq9X5NP9bcOuqv4/u8=
10
+ branches:
11
+ only:
12
+ - master
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ ruby "2.3.0"
4
+
5
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Rogério Rodrigues de Alcântara
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.
@@ -0,0 +1,87 @@
1
+ # unique_item_attributes_validator
2
+ [![Build Status](https://travis-ci.org/equalize-squad/unique_item_attributes_validator.svg?branch=master)](https://travis-ci.org/equalize-squad/unique_item_attributes_validator)
3
+ [![codecov](https://codecov.io/gh/equalize-squad/unique_item_attributes_validator/branch/master/graph/badge.svg)](https://codecov.io/gh/equalize-squad/unique_item_attributes_validator)
4
+
5
+ A simple validator to verify the uniqueness of certain attributes from a collection.
6
+ Should be compatible with all latest Rubies.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem "unique_item_attributes_validator"
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install unique_item_attributes_validator
23
+
24
+ ## Usage
25
+
26
+ Let's say you've got a simple hero model:
27
+
28
+ ```ruby
29
+ class Hero
30
+ include ActiveModel::Validations
31
+
32
+ attr_accessor :alter_ego, :name, :superpower
33
+
34
+ def initialize(alter_ego:, name:, superpower:)
35
+ @alter_ego = alter_ego
36
+ @name = name
37
+ @superpower = superpower
38
+ end
39
+ end
40
+ ```
41
+
42
+ And you want to form a super team with a collection of heroes. Then..
43
+
44
+ ```ruby
45
+ class SuperTeam
46
+ include ActiveModel::Validations
47
+
48
+ attr_accessor :heroes
49
+
50
+ def initialize(heroes:)
51
+ @heroes = heroes
52
+ end
53
+ end
54
+ ```
55
+
56
+ But the `alter_ego` and the `name` attributes must be unique for the heroes of this collection. So..
57
+
58
+ ```ruby
59
+ class SuperTeam
60
+ include ActiveModel::Validations
61
+
62
+ attr_accessor :heroes
63
+
64
+ validates :heroes, unique_item_attributes: [:alter_ego, :name] #YAY!
65
+
66
+ def initialize(heroes:)
67
+ @heroes = heroes
68
+ end
69
+ end
70
+ ```
71
+
72
+ And that's pretty much it! 😁
73
+
74
+ ## Development
75
+
76
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
77
+
78
+ 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).
79
+
80
+ ## Contributing
81
+
82
+ Bug reports and pull requests are welcome on GitHub at https://github.com/equalize-squad/unique_item_attributes_validator.
83
+
84
+
85
+ ## License
86
+
87
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -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,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "unique_item_attributes_validator"
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
@@ -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,38 @@
1
+ require "active_model/validations"
2
+
3
+ module ActiveModel
4
+ module Validations
5
+ class UniqueItemAttributesValidator < ActiveModel::EachValidator
6
+ def validate_each(record, _attribute, value)
7
+ raise ArgumentError, ":#{value} must be an enumerable" unless value.is_a? Enumerable
8
+ collection = remove_items_marked_for_destruction(value)
9
+ options[:in].each { |item_attribute| validate_unique_item_attribute(record, collection, item_attribute) }
10
+ end
11
+
12
+ private
13
+
14
+ def validate_unique_item_attribute(record, collection, attribute)
15
+ collect_duplication(collection, attribute).tap do |duplicated|
16
+ duplicated.each do |item|
17
+ item.errors[attribute] << I18n.t("errors.messages.taken")
18
+ end
19
+ record.errors[:base] << I18n.t("messages.model_invalid") if duplicated.any?
20
+ end
21
+ end
22
+
23
+ def collect_duplication(collection, attribute)
24
+ collection.group_by { |item| item.send(attribute) }.
25
+ select { |_key, values| values.size > 1 }.
26
+ flat_map { |_key, values| values.slice(1, values.size) }
27
+ end
28
+
29
+ def remove_items_marked_for_destruction(collection)
30
+ if collection.any? { |obj| obj.respond_to?(:marked_for_destruction?) }
31
+ collection.reject(&:marked_for_destruction?)
32
+ else
33
+ collection
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,9 @@
1
+ require "active_model/validations/unique_item_attributes_validator"
2
+
3
+ # A simple date validator for Rails 3+.
4
+ #
5
+ # @example
6
+ # validates :heroes,
7
+ # unique_item_attributes: [:alter_ego, :name]
8
+ module UniqueItemAttributesValidator
9
+ end
@@ -0,0 +1,4 @@
1
+ module UniqueItemAttributesValidator
2
+ # The version number.
3
+ VERSION = "0.0.1".freeze
4
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "unique_item_attributes_validator/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "unique_item_attributes_validator"
8
+ spec.version = UniqueItemAttributesValidator::VERSION
9
+ spec.authors = ["ddomingues", "roalcantara"]
10
+ spec.email = ["diego.domingues16@gmail.com", "rogerio.alcantara@gmail.com"]
11
+
12
+ spec.summary = "Collection item attributes uniqueness validator"
13
+ spec.description = "A simple validator to verify the uniqueness of certain item attributes from a collection"
14
+ spec.homepage = "https://github.com/equalize-squad/unique_item_attributes_validator"
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_runtime_dependency "activemodel"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.7"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec", "~> 3.5"
27
+ spec.add_development_dependency "simplecov", "~> 0.12.0"
28
+ spec.add_development_dependency "codecov", "~> 0.1.5"
29
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unique_item_attributes_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ddomingues
8
+ - roalcantara
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2016-08-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activemodel
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.7'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.7'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '3.5'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '3.5'
70
+ - !ruby/object:Gem::Dependency
71
+ name: simplecov
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: 0.12.0
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: 0.12.0
84
+ - !ruby/object:Gem::Dependency
85
+ name: codecov
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: 0.1.5
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: 0.1.5
98
+ description: A simple validator to verify the uniqueness of certain item attributes
99
+ from a collection
100
+ email:
101
+ - diego.domingues16@gmail.com
102
+ - rogerio.alcantara@gmail.com
103
+ executables: []
104
+ extensions: []
105
+ extra_rdoc_files: []
106
+ files:
107
+ - ".gitignore"
108
+ - ".hound.yml"
109
+ - ".rspec"
110
+ - ".rubocop.yml"
111
+ - ".ruby-version"
112
+ - ".simplecov"
113
+ - ".travis.yml"
114
+ - Gemfile
115
+ - LICENSE.txt
116
+ - README.md
117
+ - Rakefile
118
+ - bin/console
119
+ - bin/setup
120
+ - lib/active_model/validations/unique_item_attributes_validator.rb
121
+ - lib/unique_item_attributes_validator.rb
122
+ - lib/unique_item_attributes_validator/version.rb
123
+ - unique_item_attributes_validator.gemspec
124
+ homepage: https://github.com/equalize-squad/unique_item_attributes_validator
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.5.1
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: Collection item attributes uniqueness validator
148
+ test_files: []