missing_validators 0.7.2 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/config/locales/en.yml +1 -0
- data/lib/missing_validators/validators/latitude_validator.rb +29 -0
- data/lib/missing_validators/validators/longitude_validator.rb +29 -0
- data/lib/missing_validators/version.rb +1 -1
- data/lib/missing_validators.rb +2 -0
- data/spec/validators/latitude_validator_spec.rb +23 -0
- data/spec/validators/longitude_validator_spec.rb +23 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a1188aa9b98a750a5f6a5678e2781d49734d8da3
|
4
|
+
data.tar.gz: 790f2c7dd6183bfc20fb9105b385d3ac7871cee6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ef90cf11b99de00cd7af803e1caa94563645459b97899b400e437895b5f7645a23f74d01fa8fc67e234131a8a6002dd05ac1ceb778e8150416fd38c6b7c2f37
|
7
|
+
data.tar.gz: 9c887c9d69ba46e2f84ea1eb5b1456386622646c30ba4552071b905cd798541a95aed079c9bf3b5a366cb38a0c3e5d63949d57ba6cc098fb3d8a8daf28ac8f23
|
data/config/locales/en.yml
CHANGED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Allows to check if the value of a specific attribute is a valid MAC address.
|
2
|
+
#
|
3
|
+
# @example Validate that the device MAC address is valid.
|
4
|
+
# class Device << ActiveRecord::Base
|
5
|
+
# attr_accessor :lat
|
6
|
+
# validates :lat, latitude: true
|
7
|
+
# end
|
8
|
+
class LatitudeValidator < ActiveModel::EachValidator
|
9
|
+
# Checks if an attribute value is a valid MAC address.
|
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 self.class.valid?(value, options)
|
19
|
+
record.errors[attribute] << (options[:message] || I18n.t('errors.messages.latitude'))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def self.valid?(latitude, options)
|
26
|
+
latitude >= -90 && latitude <= 90
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# Allows to check if the value of a specific attribute is a valid MAC address.
|
2
|
+
#
|
3
|
+
# @example Validate that the device MAC address is valid.
|
4
|
+
# class Device << ActiveRecord::Base
|
5
|
+
# attr_accessor :lon
|
6
|
+
# validates :lon, longitude: true
|
7
|
+
# end
|
8
|
+
class LongitudeValidator < ActiveModel::EachValidator
|
9
|
+
# Checks if an attribute value is a valid MAC address.
|
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 self.class.valid?(value, options)
|
19
|
+
record.errors[attribute] << (options[:message] || I18n.t('errors.messages.longitude'))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def self.valid?(longitude, options)
|
26
|
+
longitude >= -180 && longitude <= 180
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
data/lib/missing_validators.rb
CHANGED
@@ -8,3 +8,5 @@ require 'missing_validators/validators/url_validator'
|
|
8
8
|
require 'missing_validators/matchers/ensure_valid_url_format_of' if defined?(RSpec)
|
9
9
|
require 'missing_validators/validators/mac_address_validator'
|
10
10
|
require 'missing_validators/matchers/ensure_valid_mac_address_format_of' if defined?(RSpec)
|
11
|
+
require 'missing_validators/validators/longitude_validator'
|
12
|
+
require 'missing_validators/validators/latitude_validator'
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe LatitudeValidator do
|
4
|
+
context "Latitude has a valid value" do
|
5
|
+
let(:klass) do
|
6
|
+
Class.new do
|
7
|
+
include ActiveModel::Validations
|
8
|
+
attr_accessor :lat
|
9
|
+
validates :lat, latitude: true
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
subject { klass.new }
|
14
|
+
|
15
|
+
it { should allow_value(-90).for(:lat) }
|
16
|
+
it { should allow_value(90).for(:lat) }
|
17
|
+
it { should allow_value(0).for(:lat) }
|
18
|
+
it { should allow_value(9.33).for(:lat) }
|
19
|
+
|
20
|
+
it { should_not allow_value(-90.1).for(:lat) }
|
21
|
+
it { should_not allow_value(90.1).for(:lat) }
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe LongitudeValidator do
|
4
|
+
context "Longitude has a valid value" do
|
5
|
+
let(:klass) do
|
6
|
+
Class.new do
|
7
|
+
include ActiveModel::Validations
|
8
|
+
attr_accessor :lon
|
9
|
+
validates :lon, longitude: true
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
subject { klass.new }
|
14
|
+
|
15
|
+
it { should allow_value(-180).for(:lon) }
|
16
|
+
it { should allow_value(180).for(:lon) }
|
17
|
+
it { should allow_value(0).for(:lon) }
|
18
|
+
it { should allow_value(9.33).for(:lon) }
|
19
|
+
|
20
|
+
it { should_not allow_value(-181.1).for(:lon) }
|
21
|
+
it { should_not allow_value(181.1).for(:lon) }
|
22
|
+
end
|
23
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: missing_validators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Gridnev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -88,6 +88,8 @@ files:
|
|
88
88
|
- lib/missing_validators/matchers/ensure_valid_url_format_of.rb
|
89
89
|
- lib/missing_validators/validators/email_validator.rb
|
90
90
|
- lib/missing_validators/validators/inequality_validator.rb
|
91
|
+
- lib/missing_validators/validators/latitude_validator.rb
|
92
|
+
- lib/missing_validators/validators/longitude_validator.rb
|
91
93
|
- lib/missing_validators/validators/mac_address_validator.rb
|
92
94
|
- lib/missing_validators/validators/url_validator.rb
|
93
95
|
- lib/missing_validators/version.rb
|
@@ -95,6 +97,8 @@ files:
|
|
95
97
|
- spec/spec_helper.rb
|
96
98
|
- spec/validators/email_validator_spec.rb
|
97
99
|
- spec/validators/inequality_validator_spec.rb
|
100
|
+
- spec/validators/latitude_validator_spec.rb
|
101
|
+
- spec/validators/longitude_validator_spec.rb
|
98
102
|
- spec/validators/mac_address_spec.rb
|
99
103
|
- spec/validators/url_validator_spec.rb
|
100
104
|
homepage: https://github.com/andrewgr/missing_validators/
|
@@ -125,5 +129,7 @@ test_files:
|
|
125
129
|
- spec/spec_helper.rb
|
126
130
|
- spec/validators/email_validator_spec.rb
|
127
131
|
- spec/validators/inequality_validator_spec.rb
|
132
|
+
- spec/validators/latitude_validator_spec.rb
|
133
|
+
- spec/validators/longitude_validator_spec.rb
|
128
134
|
- spec/validators/mac_address_spec.rb
|
129
135
|
- spec/validators/url_validator_spec.rb
|