active_validation 2.5.0 → 2.6.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: 8196f5d5b7d616c9b4da4eb03c09d923a517b058
4
- data.tar.gz: f76278a7cf0188be5e37fc0a2d9dda49d37e382a
3
+ metadata.gz: b0d9d879a1f9c9282e51f170e2c79b3340611c85
4
+ data.tar.gz: ee74c298376f0009c20a514ed0945459bafbdcad
5
5
  SHA512:
6
- metadata.gz: ec99d7e5ec07ab6b7ece6e5bc0f7685d5fc38f25b88a639533348f5e332b81af07c35238ea1146737f536792f7c53264f9546fb93dacd90014fe33c0568b9cf2
7
- data.tar.gz: a6a555a5994c53bbff3dd3690d8066a7db6764a3b361f3301680a804063b72621205e38393ee0b8c66a0a0f55105cd14348608981adbb54a3e37b21381f19118
6
+ metadata.gz: 482e54bd00f6223ac3ea14b47759c27ae32c2c9542e293da4908a7084e524b3b8b90b9d72c488d27a43cee3c72854f3477279f7a541b52fa2cdeb29a4fe0b5f2
7
+ data.tar.gz: 9ff5492577d2a2acf287e22684947b18055980200670e1932e593f4f5bed8ee5e00fb1a9e7a9643124250104831153df30004a31a908168893bc2b5321e09467
data/README.md CHANGED
@@ -50,6 +50,8 @@ Or install it yourself as:
50
50
  * [SEDOL](#sedolvalidator)
51
51
  * [Slug](#slugvalidator)
52
52
  * [SSN](#ssnvalidator)
53
+ * [Tracking Number](#trackingnumbervalidator)
54
+ * [Type](#typevalidator)
53
55
  * [URL](#urlvalidator)
54
56
  * [Username](#usernamevalidator)
55
57
  * [UUID](#uuidvalidator)
@@ -916,6 +918,82 @@ describe User do
916
918
  end
917
919
  ```
918
920
 
921
+ ## TrackingNumberValidator
922
+
923
+ **Ex:** 1Z8V92A70367203024
924
+
925
+ With an ActiveRecord model:
926
+
927
+ ```ruby
928
+ class Package < ActiveRecord::Base
929
+ attr_accessor :tracking_number, :name
930
+ validates :tracking_number, tracking_number: true
931
+ end
932
+ ```
933
+
934
+ Or any ruby class:
935
+
936
+ ```ruby
937
+ class Package
938
+ include ActiveModel::Validations
939
+ attr_accessor :tracking_number, :name
940
+ validates :tracking_number, tracking_number: true
941
+ end
942
+ ```
943
+
944
+ Options:
945
+ * carrier: :dhl, :fedex, :ontrac, :ups, :usps
946
+ * service: :express, :express_air, :ground, :ground18, :ground96, :smart_post, :usps13, :usps20, :usps91
947
+
948
+ ```ruby
949
+ validates :tracking_number, tracking_number: { carrier: :dhl }
950
+ validates :tracking_number, tracking_number: { carrier: :fedex, service: :express }
951
+ ```
952
+
953
+ RSpec matcher is also available for your convenience:
954
+
955
+ ```ruby
956
+ describe Package do
957
+ it { should ensure_valid_tracking_number_format_of(:tracking_number) }
958
+ it { should_not ensure_valid_tracking_number_format_of(:name) }
959
+ end
960
+ ```
961
+
962
+ ## TypeValidator
963
+
964
+ **Ex:** Boolean or String
965
+
966
+ **Rules:**
967
+ * Any valid ruby class
968
+
969
+ With an ActiveRecord model:
970
+
971
+ ```ruby
972
+ class User < ActiveRecord::Base
973
+ attr_accessor :active, :name
974
+ validates :active, type: Boolean
975
+ end
976
+ ```
977
+
978
+ Or any ruby class:
979
+
980
+ ```ruby
981
+ class User
982
+ include ActiveModel::Validations
983
+ attr_accessor :active, :name
984
+ validates :active, type: Boolean
985
+ end
986
+ ```
987
+
988
+ RSpec matcher is also available for your convenience:
989
+
990
+ ```ruby
991
+ describe User do
992
+ it { should ensure_valid_type_format_of(:active) }
993
+ it { should_not ensure_valid_type_format_of(:name) }
994
+ end
995
+ ```
996
+
919
997
  ## UrlValidator
920
998
 
921
999
  **Ex:** example.com or http://www.example.com
@@ -28,6 +28,7 @@ en:
28
28
  slug: "is not a valid slug"
29
29
  ssn: "is not a valid social security number"
30
30
  tracking_number: "is not a valid tracking number"
31
+ type: "is not a valid type"
31
32
  url: "is not a valid URL"
32
33
  username: "is not a valid username"
33
34
  uuid: "is not a valid UUID"
@@ -101,6 +102,9 @@ en:
101
102
  ensure_valid_tracking_number_format_of:
102
103
  failure_message_for_should: "%{model} should ensure valid tracking number format of attribute %{attr}"
103
104
  failure_message_for_should_not: "%{model} should not ensure valid tracking number format of attribute %{attr}"
105
+ ensure_valid_type_format_of:
106
+ failure_message_for_should: "%{model} should ensure valid type of attribute %{attr}"
107
+ failure_message_for_should_not: "%{model} should not ensure valid type of attribute %{attr}"
104
108
  ensure_valid_url_format_of:
105
109
  failure_message_for_should: "%{model} should ensure valid URL format of attribute %{attr}"
106
110
  failure_message_for_should_not: "%{model} should not ensure valid URL format of attribute %{attr}"
@@ -109,4 +113,4 @@ en:
109
113
  failure_message_for_should_not: "%{model} should not ensure valid username format of attribute %{attr}"
110
114
  ensure_valid_uuid_format_of:
111
115
  failure_message_for_should: "%{model} should ensure valid UUID format of attribute %{attr}"
112
- failure_message_for_should_not: "%{model} should not ensure valid UUID format of attribute %{attr}"
116
+ failure_message_for_should_not: "%{model} should not ensure valid UUID format of attribute %{attr}"
@@ -0,0 +1,26 @@
1
+ RSpec::Matchers.define :ensure_valid_type_format_of do |attribute|
2
+ match do |model|
3
+ model.send("#{attribute}=", nil)
4
+ model.valid?
5
+
6
+ if model.errors.has_key?(attribute)
7
+ model.errors[attribute].include?(I18n.t('active_validation.errors.messages.type'))
8
+ end
9
+ end
10
+
11
+ failure_message do |model|
12
+ I18n.t(
13
+ 'active_validation.errors.matchers.ensure_valid_type_format_of.failure_message_for_should',
14
+ attr: attribute.inspect,
15
+ model: model.class.name
16
+ )
17
+ end
18
+
19
+ failure_message_when_negated do |model|
20
+ I18n.t(
21
+ 'active_validation.errors.matchers.ensure_valid_type_format_of.failure_message_for_should_not',
22
+ attr: attribute.inspect,
23
+ model: model.class.name
24
+ )
25
+ end
26
+ end
@@ -0,0 +1,25 @@
1
+ class Boolean
2
+ # Implement pseudo-boolean class
3
+ end
4
+
5
+ class TypeValidator < ActiveModel::EachValidator
6
+
7
+ def validate_each(record, attribute, value)
8
+ unless valid?(value, options)
9
+ record.errors[attribute] << (options.fetch(:message, false) || I18n.t('active_validation.errors.messages.type'.freeze))
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def valid?(value, options)
16
+ klass = options.fetch(:with, nil)
17
+
18
+ if klass == Boolean
19
+ value.is_a?(TrueClass) || value.is_a?(FalseClass)
20
+ else
21
+ value.is_a?(klass)
22
+ end
23
+ end
24
+
25
+ end
@@ -1,3 +1,3 @@
1
1
  module ActiveValidation
2
- VERSION = "2.5.0"
3
- end
2
+ VERSION = "2.6.0"
3
+ end
@@ -26,6 +26,7 @@ require 'active_validation/validators/sedol_validator'
26
26
  require 'active_validation/validators/slug_validator'
27
27
  require 'active_validation/validators/ssn_validator'
28
28
  require 'active_validation/validators/tracking_number_validator'
29
+ require 'active_validation/validators/type_validator'
29
30
  require 'active_validation/validators/url_validator'
30
31
  require 'active_validation/validators/username_validator'
31
32
  require 'active_validation/validators/uuid_validator'
@@ -55,6 +56,7 @@ if defined?(RSpec)
55
56
  require 'active_validation/matchers/ensure_valid_slug_format_of'
56
57
  require 'active_validation/matchers/ensure_valid_ssn_format_of'
57
58
  require 'active_validation/matchers/ensure_valid_tracking_number_format_of'
59
+ require 'active_validation/matchers/ensure_valid_type_format_of'
58
60
  require 'active_validation/matchers/ensure_valid_url_format_of'
59
61
  require 'active_validation/matchers/ensure_valid_username_format_of'
60
62
  require 'active_validation/matchers/ensure_valid_uuid_format_of'
@@ -82,4 +84,4 @@ if defined?(Rails)
82
84
 
83
85
  end
84
86
  end
85
- end
87
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_validation
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.0
4
+ version: 2.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Gomez
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-03-24 00:00:00.000000000 Z
11
+ date: 2016-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -167,6 +167,7 @@ files:
167
167
  - lib/active_validation/matchers/ensure_valid_slug_format_of.rb
168
168
  - lib/active_validation/matchers/ensure_valid_ssn_format_of.rb
169
169
  - lib/active_validation/matchers/ensure_valid_tracking_number_format_of.rb
170
+ - lib/active_validation/matchers/ensure_valid_type_format_of.rb
170
171
  - lib/active_validation/matchers/ensure_valid_url_format_of.rb
171
172
  - lib/active_validation/matchers/ensure_valid_username_format_of.rb
172
173
  - lib/active_validation/matchers/ensure_valid_uuid_format_of.rb
@@ -193,6 +194,7 @@ files:
193
194
  - lib/active_validation/validators/slug_validator.rb
194
195
  - lib/active_validation/validators/ssn_validator.rb
195
196
  - lib/active_validation/validators/tracking_number_validator.rb
197
+ - lib/active_validation/validators/type_validator.rb
196
198
  - lib/active_validation/validators/url_validator.rb
197
199
  - lib/active_validation/validators/username_validator.rb
198
200
  - lib/active_validation/validators/uuid_validator.rb
@@ -217,7 +219,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
217
219
  version: '0'
218
220
  requirements: []
219
221
  rubyforge_project:
220
- rubygems_version: 2.6.2
222
+ rubygems_version: 2.6.4
221
223
  signing_key:
222
224
  specification_version: 4
223
225
  summary: Gem for commonly used validators.