dm-validations 0.10.2 → 1.0.0.rc1

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.
Files changed (109) hide show
  1. data/.gitignore +36 -0
  2. data/Gemfile +143 -0
  3. data/README.rdoc +52 -102
  4. data/Rakefile +3 -3
  5. data/VERSION +1 -1
  6. data/dm-validations.gemspec +126 -14
  7. data/lib/dm-validations.rb +78 -18
  8. data/lib/dm-validations/auto_validate.rb +27 -23
  9. data/lib/dm-validations/contextual_validators.rb +3 -3
  10. data/lib/dm-validations/formats/email.rb +3 -3
  11. data/lib/dm-validations/formats/url.rb +3 -3
  12. data/lib/dm-validations/support/context.rb +1 -1
  13. data/lib/dm-validations/validation_errors.rb +5 -7
  14. data/lib/dm-validations/validators/absent_field_validator.rb +13 -9
  15. data/lib/dm-validations/validators/acceptance_validator.rb +10 -7
  16. data/lib/dm-validations/validators/block_validator.rb +3 -3
  17. data/lib/dm-validations/validators/confirmation_validator.rb +10 -7
  18. data/lib/dm-validations/validators/format_validator.rb +14 -10
  19. data/lib/dm-validations/validators/generic_validator.rb +3 -3
  20. data/lib/dm-validations/validators/length_validator.rb +10 -7
  21. data/lib/dm-validations/validators/method_validator.rb +3 -3
  22. data/lib/dm-validations/validators/numeric_validator.rb +25 -14
  23. data/lib/dm-validations/validators/primitive_validator.rb +12 -8
  24. data/lib/dm-validations/validators/required_field_validator.rb +12 -9
  25. data/lib/dm-validations/validators/uniqueness_validator.rb +9 -6
  26. data/lib/dm-validations/validators/within_validator.rb +3 -3
  27. data/spec/fixtures/barcode.rb +10 -2
  28. data/spec/fixtures/basketball_court.rb +7 -7
  29. data/spec/fixtures/basketball_player.rb +3 -3
  30. data/spec/fixtures/beta_tester_account.rb +4 -4
  31. data/spec/fixtures/bill_of_landing.rb +7 -7
  32. data/spec/fixtures/boat_dock.rb +2 -2
  33. data/spec/fixtures/city.rb +1 -1
  34. data/spec/fixtures/company.rb +7 -7
  35. data/spec/fixtures/corporate_world.rb +24 -26
  36. data/spec/fixtures/country.rb +1 -1
  37. data/spec/fixtures/currency.rb +4 -4
  38. data/spec/fixtures/ethernet_frame.rb +5 -5
  39. data/spec/fixtures/event.rb +2 -2
  40. data/spec/fixtures/g3_concert.rb +3 -3
  41. data/spec/fixtures/jabberwock.rb +2 -2
  42. data/spec/fixtures/kayak.rb +2 -2
  43. data/spec/fixtures/lernean_hydra.rb +3 -3
  44. data/spec/fixtures/mathematical_function.rb +2 -2
  45. data/spec/fixtures/memory_object.rb +3 -3
  46. data/spec/fixtures/mittelschnauzer.rb +3 -3
  47. data/spec/fixtures/motor_launch.rb +1 -1
  48. data/spec/fixtures/page.rb +3 -3
  49. data/spec/fixtures/phone_number.rb +2 -2
  50. data/spec/fixtures/pirogue.rb +2 -2
  51. data/spec/fixtures/programming_language.rb +3 -3
  52. data/spec/fixtures/reservation.rb +5 -5
  53. data/spec/fixtures/scm_operation.rb +42 -45
  54. data/spec/fixtures/sms_message.rb +1 -1
  55. data/spec/fixtures/udp_packet.rb +3 -3
  56. data/spec/integration/absent_field_validator/absent_field_validator_spec.rb +6 -6
  57. data/spec/integration/acceptance_validator/acceptance_validator_spec.rb +3 -3
  58. data/spec/integration/automatic_validation/inferred_length_validation_spec.rb +5 -5
  59. data/spec/integration/automatic_validation/spec_helper.rb +1 -1
  60. data/spec/integration/block_validator/block_validator_spec.rb +2 -2
  61. data/spec/integration/conditional_validation/if_condition_spec.rb +3 -3
  62. data/spec/integration/confirmation_validator/confirmation_validator_spec.rb +3 -3
  63. data/spec/integration/datamapper_models/association_validation_spec.rb +5 -5
  64. data/spec/integration/datamapper_models/inheritance_spec.rb +6 -6
  65. data/spec/integration/duplicated_validations/duplicated_validations_spec.rb +3 -3
  66. data/spec/integration/format_validator/email_format_validator_spec.rb +6 -6
  67. data/spec/integration/format_validator/format_validator_spec.rb +8 -8
  68. data/spec/integration/format_validator/regexp_validator_spec.rb +4 -4
  69. data/spec/integration/format_validator/url_format_validator_spec.rb +4 -4
  70. data/spec/integration/length_validator/default_value_spec.rb +3 -3
  71. data/spec/integration/length_validator/equality_spec.rb +4 -4
  72. data/spec/integration/length_validator/error_message_spec.rb +3 -3
  73. data/spec/integration/length_validator/maximum_spec.rb +3 -3
  74. data/spec/integration/length_validator/minimum_spec.rb +3 -3
  75. data/spec/integration/length_validator/range_spec.rb +3 -3
  76. data/spec/integration/method_validator/method_validator_spec.rb +8 -8
  77. data/spec/integration/numeric_validator/equality_with_float_type_spec.rb +7 -7
  78. data/spec/integration/numeric_validator/equality_with_integer_type_spec.rb +5 -5
  79. data/spec/integration/numeric_validator/float_type_spec.rb +3 -3
  80. data/spec/integration/numeric_validator/gt_with_float_type_spec.rb +3 -3
  81. data/spec/integration/numeric_validator/gte_with_float_type_spec.rb +3 -3
  82. data/spec/integration/numeric_validator/lt_with_float_type_spec.rb +3 -3
  83. data/spec/integration/numeric_validator/lte_with_float_type_spec.rb +3 -3
  84. data/spec/integration/primitive_validator/primitive_validator_spec.rb +4 -4
  85. data/spec/integration/pure_ruby_objects/plain_old_ruby_object_validation_spec.rb +4 -4
  86. data/spec/integration/required_field_validator/association_spec.rb +19 -51
  87. data/spec/integration/required_field_validator/boolean_type_value_spec.rb +4 -1
  88. data/spec/integration/required_field_validator/date_type_value_spec.rb +4 -2
  89. data/spec/integration/required_field_validator/datetime_type_value_spec.rb +4 -2
  90. data/spec/integration/required_field_validator/float_type_value_spec.rb +4 -2
  91. data/spec/integration/required_field_validator/integer_type_value_spec.rb +4 -2
  92. data/spec/integration/required_field_validator/plain_old_ruby_object_spec.rb +6 -4
  93. data/spec/integration/required_field_validator/string_type_value_spec.rb +3 -1
  94. data/spec/integration/required_field_validator/text_type_value_spec.rb +3 -1
  95. data/spec/integration/shared/valid_and_invalid_model.rb +8 -0
  96. data/spec/integration/uniqueness_validator/uniqueness_validator_spec.rb +29 -25
  97. data/spec/integration/within_validator/within_validator_spec.rb +6 -6
  98. data/spec/public/resource_spec.rb +47 -9
  99. data/spec/spec_helper.rb +13 -27
  100. data/spec/unit/contextual_validators/emptiness_spec.rb +4 -4
  101. data/spec/unit/contextual_validators/execution_spec.rb +6 -6
  102. data/spec/unit/contextual_validators/spec_helper.rb +3 -3
  103. data/spec/unit/generic_validator/equality_operator_spec.rb +7 -7
  104. data/spec/unit/validation_errors/adding_spec.rb +2 -2
  105. data/spec/unit/validation_errors/emptiness_spec.rb +2 -2
  106. data/spec/unit/validation_errors/enumerable_spec.rb +2 -2
  107. data/tasks/local_gemfile.rake +18 -0
  108. data/tasks/spec.rake +0 -3
  109. metadata +157 -25
