array_validator 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b026957a9197f90b480fdcb3a9202b13e42f6e89
4
- data.tar.gz: c8e2997b85250c3ccffdc08ccd90ce5c485212a2
3
+ metadata.gz: bc31c0c649677829798ded258de4247ab5025c01
4
+ data.tar.gz: 30cc7c88c932786dbb6dfbb5ec5b127febd7c598
5
5
  SHA512:
6
- metadata.gz: bc40bfd0ca9c205f81d0244f80854cd37ab8e0ebac1b924ccc3f378623d6cf9c13a5d6fdf80f59c0b1131847a99ce1b83c16c85a96c13360b84b346937e63305
7
- data.tar.gz: 209f8e6efb556d6ad27f517e2e42c9ce6e384f62425c45b3fc6496b3c05956859298a28221b06e1784c42066660a951a4bf84b83c1e99744feb075409467acb7
6
+ metadata.gz: d09e94377307fd625dc3e38f729eedb48f5254d7314420447a71d4d2406c577182b95fff1c12fa60585ec77e8963d2ff3454540590cbf0ed1cffcdd1516d2abc
7
+ data.tar.gz: 65fd955eddb83c6f409efb1da33cd2d4eadf91b61a0cab9cb75b450beabe11707ce67dd247647b293df24633a139da41da8eb59d878511bfde50441e7695dbe6
@@ -1,5 +1,11 @@
1
1
  sudo: false
2
2
  language: ruby
3
- rvm:
4
- - 2.3.1
5
3
  before_install: gem install bundler -v 1.14.3
4
+ script:
5
+ - bundle exec rspec spec/
6
+ - bundle exec codeclimate-test-reporter
7
+ matrix:
8
+ include:
9
+ - rvm: 2.2.2
10
+ - rvm: 2.3.0
11
+ - rvm: 2.4.0
data/Gemfile CHANGED
@@ -1,4 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in array_validator.gemspec
4
3
  gemspec
data/README.md CHANGED
@@ -1,18 +1,10 @@
1
1
  # ArrayValidator
2
2
 
