Module: DataMapper::Validate

Defined in:
lib/dm-validations.rb,
lib/dm-validations/formats/url.rb,
lib/dm-validations/auto_validate.rb,
lib/dm-validations/formats/email.rb,
lib/dm-validations/block_validator.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, ValidatesWithBlock, 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

Class Method Details

.included(model) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dm-validations.rb', line 33

def self.included(model)
  model.class_eval <<-EOS, __FILE__, __LINE__
    if method_defined?(:save)
      before :save, :check_validations
    end

    class << self
      def create(attributes = {}, context = :default)
        resource = new(attributes)
        return resource unless resource.valid?(context)
        resource.save!
        resource
      end

      def create!(attributes = {})
        resource = new(attributes)
        resource.save!
        resource
      end
    end
  EOS
end

Instance Method Details

#all_valid?(context = :default) ⇒ Boolean

Begin a recursive walk of the model checking validity

Returns:

  • (Boolean)

97
98
99
# File 'lib/dm-validations.rb', line 97

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.


59
60
61
# File 'lib/dm-validations.rb', line 59

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

#errorsObject

Return the ValidationErrors


71
72
73
# File 'lib/dm-validations.rb', line 71

def errors
  @errors ||= ValidationErrors.new
end

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

Do recursive validity checking

Returns:

  • (Boolean)

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/dm-validations.rb', line 103

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!Object

Calls save with a context of nil, thus skipping validations.


65
66
67
# File 'lib/dm-validations.rb', line 65

def save!
  save(nil)
end

#valid?(context = :default) ⇒ Boolean

Check if a resource is valid in a given context

Returns:

  • (Boolean)

91
92
93
# File 'lib/dm-validations.rb', line 91

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

#valid_for_default?Boolean

Alias for valid?(:default)

Returns:

  • (Boolean)

85
86
87
# File 'lib/dm-validations.rb', line 85

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)

79
80
81
# File 'lib/dm-validations.rb', line 79

def validatable?()
  true
end

#validation_association_keys(name) ⇒ Object


135
136
137
138
139
140
141
142
143
144
145
# File 'lib/dm-validations.rb', line 135

def validation_association_keys(name)
  if model.relationships.has_key?(name)
    result = []
    relation = model.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.


129
130
131
132
133
# File 'lib/dm-validations.rb', line 129

def validation_property(field_name)
  if respond_to?(:model) && (properties = model.properties(self.repository.name)) && properties.has_property?(field_name)
    properties[field_name]
  end
end

#validation_property_value(name) ⇒ Object


121
122
123
# File 'lib/dm-validations.rb', line 121

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