missing_validators 0.9.1 → 1.0.1

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: c63967ad9baecba5c898b86d7f38036ef7f18c25
4
- data.tar.gz: 8b4a19ae8534abda461c8d02326696454f93a4c0
3
+ metadata.gz: 0e08933cb1d48eaa485ea7c22290d71be64477d0
4
+ data.tar.gz: 9456a6513e7249af10493f30d28d0a1947cfd5d3
5
5
  SHA512:
6
- metadata.gz: dabbbcce9d7f753332e5279157580c2ef147d2a83be271f3bb8b5a6c99eb8ab0693028e115a4df6ebabc857f7ae0d422dde3da7c45847e6cd1a0fa3fcf432bc6
7
- data.tar.gz: 469ef6c5b5b4644eb5166ea5b923bd24e66f197f8839a33f64f57cbecf2670b015fe75c0949b08f1b91f86992253d9991e6b081e56e702ff04dcd312f959853e
6
+ metadata.gz: d54385d82f777f5605fbfc6a49bb42a0c8ab95f490973816ad25f2ff818a8ca15e05a7a853c7c28da482895a6c5840885457b207da24e55110019c05ef41429d
7
+ data.tar.gz: e7e588451d9109835edfa1e2d15e57598ed37287a6fbb386577e1007bb61b97359ce3424916806670fd24cb02f78c364da39465f4c543ef48e4d8c0965f6fb4e
@@ -7,6 +7,8 @@ en:
7
7
  url: "is not a valid URL"
8
8
  mac_address: "is not a valid MAC address"
9
9
  longitude: "is not a valid longitude"
10
+ latitude: "is not a valid latitude"
11
+ color: "is not a valid hexadecimal color"
10
12
  missing_validators:
11
13
  matchers:
12
14
  ensure_inequality_of:
@@ -15,12 +15,12 @@ RSpec::Matchers.define :ensure_inequality_of do |attribute|
15
15
  end
16
16
  end
17
17
 
18
- failure_message_for_should do |model|
18
+ failure_message do |model|
19
19
  I18n.t 'missing_validators.matchers.ensure_inequality_of.failure_message_for_should',
20
20
  model: model.class, attr: attribute.inspect
21
21
  end
22
22
 
23
- failure_message_for_should_not do |model|
23
+ failure_message_when_negated do |model|
24
24
  I18n.t 'missing_validators.matchers.ensure_inequality_of.failure_message_for_should_not',
25
25
  model: model.class, attr: attribute.inspect
26
26
  end
@@ -8,12 +8,12 @@ RSpec::Matchers.define :ensure_valid_email_format_of do |attribute|
8
8
  end
9
9
  end
10
10
 
11
- failure_message_for_should do |model|
11
+ failure_message do |model|
12
12
  I18n.t 'missing_validators.matchers.ensure_valid_email_format_of.failure_message_for_should',
13
13
  model: model.class
14
14
  end
15
15
 
16
- failure_message_for_should_not do |model|
16
+ failure_message_when_negated do |model|
17
17
  I18n.t 'missing_validators.matchers.ensure_valid_email_format_of.failure_message_for_should_not',
18
18
  model: model.class
19
19
  end
@@ -8,12 +8,12 @@ RSpec::Matchers.define :ensure_valid_imei_format_of do |attribute|
8
8
  end
9
9
  end
10
10
 
11
- failure_message_for_should do |model|
11
+ failure_message do |model|
12
12
  I18n.t 'missing_validators.matchers.ensure_valid_imei_format_of.failure_message_for_should',
13
13
  model: model.class
14
14
  end
15
15
 
16
- failure_message_for_should_not do |model|
16
+ failure_message_when_negated do |model|
17
17
  I18n.t 'missing_validators.matchers.ensure_valid_imei_format_of.failure_message_for_should_not',
18
18
  model: model.class
19
19
  end
@@ -8,12 +8,12 @@ RSpec::Matchers.define :ensure_valid_mac_address_format_of do |attribute|
8
8
  end
9
9
  end
10
10
 
11
- failure_message_for_should do |model|
11
+ failure_message do |model|
12
12
  I18n.t 'missing_validators.matchers.ensure_valid_mac_address_format_of.failure_message_for_should',
13
13
  model: model.class
14
14
  end
15
15
 
