Module: CouchRest::Validation
- Defined in:
- lib/couchrest/mixins/validation.rb,
lib/couchrest/validation/auto_validate.rb,
lib/couchrest/validation/validation_errors.rb,
lib/couchrest/validation/contextual_validators.rb,
lib/couchrest/validation/validators/formats/url.rb,
lib/couchrest/validation/validators/formats/email.rb,
lib/couchrest/validation/validators/format_validator.rb,
lib/couchrest/validation/validators/length_validator.rb,
lib/couchrest/validation/validators/method_validator.rb,
lib/couchrest/validation/validators/generic_validator.rb,
lib/couchrest/validation/validators/numeric_validator.rb,
lib/couchrest/validation/validators/absent_field_validator.rb,
lib/couchrest/validation/validators/confirmation_validator.rb,
lib/couchrest/validation/validators/required_field_validator.rb
Defined Under Namespace
Modules: AutoValidate, ClassMethods, Format, ValidatesAbsent, ValidatesFormat, ValidatesIsConfirmed, ValidatesIsNumber, ValidatesLength, ValidatesPresent, ValidatesWithMethod Classes: AbsentFieldValidator, ConfirmationValidator, ContextualValidators, FormatValidator, GenericValidator, LengthValidator, MethodValidator, NumericValidator, RequiredFieldValidator, ValidationErrors
Class Method Summary collapse
Instance Method Summary collapse
-
#all_valid?(context = :default) ⇒ Boolean
Begin a recursive walk of the model checking validity.
-
#check_validations(context = :default) ⇒ Object
Ensures the object is valid for the context provided, and otherwise throws :halt and returns false.
-
#errors ⇒ Object
Return the ValidationErrors.
-
#recursive_valid?(target, context, state) ⇒ Boolean
Do recursive validity checking.
-
#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_property(field_name) ⇒ Object
Get the corresponding Object property, if it exists.
- #validation_property_value(name) ⇒ Object
Class Method Details
.included(base) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/couchrest/mixins/validation.rb', line 51 def self.included(base) base.extlib_inheritable_accessor(:auto_validation) base.class_eval <<-EOS, __FILE__, __LINE__ # Turn off auto validation by default self.auto_validation ||= false # Force the auto validation for the class properties # This feature is still not fully ported over, # test are lacking, so please use with caution def self.auto_validate! self.auto_validation = true end # share the validations with subclasses def self.inherited(subklass) self.validators.contexts.each do |k, v| subklass.validators.contexts[k] = v.dup end super end EOS base.extend(ClassMethods) base.class_eval <<-EOS, __FILE__, __LINE__ if method_defined?(:_run_save_callbacks) save_callback :before, :check_validations end EOS base.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 def self.define_property(name, options={}) super auto_generate_validations(properties.last) if properties && properties.size > 0 autovalidation_check = true end RUBY_EVAL end |
Instance Method Details
#all_valid?(context = :default) ⇒ Boolean
Begin a recursive walk of the model checking validity
123 124 125 |
# File 'lib/couchrest/mixins/validation.rb', line 123 def all_valid?(context = :default) recursive_valid?(self, context, true) end |
#check_validations(context = :default) ⇒ Object
Ensures the object is valid for the context provided, and otherwise throws :halt and returns false.
91 92 93 |
# File 'lib/couchrest/mixins/validation.rb', line 91 def check_validations(context = :default) throw(:halt, false) unless context.nil? || valid?(context) end |
#errors ⇒ Object
Return the ValidationErrors
97 98 99 |
# File 'lib/couchrest/mixins/validation.rb', line 97 def errors @errors ||= ValidationErrors.new end |
#recursive_valid?(target, context, state) ⇒ Boolean
Do recursive validity checking
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/couchrest/mixins/validation.rb', line 129 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 |
#valid?(context = :default) ⇒ Boolean
Check if a resource is valid in a given context
117 118 119 |
# File 'lib/couchrest/mixins/validation.rb', line 117 def valid?(context = :default) self.class.validators.execute(context, self) end |
#valid_for_default? ⇒ Boolean
Alias for valid?(:default)
111 112 113 |
# File 'lib/couchrest/mixins/validation.rb', line 111 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
105 106 107 |
# File 'lib/couchrest/mixins/validation.rb', line 105 def validatable? true end |
#validation_property(field_name) ⇒ Object
Get the corresponding Object property, if it exists.
152 153 154 |
# File 'lib/couchrest/mixins/validation.rb', line 152 def validation_property(field_name) properties.find{|p| p.name == field_name} end |
#validation_property_value(name) ⇒ Object
147 148 149 |
# File 'lib/couchrest/mixins/validation.rb', line 147 def validation_property_value(name) self.respond_to?(name, true) ? self.send(name) : nil end |