@@ -1,5 +1,5 @@
1
1
  module DataMapper
2
- module Validate
2
+ module Validations
3
3
 
4
4
  ##
5
5
  #
@@ -48,9 +48,9 @@ module DataMapper
48
48
  #
49
49
  def validates_within(*fields)
50
50
  opts = opts_from_validator_args(fields)
51
- add_validator_to_context(opts, fields, DataMapper::Validate::WithinValidator)
51
+ add_validator_to_context(opts, fields, DataMapper::Validations::WithinValidator)
52
52
  end
53
53
 
54
54
  end # module ValidatesWithin
55
- end # module Validate
55
+ end # module Validations
56
56
  end # module DataMapper
@@ -1,7 +1,8 @@
1
1
  module DataMapper
2
- module Validate
2
+ module Validations
3
3
  module Fixtures
4
4
  class Barcode
5
+ attr_accessor :valid_hook_call_count
5
6
 
6
7
  #
7
8
  # Behaviors
@@ -21,11 +22,18 @@ module DataMapper
21
22
  # Validations
22
23
  #
23
24
 
24
- validates_length :code, :max => 10
25
+ validates_length_of :code, :max => 10
25
26
 
26
27
  def self.valid_instance
27
28
  new(:code => "3600029145")
28
29
  end
30
+
31
+ # measure the number of times #valid? is executed
32
+ before :valid? do
33
+ @valid_hook_call_count ||= 0
34
+ @valid_hook_call_count += 1
35
+ end
36
+
29
37
  end # Barcode