16
- failure_message_for_should_not do |model|
16
+ failure_message_when_negated do |model|
17
17
  I18n.t 'missing_validators.matchers.ensure_valid_mac_address_format_of.failure_message_for_should_not',
18
18
  model: model.class
19
19
  end
@@ -8,12 +8,12 @@ RSpec::Matchers.define :ensure_valid_url_format_of do |attribute|
8
8
  end
9
9
  end
10
10
 
11
- failure_message_for_should do |model|
11
+ failure_message do |model|
12
12
  I18n.t 'missing_validators.matchers.ensure_valid_url_format_of.failure_message_for_should',
13
13
  model: model.class
14
14
  end
15
15
 
16
- failure_message_for_should_not do |model|
16
+ failure_message_when_negated do |model|
17
17
  I18n.t 'missing_validators.matchers.ensure_valid_url_format_of.failure_message_for_should_not',
18
18
  model: model.class
19
19
  end
@@ -0,0 +1,33 @@
1
+ # Allows to check if the value of a specific attribute is a valid hex color.
2
+ #
3
+ # @example Validate that the product color is a valid hex color.
4
+ # class Product << ActiveRecord::Base
5
+ # attr_accessor :color
6
+ # validates :color, color: true
7
+ # end
8
+ class ColorValidator < ActiveModel::EachValidator
9
+ # Checks if an attribute value is a valid hex color.
10
+ #
11
+ # @param [Object] record object to validate
12
+ # @param [String] attribute name of the object attribute to validate
13
+ # @param [Object] value attribute value
14
+ def validate_each(record, attribute, value)
15
+ allow_blank = options[:allow_blank] || false
16
+ return if allow_blank && value.blank?
17
+
18
+ unless valid?(value, options)
19
+ record.errors[attribute] << (options[:message] || I18n.t('errors.messages.color'))
20
+ end
21
+ end
22
+
23
+ def self.validate_format(color)
24
+ !!(color =~ /^#(?:[0-9a-f]{3})(?:[0-9a-f]{3})?$/i)
25
+ end
26
+
27
+ private
28
+
29
+ def valid?(color, options)
30
+ self.class.validate_format(color)
31
+ end
32
+
33
+ end
@@ -1,5 +1,5 @@
1
1
  # Provides a collection of custom validators that are often required in Rails applications.
2
2
  module MissingValidators
3
3
  # Gem version.
4
- VERSION = "0.9.1"
4
+ VERSION = "1.0.1"
5
5
  end
@@ -11,5 +11,6 @@ require 'missing_validators/validators/mac_address_validator'
11
11
  require 'missing_validators/matchers/ensure_valid_mac_address_format_of' if defined?(RSpec)
12
12
  require 'missing_validators/validators/longitude_validator'
13
13
  require 'missing_validators/validators/latitude_validator'
14
+ require 'missing_validators/validators/color_validator'
14
15
  require 'missing_validators/validators/imei_validator'
15
16
  require 'missing_validators/matchers/ensure_valid_imei_format_of' if defined?(RSpec)
@@ -2,12 +2,12 @@
2
2
  require File.expand_path('../lib/missing_validators/version', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |gem|
5
- gem.authors = ["Andrew Gridnev"]
5
+ gem.authors = ["Andrei Gridnev"]
6
6
  gem.email = ["andrew.gridnev@gmail.com"]
7
7
  gem.summary = %q{Adds some handy validators.}
8
- gem.description = %q{Validates email addresses, URLs, IMEI, MAC addresses and inequality of attributes.}
8
+ gem.description = %q{Validates email addresses, URLs, IMEI, MAC addresses, latitude, longitude, hex colors and inequality of attributes.}
9
9
  gem.homepage = "https://github.com/andrewgr/missing_validators/"
10
- gem.license = 'MIT'
10
+ gem.license = 'MIT'
11
11
 
12
12
  gem.files = `git ls-files`.split($\)
13
13
  gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
@@ -16,8 +16,8 @@ Gem::Specification.new do |gem|
16
16
  gem.require_paths = ["lib"]
17
17
  gem.version = MissingValidators::VERSION
18
18
 
19
- gem.add_dependency 'activemodel', '> 3.0.0'
20
- gem.add_dependency 'activesupport', '> 3.0.0'
19
+ gem.add_runtime_dependency 'activemodel'
20
+ gem.add_runtime_dependency 'activesupport'
21
21
 
22
22
  gem.add_development_dependency 'rspec'
23
23
  gem.add_development_dependency 'shoulda-matchers'
data/spec/spec_helper.rb CHANGED
@@ -2,4 +2,5 @@ require 'missing_validators'
2
2
  require 'shoulda/matchers/active_record'
3
3
  require 'shoulda-matchers'
4
4
 
5
+ I18n.enforce_available_locales = false
5
6
  I18n.load_path << File.expand_path("../../config/locales/en.yml", __FILE__)
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe ColorValidator do
4
+ context "Color has a valid value" do
5
+ let(:klass) do
6
+ Class.new do
7
+ include ActiveModel::Validations
8
+ attr_accessor :color
9
+ validates :color, color: true
10
+ end
11
+ end
12
+
13
+ subject { klass.new }
14
+
15
+ it { should allow_value("#000000").for(:color) }
16
+ it { should allow_value("#ab00FF").for(:color) }
17
+
18
+ it { should_not allow_value("kk").for(:color) }
19
+ it { should_not allow_value(0).for(:color) }
20
+ end
21
+ end
metadata CHANGED
@@ -1,82 +1,82 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: missing_validators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
- - Andrew Gridnev
7
+ - Andrei Gridnev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-14 00:00:00.000000000 Z
11
+ date: 2014-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>'
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 3.0.0
19
+ version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>'
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 3.0.0
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>'
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 3.0.0
33
+ version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>'
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 3.0.0
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: shoulda-matchers
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- description: Validates email addresses, URLs, IMEI, MAC addresses and inequality of
70
- attributes.
69
+ description: Validates email addresses, URLs, IMEI, MAC addresses, latitude, longitude,
70
+ hex colors and inequality of attributes.
71
71
  email:
72
72
  - andrew.gridnev@gmail.com
73
73
  executables: []
74
74
  extensions: []
75
75
  extra_rdoc_files: []
76
76
  files:
77
- - .gitignore
78
- - .rspec
79
- - .travis.yml
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
80
  - Gemfile
81
81
  - LICENSE
82
82
  - README.md
@@ -88,6 +88,7 @@ files:
88
88
  - lib/missing_validators/matchers/ensure_valid_imei_format_of.rb
89
89
  - lib/missing_validators/matchers/ensure_valid_mac_address_format_of.rb
90
90
  - lib/missing_validators/matchers/ensure_valid_url_format_of.rb
91
+ - lib/missing_validators/validators/color_validator.rb
91
92
  - lib/missing_validators/validators/email_validator.rb
92
93
  - lib/missing_validators/validators/imei_validator.rb
93
94
  - lib/missing_validators/validators/inequality_validator.rb
@@ -98,6 +99,7 @@ files:
98
99
  - lib/missing_validators/version.rb
99
100
  - missing_validators.gemspec
100
101
  - spec/spec_helper.rb
102
+ - spec/validators/color_validator_spec.rb
101
103
  - spec/validators/email_validator_spec.rb
102
104
  - spec/validators/imei_spec.rb
103
105
  - spec/validators/inequality_validator_spec.rb
@@ -115,22 +117,23 @@ require_paths:
115
117
  - lib
116
118
  required_ruby_version: !ruby/object:Gem::Requirement
117
119
  requirements:
118
- - - '>='
120
+ - - ">="
119
121
  - !ruby/object:Gem::Version
120
122
  version: '0'
121
123
  required_rubygems_version: !ruby/object:Gem::Requirement
122
124
  requirements:
123
- - - '>='
125
+ - - ">="
124
126
  - !ruby/object:Gem::Version
125
127
  version: '0'
126
128
  requirements: []
127
129
  rubyforge_project:
128
- rubygems_version: 2.1.11
130
+ rubygems_version: 2.2.2
129
131
  signing_key:
130
132
  specification_version: 4
131
133
  summary: Adds some handy validators.
132
134
  test_files:
133
135
  - spec/spec_helper.rb
136
+ - spec/validators/color_validator_spec.rb
134
137
  - spec/validators/email_validator_spec.rb
135
138
  - spec/validators/imei_spec.rb
136
139
  - spec/validators/inequality_validator_spec.rb