diff --git a/lib/dm-validations/validators/absent_field_validator.rb b/lib/dm-validations/validators/absent_field_validator.rb index f4d14d74..ba3caf1f 100644 --- a/lib/dm-validations/validators/absent_field_validator.rb +++ b/lib/dm-validations/validators/absent_field_validator.rb @@ -48,13 +48,8 @@ module ValidatesAbsence # # all three attributes are blank # end # - def validates_absence_of(*fields) - DataMapper::Validations.add_validator_to_context( - self, - DataMapper::Validations.opts_from_validator_args(fields), - fields, - DataMapper::Validations::AbsenceValidator - ) + def validates_absence_of(*args) + validators.add(AbsenceValidator, *args) end deprecate :validates_absent, :validates_absence_of diff --git a/lib/dm-validations/validators/acceptance_validator.rb b/lib/dm-validations/validators/acceptance_validator.rb index e7bac571..857e28ed 100644 --- a/lib/dm-validations/validators/acceptance_validator.rb +++ b/lib/dm-validations/validators/acceptance_validator.rb @@ -68,13 +68,8 @@ module ValidatesAcceptance # # and # # terms_accepted is one of ["1", 1, "true", true, "t"] # - def validates_acceptance_of(*fields) - DataMapper::Validations.add_validator_to_context( - self, - DataMapper::Validations.opts_from_validator_args(fields), - fields, - DataMapper::Validations::AcceptanceValidator - ) + def validates_acceptance_of(*args) + validators.add(AcceptanceValidator, *args) end deprecate :validates_is_accepted, :validates_acceptance_of diff --git a/lib/dm-validations/validators/block_validator.rb b/lib/dm-validations/validators/block_validator.rb index 483da5de..aeb98e92 100644 --- a/lib/dm-validations/validators/block_validator.rb +++ b/lib/dm-validations/validators/block_validator.rb @@ -50,15 +50,11 @@ def validates_with_block(*fields, &block) method_name = "__validates_with_block_#{@__validates_with_block_count}".to_sym define_method(method_name, block) - opts = DataMapper::Validations.opts_from_validator_args(fields) - opts[:method] = method_name + options = fields.last.is_a?(Hash) ? fields.last.pop.dup : {} + options[:method] = method_name + fields = [method_name] if fields.empty? - DataMapper::Validations.add_validator_to_context( - self, - opts, - fields.empty? ? [method_name] : fields, - DataMapper::Validations::MethodValidator - ) + validators.add(MethodValidator, fields, options) end end # module ValidatesWithMethod end # module Validations diff --git a/lib/dm-validations/validators/confirmation_validator.rb b/lib/dm-validations/validators/confirmation_validator.rb index 5817ee61..30b9fc1c 100644 --- a/lib/dm-validations/validators/confirmation_validator.rb +++ b/lib/dm-validations/validators/confirmation_validator.rb @@ -82,12 +82,7 @@ module ValidatesConfirmation # # email == email_repeated # def validates_confirmation_of(*fields) - DataMapper::Validations.add_validator_to_context( - self, - DataMapper::Validations.opts_from_validator_args(fields), - fields, - DataMapper::Validations::ConfirmationValidator - ) + validators.add(ConfirmationValidator, *fields) end deprecate :validates_is_confirmed, :validates_confirmation_of diff --git a/lib/dm-validations/validators/format_validator.rb b/lib/dm-validations/validators/format_validator.rb index d7686491..c7d601ad 100644 --- a/lib/dm-validations/validators/format_validator.rb +++ b/lib/dm-validations/validators/format_validator.rb @@ -115,12 +115,7 @@ module ValidatesFormat # # zip_code is a string of 5 digits # def validates_format_of(*fields) - DataMapper::Validations.add_validator_to_context( - self, - DataMapper::Validations.opts_from_validator_args(fields), - fields, - DataMapper::Validations::FormatValidator - ) + validators.add(FormatValidator, *fields) end deprecate :validates_format, :validates_format_of diff --git a/lib/dm-validations/validators/generic_validator.rb b/lib/dm-validations/validators/generic_validator.rb index e4e2038b..e34879c3 100644 --- a/lib/dm-validations/validators/generic_validator.rb +++ b/lib/dm-validations/validators/generic_validator.rb @@ -178,7 +178,7 @@ def get_resource_property(resource, property_name) model = resource.model if resource.respond_to?(:model) repository = resource.repository if model properties = model.properties(repository.name) if model - properties[property_name] if properties + properties[property_name] if properties end end # class GenericValidator diff --git a/lib/dm-validations/validators/length_validator.rb b/lib/dm-validations/validators/length_validator.rb index 0599272e..2441c759 100644 --- a/lib/dm-validations/validators/length_validator.rb +++ b/lib/dm-validations/validators/length_validator.rb @@ -240,12 +240,7 @@ module ValidatesLength # # just_right is between 1 and 10 (inclusive of both 1 and 10) # def validates_length_of(*fields) - DataMapper::Validations.add_validator_to_context( - self, - DataMapper::Validations.opts_from_validator_args(fields), - fields, - DataMapper::Validations::LengthValidator - ) + validators.add(LengthValidator, *fields) end deprecate :validates_length, :validates_length_of diff --git a/lib/dm-validations/validators/method_validator.rb b/lib/dm-validations/validators/method_validator.rb index d8f5ee8b..fb3a9a56 100644 --- a/lib/dm-validations/validators/method_validator.rb +++ b/lib/dm-validations/validators/method_validator.rb @@ -57,12 +57,7 @@ module ValidatesWithMethod # # wrong zip code" unless zip_code == "94301" # end def validates_with_method(*fields) - DataMapper::Validations.add_validator_to_context( - self, - DataMapper::Validations.opts_from_validator_args(fields), - fields, - DataMapper::Validations::MethodValidator - ) + validators.add(MethodValidator, *fields) end end # module ValidatesWithMethod end # module Validations diff --git a/lib/dm-validations/validators/numeric_validator.rb b/lib/dm-validations/validators/numeric_validator.rb index 6315a7f9..0a2e095a 100644 --- a/lib/dm-validations/validators/numeric_validator.rb +++ b/lib/dm-validations/validators/numeric_validator.rb @@ -173,12 +173,7 @@ module ValidatesNumericality # Use to restrict allowed values to integers. # def validates_numericality_of(*fields) - DataMapper::Validations.add_validator_to_context( - self, - DataMapper::Validations.opts_from_validator_args(fields), - fields, - DataMapper::Validations::NumericalityValidator - ) + validators.add(NumericalityValidator, *fields) end deprecate :validates_is_number, :validates_numericality_of diff --git a/lib/dm-validations/validators/primitive_validator.rb b/lib/dm-validations/validators/primitive_validator.rb index 7f9b5c6a..2ceeac4f 100644 --- a/lib/dm-validations/validators/primitive_validator.rb +++ b/lib/dm-validations/validators/primitive_validator.rb @@ -49,12 +49,7 @@ module ValidatesPrimitiveType # # casted into a Date object. # end def validates_primitive_type_of(*fields) - DataMapper::Validations.add_validator_to_context( - self, - DataMapper::Validations.opts_from_validator_args(fields), - fields, - DataMapper::Validations::PrimitiveTypeValidator - ) + validators.add(PrimitiveTypeValidator, *fields) end deprecate :validates_is_primitive, :validates_primitive_type_of diff --git a/lib/dm-validations/validators/required_field_validator.rb b/lib/dm-validations/validators/required_field_validator.rb index d70cd888..9999ffaa 100644 --- a/lib/dm-validations/validators/required_field_validator.rb +++ b/lib/dm-validations/validators/required_field_validator.rb @@ -74,12 +74,7 @@ module ValidatesPresence # # all three attributes are !blank? # end def validates_presence_of(*fields) - DataMapper::Validations.add_validator_to_context( - self, - DataMapper::Validations.opts_from_validator_args(fields), - fields, - DataMapper::Validations::PresenceValidator - ) + validators.add(PresenceValidator, *fields) end deprecate :validates_present, :validates_presence_of diff --git a/lib/dm-validations/validators/uniqueness_validator.rb b/lib/dm-validations/validators/uniqueness_validator.rb index b650feb4..67979ae6 100644 --- a/lib/dm-validations/validators/uniqueness_validator.rb +++ b/lib/dm-validations/validators/uniqueness_validator.rb @@ -58,12 +58,7 @@ module ValidatesUniqueness # Validate the uniqueness of a field # def validates_uniqueness_of(*fields) - DataMapper::Validations.add_validator_to_context( - self, - DataMapper::Validations.opts_from_validator_args(fields), - fields, - DataMapper::Validations::UniquenessValidator - ) + validators.add(UniquenessValidator, *fields) end deprecate :validates_is_unique, :validates_uniqueness_of diff --git a/lib/dm-validations/validators/within_validator.rb b/lib/dm-validations/validators/within_validator.rb index c4ced9eb..6fa1fff3 100644 --- a/lib/dm-validations/validators/within_validator.rb +++ b/lib/dm-validations/validators/within_validator.rb @@ -45,12 +45,7 @@ module ValidatesWithin # Validate that value of a field if within a range/set # def validates_within(*fields) - DataMapper::Validations.add_validator_to_context( - self, - DataMapper::Validations.opts_from_validator_args(fields), - fields, - DataMapper::Validations::WithinValidator - ) + validators.add(WithinValidator, *fields) end end # module ValidatesWithin end # module Validations