30
38
  end
31
39
  end
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  module DataMapper
4
- module Validate
4
+ module Validations
5
5
  module Fixtures
6
6
  class BasketballCourt
7
7
  #
@@ -32,14 +32,14 @@ module DataMapper
32
32
  #
33
33
 
34
34
  # obviously these are all metrics
35
- validates_is_number :length, :gte => 15.0, :lte => 15.24
36
- validates_is_number :width, :gte => 25.28, :lte => 28.65
35
+ validates_numericality_of :length, :gte => 15.0, :lte => 15.24
36
+ validates_numericality_of :width, :gte => 25.28, :lte => 28.65
37
37
 
38
38
  # 3 pt line distance may use :gte and :lte, but for
39
39
  # sake of spec example we make it up a little
40
- validates_is_number :three_point_line_distance, :gt => 6.7, :lt => 7.24
41
- validates_is_number :free_throw_line_distance, :equals => 4.57
42
- validates_is_number :rim_height, :eq => 3.05
40
+ validates_numericality_of :three_point_line_distance, :gt => 6.7, :lt => 7.24
41
+ validates_numericality_of :free_throw_line_distance, :equals => 4.57
42
+ validates_numericality_of :rim_height, :eq => 3.05
43
43
 
44
44
  def self.valid_instance(overrides = {})
