missing_validators 0.7.2 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 033cbbe718a1d580223dbb0adc271f1ed173b44f
4
- data.tar.gz: d0d25453fc9461d621ac04bcb6cf0da3ac2525f5
3
+ metadata.gz: a1188aa9b98a750a5f6a5678e2781d49734d8da3
4
+ data.tar.gz: 790f2c7dd6183bfc20fb9105b385d3ac7871cee6
5
5
  SHA512:
6
- metadata.gz: e85456bdc73b306b960706151d1c345f4d33cf5f35d6c7468c5ade014afabb61a6f77280848b2be0c14b2fc0848afd5fd5a6469143d1b8a7da822afc3ca5bbfc
7
- data.tar.gz: d64609918b653ab0c5e6f2a269d1edc995860661a2b76cc525edff37e96e6d32959188c086d67eb8c7662559a1badb5ed0ba618423b14a391874d64efd4b23d6
6
+ metadata.gz: 9ef90cf11b99de00cd7af803e1caa94563645459b97899b400e437895b5f7645a23f74d01fa8fc67e234131a8a6002dd05ac1ceb778e8150416fd38c6b7c2f37
7
+ data.tar.gz: 9c887c9d69ba46e2f84ea1eb5b1456386622646c30ba4552071b905cd798541a95aed079c9bf3b5a366cb38a0c3e5d63949d57ba6cc098fb3d8a8daf28ac8f23
@@ -5,6 +5,7 @@ en:
5
5
  email: "isn't a valid email address"
6
6
  url: "isn't a valid URL"
7
7
  mac_address: "isn't a valid MAC address"
8
+ longitude: "isn't a valid longitude"
8
9
  missing_validators:
9
10
  matchers:
10
11
  ensure_inequality_of:
@@ -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
@@ -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.7.2"
4
+ VERSION = "0.8.0"
5
5
  end
@@ -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.7.2
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-13 00:00:00.000000000 Z
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