Module: DataMapper::Validate
- Defined in:
- lib/dm-validations.rb,
lib/dm-validations/auto_validate.rb,
lib/dm-validations/formats/email.rb,
lib/dm-validations/custom_validator.rb,
lib/dm-validations/format_validator.rb,
lib/dm-validations/length_validator.rb,
lib/dm-validations/method_validator.rb,
lib/dm-validations/within_validator.rb,
lib/dm-validations/generic_validator.rb,
lib/dm-validations/numeric_validator.rb,
lib/dm-validations/validation_errors.rb,
lib/dm-validations/primitive_validator.rb,
lib/dm-validations/acceptance_validator.rb,
lib/dm-validations/uniqueness_validator.rb,
lib/dm-validations/contextual_validators.rb,
lib/dm-validations/absent_field_validator.rb,
lib/dm-validations/confirmation_validator.rb,
lib/dm-validations/required_field_validator.rb
Defined Under Namespace
Modules: AutoValidate, ClassMethods, Format, ValidatesAbsent, ValidatesFormat, ValidatesFormatOf, ValidatesIsAccepted, ValidatesIsConfirmed, ValidatesIsNumber, ValidatesIsPrimitive, ValidatesIsUnique, ValidatesLength, ValidatesPresent, ValidatesWithMethod, ValidatesWithin Classes: AbsentFieldValidator, AcceptanceValidator, ConfirmationValidator, ContextualValidators, CustomValidator, FormatValidator, GenericValidator, LengthValidator, MethodValidator, NumericValidator, PrimitiveValidator, RequiredFieldValidator, UniquenessValidator, ValidationErrors, WithinValidator
Class Method Summary collapse
Instance Method Summary collapse
-
#all_valid?(context = :default) ⇒ Boolean
Begin a recursive walk of the model checking validity.
-
#errors ⇒ Object
Return the ValidationErrors.
-
#recursive_valid?(target, context, state) ⇒ Boolean
Do recursive validity checking.
-
#save_with_validations(context = :default) ⇒ Object
Validate the resource before saving.
-
#valid?(context = :default) ⇒ Boolean
Check if a resource is valid in a given context.
-
#valid_for_default? ⇒ Boolean
Alias for valid?(:default).
-
#validatable? ⇒ Boolean
Mark this resource as validatable.
- #validation_association_keys(name) ⇒ Object
-
#validation_property(field_name) ⇒ Object
Get the corresponding Resource property, if it exists.
- #validation_property_value(name) ⇒ Object
Class Method Details
.included(model) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/dm-validations.rb', line 32 def self.included(model) if model.method_defined?(:save) && !model.method_defined?(:save!) model.send(:alias_method, :save!, :save) model.send(:alias_method, :save, :save_with_validations) end end |
Instance Method Details
#all_valid?(context = :default) ⇒ Boolean
Begin a recursive walk of the model checking validity
75 76 77 |
# File 'lib/dm-validations.rb', line 75 def all_valid?(context = :default) recursive_valid?(self,context,true) end |
#errors ⇒ Object
Return the ValidationErrors
49 50 51 |
# File 'lib/dm-validations.rb', line 49 def errors @errors ||= ValidationErrors.new end |
#recursive_valid?(target, context, state) ⇒ Boolean
Do recursive validity checking
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/dm-validations.rb', line 81 def recursive_valid?(target, context, state) valid = state target.instance_variables.each do |ivar| ivar_value = target.instance_variable_get(ivar) if ivar_value.validatable? valid = valid && recursive_valid?(ivar_value,context,valid) elsif ivar_value.respond_to?(:each) ivar_value.each do |item| if item.validatable? valid = valid && recursive_valid?(item,context,valid) end end end end return valid && target.valid? end |
#save_with_validations(context = :default) ⇒ Object
Validate the resource before saving. Use #save! to save the record without validations.
42 43 44 45 |
# File 'lib/dm-validations.rb', line 42 def save_with_validations(context = :default) return false unless valid?(context) save! end |
#valid?(context = :default) ⇒ Boolean
Check if a resource is valid in a given context
69 70 71 |
# File 'lib/dm-validations.rb', line 69 def valid?(context = :default) self.class.validators.execute(context,self) end |
#valid_for_default? ⇒ Boolean
Alias for valid?(:default)
63 64 65 |
# File 'lib/dm-validations.rb', line 63 def valid_for_default? valid?(:default) end |
#validatable? ⇒ Boolean
Mark this resource as validatable. When we validate associations of a resource we can check if they respond to validatable? before trying to recursivly validate them
57 58 59 |
# File 'lib/dm-validations.rb', line 57 def validatable?() true end |
#validation_association_keys(name) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/dm-validations.rb', line 115 def validation_association_keys(name) if self.class.relationships.has_key?(name) result = [] relation = self.class.relationships[name] relation.child_key.each do |key| result << key.name end return result end nil end |
#validation_property(field_name) ⇒ Object
Get the corresponding Resource property, if it exists.
Note: DataMapper validations can be used on non-DataMapper resources. In such cases, the return value will be nil.
109 110 111 112 113 |
# File 'lib/dm-validations.rb', line 109 def validation_property(field_name) if DataMapper::Resource > self.class self.class.properties(self.repository.name)[field_name] end end |
#validation_property_value(name) ⇒ Object
99 100 101 102 103 |
# File 'lib/dm-validations.rb', line 99 def validation_property_value(name) return self.instance_variable_get("@#{name}") if self.instance_variables.include?(name) return self.send(name) if self.respond_to?(name) nil end |