45
45
  defaults = {
@@ -54,5 +54,5 @@ module DataMapper
54
54
  end
55
55
  end
56
56
  end # Fixtures
57
- end # Validate
57
+ end # Validations
58
58
  end # DataMapper
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  module DataMapper
4
- module Validate
4
+ module Validations
5
5
  module Fixtures
6
6
  class BasketballPlayer
7
7
  #
@@ -27,8 +27,8 @@ module DataMapper
27
27
  #
28
28
 
29
29
  # precision and scale need to be defined for length to be validated
30
- validates_is_number :height, :weight, :precision => 10
30
+ validates_numericality_of :height, :weight, :precision => 10
31
31
  end
32
32
  end # Fixtures
33
- end # Validate
33
+ end # Validations
34
34
  end # DataMapper
@@ -1,5 +1,5 @@
1
1
  module DataMapper
2
- module Validate
2
+ module Validations
3
3
  module Fixtures
4
4
  class BetaTesterAccount
5
5
  #
@@ -24,9 +24,9 @@ module DataMapper
24
24
  # Validations
25
25
  #
26
26
 
27
- validates_is_accepted :user_agreement, :allow_nil => false
28
- validates_is_accepted :newsletter_signup
29
- validates_is_accepted :privacy_agreement, :accept => %w(agreed accept), :message => "You must accept this agreement in order to proceed"
27
+ validates_acceptance_of :user_agreement, :allow_nil => false
28
+ validates_acceptance_of :newsletter_signup
29
+ validates_acceptance_of :privacy_agreement, :accept => %w(agreed accept), :message => "You must accept this agreement in order to proceed"
30
30
  end # BetaTesterAccount
31
31
  end
32
32
  end
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  module DataMapper
4
- module Validate
4
+ module Validations
5
5
  module Fixtures
6
6
  class BillOfLading
7
7
  #
@@ -27,16 +27,16 @@ module DataMapper
27
27
  #
28
28
 
29
29
  # this is a trivial example
30
- validates_format :doc_no, :with => lambda { |code|
30
+ validates_format_of :doc_no, :with => lambda { |code|
31
31
  code =~ /\AA\d{4}\z/ || code =~ /\A[B-Z]\d{6}X12\z/
32
32
  }
33
33
 
34
- validates_format :email, :as => :email_address
35
- validates_format :url, :as => :url, :allow_nil => false, :allow_blank => false
34
+ validates_format_of :email, :as => :email_address
35
+ validates_format_of :url, :as => :url, :allow_nil => false, :allow_blank => false
36
36
 
37
- validates_format :username, :with => /[a-z]/, :message => 'Username must have at least one letter', :allow_nil => true
38
- validates_format :code, :with => /\d{5,6}/, :message => 'Code format is invalid'
37
+ validates_format_of :username, :with => /[a-z]/, :message => 'Username must have at least one letter', :allow_nil => true
38
+ validates_format_of :code, :with => /\d{5,6}/, :message => 'Code format is invalid'
39
39
  end
40
40
  end # Fixtures
41
- end # Validate
41
+ end # Validations
42
42
  end # DataMapper
@@ -1,5 +1,5 @@
1
1
  module DataMapper
2
- module Validate
2
+ module Validations
3
3
  module Fixtures
4
4
  class BoatDock
5
5
  #
@@ -19,7 +19,7 @@ module DataMapper
19
19
  # Validations
20
20
  #
21
21
 
22
- validates_length :name, :min => 3
22
+ validates_length_of :name, :min => 3
23
23
  end
24
24
  end
25
25
  end
@@ -20,5 +20,5 @@ class City
20
20
  # Validations
21
21
  #
22
22
 
23
- validates_is_number :founded_in, :message => "Foundation year must be an integer"
23
+ validates_numericality_of :founded_in, :message => "Foundation year must be an integer"
24
24
  end
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  module DataMapper
4
- module Validate
4
+ module Validations
5
5
  module Fixtures
6
6
  class Company
7
7
  #
@@ -23,7 +23,7 @@ module DataMapper
23
23
  # Validations
24
24
  #
25
25
 
26
- validates_present :title, :message => "Company name is a required field"
26
+ validates_presence_of :title, :message => "Company name is a required field"
27
27
 
28
28
  end
29
29
 
@@ -41,7 +41,7 @@ module DataMapper
41
41
  # Validations
42
42
  #
43
43
 
44
- validates_present :area_of_expertise
44
+ validates_presence_of :area_of_expertise
45
45
  end
46
46
 
47
47
  class ProductCompany < Company
@@ -58,8 +58,8 @@ module DataMapper
58
58
  # Validations
59
59
  #
60
60
 
61
- validates_present :title, :message => "Product company must have a name"
62
- validates_present :flagship_product
61
+ validates_presence_of :title, :message => "Product company must have a name"
62
+ validates_presence_of :flagship_product
63
63
  end
64
64
 
65
65
  class Product
@@ -80,13 +80,13 @@ module DataMapper
80
80
  # Associations
81
81
  #
82
82
 
83
- belongs_to :company, :model => DataMapper::Validate::Fixtures::ProductCompany
83
+ belongs_to :company, :model => DataMapper::Validations::Fixtures::ProductCompany
84
84
 
85
85
  #
86
86
  # Validations
87
87
  #
88
88
 
89
- validates_present :company
89
+ validates_presence_of :company
90
90
  end
91
91
  end
92
92
  end
@@ -1,40 +1,38 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
4
- module DataMapper
5
- module Validate
6
- module Fixtures
7
- class Organisation
8
- include DataMapper::Resource
3
+ module DataMapper
4
+ module Validations
5
+ module Fixtures
6
+ class Organisation
7
+ include DataMapper::Resource
9
8
 
10
- property :id, Serial
11
- property :name, String
12
- property :domain, String, :unique_index => true
9
+ property :id, Serial
10
+ property :name, String
11
+ property :domain, String, :unique_index => true
13
12
 
14
- validates_is_unique :domain, :allow_nil => true
15
- end
13
+ validates_uniqueness_of :domain, :allow_nil => true
14
+ end
16
15
 
17
- class Department
18
- include DataMapper::Resource
16
+ class Department
17
+ include DataMapper::Resource
19
18
 
20
- property :id, Serial
21
- property :name, String, :unique_index => true
19
+ property :id, Serial
20
+ property :name, String, :unique_index => true
22
21
 
23
- validates_is_unique :name
24
- end
22
+ validates_uniqueness_of :name
23
+ end
25
24
 
26
- class User
27
- include DataMapper::Resource
25
+ class User
26
+ include DataMapper::Resource
28
27
 
29
- property :id, Serial
30
- property :user_name, String
28
+ property :id, Serial
29
+ property :user_name, String
31
30
 
32
- belongs_to :organisation
33
- belongs_to :department
31
+ belongs_to :organisation
32
+ belongs_to :department
34
33
 
35
- validates_is_unique :user_name, :when => :signing_up_for_department_account, :scope => [:department]
36
- validates_is_unique :user_name, :when => :signing_up_for_organization_account, :scope => [:organisation]
37
- end
34
+ validates_uniqueness_of :user_name, :when => :signing_up_for_department_account, :scope => [:department]
35
+ validates_uniqueness_of :user_name, :when => :signing_up_for_organization_account, :scope => [:organisation]
38
36
  end
39
37
  end
40
38
  end
@@ -20,5 +20,5 @@ class Country
20
20
  # Validations
21
21
  #
22
22
 
23
- validates_is_number :area, :message => "Please use integers to specify area"
23
+ validates_numericality_of :area, :message => "Please use integers to specify area"
24
24
  end
@@ -1,5 +1,5 @@
1
1
  module DataMapper
2
- module Validate
2
+ module Validations
3
3
  module Fixtures
4
4
  class Currency
5
5
 
@@ -23,9 +23,9 @@ module DataMapper
23
23
  # Validations
24
24
  #
25
25
 
26
- validates_length :name, :within => 1..50
27
- validates_length :code, :is => 3
28
- validates_length :symbol, :is => 1
26
+ validates_length_of :name, :within => 1..50
27
+ validates_length_of :code, :is => 3
28
+ validates_length_of :symbol, :is => 1
29
29
 
30
30
  def self.valid_instance(overrides = {})
31
31
  defaults = {
@@ -1,5 +1,5 @@
1
1
  module DataMapper
2
- module Validate
2
+ module Validations
3
3
  module Fixtures
4
4
 
5
5
  # for pedants: we refer to DIX Ethernet here
@@ -32,11 +32,11 @@ module DataMapper
32
32
  # Validations
33
33
  #
34
34
 
35
- validates_length :destination_mac, :source_mac, :equals => 6
36
- validates_length :ether_type, :equals => 2
37
- validates_length :payload, :min => 46, :max => 1500, :unless => :link_support_fragmentation
35
+ validates_length_of :destination_mac, :source_mac, :equals => 6
36
+ validates_length_of :ether_type, :equals => 2
37
+ validates_length_of :payload, :min => 46, :max => 1500, :unless => :link_support_fragmentation
38
38
  # :is is alias for :equal
39
- validates_length :crc, :is => 4
39
+ validates_length_of :crc, :is => 4
40
40
 
41
41
  def self.valid_instance
42
42
  # these are obvisouly not bits, and not in hexadecimal
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  module DataMapper
4
- module Validate
4
+ module Validations
5
5
  module Fixtures
6
6
 
7
7
  class Event
@@ -40,5 +40,5 @@ module DataMapper
40
40
  end
41
41
  end # Event
42
42
  end # Fixtures
43
- end # Validate
43
+ end # Validations
44
44
  end # DataMapper
@@ -1,14 +1,14 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  module DataMapper
4
- module Validate
4
+ module Validations
5
5
  module Fixtures
6
6
  class G3Concert
7
7
  #
8
8
  # Bahaviors
9
9
  #
10
10
 
11
- include ::DataMapper::Validate
11
+ include ::DataMapper::Validations
12
12
 
13
13
  #
14
14
  # Attributes
@@ -53,5 +53,5 @@ module DataMapper
53
53
  end
54
54
  end # G3Concert
55
55
  end # Fixtures
56
- end # Validate
56
+ end # Validations
57
57
  end # DataMapper
@@ -1,5 +1,5 @@
1
1
  module DataMapper
2
- module Validate
2
+ module Validations
3
3
  module Fixtures
4
4
 
5
5
  class Jabberwock
@@ -20,7 +20,7 @@ module DataMapper
20
20
  # Validations
21
21
  #
22
22
 
23
- validates_length :snickersnack, :within => 3..40, :message => "worble warble"
23
+ validates_length_of :snickersnack, :within => 3..40, :message => "worble warble"
24
24
  end # Jabberwock
25
25
  end
26
26
  end
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  module DataMapper
4
- module Validate
4
+ module Validations
5
5
  module Fixtures
6
6
  class Kayak
7
7
  #
@@ -21,7 +21,7 @@ module DataMapper
21
21
  # Validations
22
22
  #
23
23
 
24
- validates_absent :salesman, :on => :sale
24
+ validates_absence_of :salesman, :on => :sale
25
25
  end
26
26
  end
27
27
  end
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  module DataMapper
4
- module Validate
4
+ module Validations
5
5
  module Fixtures
6
6
  class LerneanHydra
7
7
  #
@@ -24,7 +24,7 @@ module DataMapper
24
24
  # Validations
25
25
  #
26
26
 
27
- validates_is_number :head_count, :eq => 9, :message => "Lernean hydra is said to have exactly 9 heads"
27
+ validates_numericality_of :head_count, :eq => 9, :message => "Lernean hydra is said to have exactly 9 heads"
28
28
 
29
29
  def self.valid_instance(overrides = {})
30
30
  defaults = {
@@ -35,5 +35,5 @@ module DataMapper
35
35
  end
36
36
  end
37
37
  end # Fixtures
38
- end # Validate
38
+ end # Validations
39
39
  end # DataMapper