Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Switch over to using ContextualValidator#add.
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuel committed May 30, 2011
1 parent f1a598a commit c874faf
Show file tree
Hide file tree
Showing 13 changed files with 18 additions and 77 deletions.
9 changes: 2 additions & 7 deletions lib/dm-validations/validators/absent_field_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions lib/dm-validations/validators/acceptance_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 4 additions & 8 deletions lib/dm-validations/validators/block_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions lib/dm-validations/validators/confirmation_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions lib/dm-validations/validators/format_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/dm-validations/validators/generic_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions lib/dm-validations/validators/length_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions lib/dm-validations/validators/method_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions lib/dm-validations/validators/numeric_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions lib/dm-validations/validators/primitive_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions lib/dm-validations/validators/required_field_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions lib/dm-validations/validators/uniqueness_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions lib/dm-validations/validators/within_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c874faf

Please sign in to comment.