3
- ActiveModel Validations for array attributes (e.g. Postgres JSONB).
4
-
5
- ## Installation
6
-
7
- Add this line to your application's Gemfile:
3
+ [![Build Status](https://travis-ci.org/infinum/array_validator.png)](https://travis-ci.org/infinum/array_validator)
4
+ [![Code Climate](https://codeclimate.com/github/infinum/array_validator/badges/gpa.svg)](https://codeclimate.com/github/infinum/array_validator)
5
+ [![Test Coverage](https://codeclimate.com/github/infinum/array_validator/badges/coverage.svg)](https://codeclimate.com/github/infinum/array_validator/coverage)
8
6
 
9
- ```ruby
10
- gem 'array_validator'
11
- ```
12
-
13
- And then execute:
14
-
15
- $ bundle
7
+ ActiveModel Validations for array attributes (e.g. Postgres JSONB).
16
8
 
17
9
  ## Usage
18
10
 
@@ -28,7 +20,7 @@ Don't allow duplicates:
28
20
 
29
21
  ```ruby
30
22
  class Plant < ActiveRecord::Base
31
- validates :watering_times, array: { subset_of: ['trees', 'flowers'], duplicates: false }
23
+ validates :categories, array: { subset_of: ['trees', 'flowers'], duplicates: false }
32
24
  end
33
25
  ```
34
26
 
@@ -41,10 +33,27 @@ class Plant < ActiveRecord::Base
41
33
  end
42
34
  ```
43
35
 
44
- ## TODO
36
+ Validate the elements are ordered:
37
+
38
+ ```ruby
39
+ class Plant < ActiveRecord::Base
40
+ validates :intake_times, array: { order: :asc }
41
+ validates :quantities, array: { order: :desc }
42
+ end
43
+ ```
44
+
45
+ ## Installation
46
+
47
+ Add this line to your application's Gemfile:
48
+
49
+ ```ruby
50
+ gem 'array_validator'
51
+ ```
52
+
53
+ And then execute:
54
+
55
+ $ bundle
45
56
 
46
- - array elements are sorted
47
- - length of the array
48
57
 
49
58
  ## Development
50
59
 
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'array_validator'
7
- spec.version = '0.1.0'
7
+ spec.version = '0.2.0'
8
8
  spec.authors = ['Damir Svrtan']
9
9
  spec.email = ['damir.svrtan@gmail.com']
10
10
 
@@ -26,4 +26,6 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency 'rake', '~> 10.0'
27
27
  spec.add_development_dependency 'rspec', '~> 3.0'
28
28
  spec.add_development_dependency 'pry'
29
+ spec.add_development_dependency 'simplecov', '~> 0.12.0'
30
+ spec.add_development_dependency 'codeclimate-test-reporter', '~> 1.0.0'
29
31
  end
@@ -1,9 +1,15 @@
1
1
  require 'active_model'
2
2
  require 'active_support/i18n'
3
+ require 'array_validator/subset_validator'
4
+ require 'array_validator/format_validator'
5
+ require 'array_validator/duplicates_validator'
6
+ require 'array_validator/order_validator'
7
+
3
8
  I18n.load_path += Dir[File.dirname(__FILE__) + "/locale/*.yml"]
4
9
 
5
10
  class ArrayValidator < ActiveModel::EachValidator
6
11
  I18N_SCOPE = 'activerecord.errors.messages.array'.freeze
12
+ VALIDATORS = [SubsetValidator, FormatValidator, DuplicatesValidator, OrderValidator].freeze
7
13
 
8
14
  def validate_each(record, attribute, values)
9
15
  unless values.is_a? Array
@@ -11,43 +17,8 @@ class ArrayValidator < ActiveModel::EachValidator
11
17
  return
12
18
  end
13
19
 
14
- check_subset(record, attribute, values) if options[:subset_of].present?
15
- check_format(record, attribute, values) if options[:format].present?
16
- check_duplicates(record, attribute, values) if options[:duplicates] == false
17
- end
18
-
19
- private
20
-
21
- def check_subset(record, attribute, values)
22
- unlisted_values = values - options[:subset_of]
23
- return if unlisted_values.empty?
24
-
25
- record.errors[attribute] << subset_error_message(unlisted_values)
26
- end
27
-
28
- def subset_error_message(unlisted_values)
29
- I18n.t('subset_of', scope: I18N_SCOPE, invalid_values: unlisted_values.join(', '))
30
- end
31
-
32
- def check_format(record, attribute, values)
33
- invalid_values = values.select { |value| value !~ options[:format] }
34
- return if invalid_values.empty?
35
-
36
- record.errors[attribute] << format_error_message(invalid_values)
37
- end
38
-
39
- def format_error_message(invalid_values)
40
- I18n.t('format', scope: I18N_SCOPE, invalid_values: invalid_values.join(', '))
41
- end
42
-
43
- def check_duplicates(record, attribute, values)
44
- duplicates = values.select { |value| values.count(value) > 1 }.uniq
45
- return if duplicates.none?
46
-
47
- record.errors[attribute] << duplicates_error_message(duplicates)
48
- end
49
-
50
- def duplicates_error_message(duplicates)
51
- I18n.t('duplicates', scope: I18N_SCOPE, invalid_values: duplicates.join(', '))
20
+ VALIDATORS.each do |validator|
21
+ validator.call(record, attribute, values, options)
22
+ end
52
23
  end
53
24
  end
@@ -0,0 +1,13 @@
1
+ class ArrayValidator < ActiveModel::EachValidator
2
+ module DuplicatesValidator
3
+ def self.call(record, attribute, values, options = {})
4
+ return if options[:duplicates] != false
5
+ duplicates = values.select { |value| values.count(value) > 1 }.uniq
6
+ return if duplicates.none?
7
+
8
+ record.errors[attribute].push(
9
+ I18n.t('duplicates', scope: I18N_SCOPE, invalid_values: duplicates.join(', '))
10
+ )
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ class ArrayValidator < ActiveModel::EachValidator
2
+ module FormatValidator
3
+ def self.call(record, attribute, values, options = {})
4
+ return if options[:format].nil?
5
+ invalid_values = values.select { |value| value !~ options[:format] }
6
+ return if invalid_values.empty?
7
+
8
+ record.errors[attribute].push(
9
+ I18n.t('format', scope: I18N_SCOPE, invalid_values: invalid_values.join(', '))
10
+ )
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ class ArrayValidator
2
+ module OrderValidator
3
+ def self.call(record, attribute, values, options)
4
+ return if options[:order].nil?
5
+ raise ArgumentError, 'Not a supported order option' unless [:asc, :desc].include?(options[:order])
6
+
7
+ ordered = if options[:order] == :asc
8
+ values.sort == values
9
+ else
10
+ values.sort.reverse == values
11
+ end
12
+ return if ordered
13
+
14
+ record.errors[attribute].push(
15
+ I18n.t('order', scope: I18N_SCOPE, direction: "#{options[:order]}ending")
16
+ )
17
+ end
18
+ end
19
+ end
@@ -1,18 +1,13 @@
1
- class ArrayValidator
1
+ class ArrayValidator < ActiveModel::EachValidator
2
2
  module SubsetValidator
3
- def self.call(record, attribute, values, options)
3
+ def self.call(record, attribute, values, options = {})
4
4
  return if options[:subset_of].nil?
5
-
6
5
  unlisted_values = values - options[:subset_of]
7
- return if unlisted_values.empty?
8
-
9
- error_message = if unlisted_values.size == 1
10
- "#{unlisted_values.first} is not in the list"
11
- else
12
- "#{unlisted_values.join(', ')} are not in the list"
13
- end
6
+ return if unlisted_values.none?
14
7
 
15
- record.errors[attribute] << error_message
8
+ record.errors[attribute].push(
9
+ I18n.t('subset_of', scope: I18N_SCOPE, invalid_values: unlisted_values.join(', '))
10
+ )
16
11
  end
17
12
  end
18
13
  end
@@ -6,3 +6,4 @@ en:
6
6
  subset_of: 'has invalid values: %{invalid_values}'
7
7
  format: 'not in a valid format: %{invalid_values}'
8
8
  duplicates: 'has duplicates: %{invalid_values}'
9
+ order: 'is not ordered in %{direction} order'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: array_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damir Svrtan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-07 00:00:00.000000000 Z
11
+ date: 2017-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -80,6 +80,34 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.12.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.12.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: codeclimate-test-reporter
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.0.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.0.0
83
111
  description: Array validations for Rails (e.g. Postgres jsonb columns)
84
112
  email:
85
113
  - damir.svrtan@gmail.com
@@ -99,6 +127,9 @@ files:
99
127
  - bin/console
100
128
  - bin/setup
101
129
  - lib/array_validator.rb
130
+ - lib/array_validator/duplicates_validator.rb
131
+ - lib/array_validator/format_validator.rb
132
+ - lib/array_validator/order_validator.rb
102
133
  - lib/array_validator/subset_validator.rb
103
134
  - lib/locale/en.yml
104
135
  homepage: https://github.com/infinum/array_validator
@@ -121,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
152
  version: '0'
122
153
  requirements: []
123
154
  rubyforge_project:
124
- rubygems_version: 2.5.1
155
+ rubygems_version: 2.6.6
125
156
  signing_key:
126
157
  specification_version: 4
127
158
  summary: Array validations for Rails