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

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
# File 'lib/couchrest/mixins/validation.rb', line 51

def self.included(base)
  base.cattr_accessor(:auto_validation)
  base.class_eval <<-EOS, __FILE__, __LINE__
      # Turn off auto validation by default
      @@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
  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)
      autovalidation_check = true
    end
  RUBY_EVAL
end

Instance Method Details

#all_valid?(context = :default) ⇒ Boolean

Begin a recursive walk of the model checking validity

Returns:

  • (Boolean)


115
116
117
# File 'lib/couchrest/mixins/validation.rb', line 115

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.



83
84
85
# File 'lib/couchrest/mixins/validation.rb', line 83

def check_validations(context = :default)
  throw(:halt, false) unless context.nil? || valid?(context)
end

#errorsObject

Return the ValidationErrors



89
90
91
# File 'lib/couchrest/mixins/validation.rb', line 89

def errors
  @errors ||= ValidationErrors.new
end

#recursive_valid?(target, context, state) ⇒ Boolean

Do recursive validity checking

Returns:

  • (Boolean)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/couchrest/mixins/validation.rb', line 121

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

Returns:

  • (Boolean)


109
110
111
# File 'lib/couchrest/mixins/validation.rb', line 109

def valid?(context = :default)
  self.class.validators.execute(context, self)
end

#valid_for_default?Boolean

Alias for valid?(:default)

Returns:

  • (Boolean)


103
104
105
# File 'lib/couchrest/mixins/validation.rb', line 103

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

Returns:

  • (Boolean)


97
98
99
# File 'lib/couchrest/mixins/validation.rb', line 97

def validatable?
  true
end

#validation_property(field_name) ⇒ Object

Get the corresponding Object property, if it exists.



144
145
146
# File 'lib/couchrest/mixins/validation.rb', line 144

def validation_property(field_name)
  properties.find{|p| p.name == field_name}
end

#validation_property_value(name) ⇒ Object



139
140
141
# File 'lib/couchrest/mixins/validation.rb', line 139

def validation_property_value(name)
  self.respond_to?(name, true) ? self.send(name) : nil
end