Module: DataMapper::Validate::AutoValidate

Included in:
ClassMethods
Defined in:
lib/gems/dm-types-0.9.9/lib/dm-types/enum.rb,
lib/gems/dm-validations-0.9.9/lib/dm-validations/auto_validate.rb

Instance Method Summary collapse

Instance Method Details

#auto_generate_validations(property) ⇒ Object

Auto-generate validations for a given property. This will only occur if the option :auto_validation is either true or left undefined.



78
79
80
81
82
83
84
85
# File 'lib/gems/dm-validations-0.9.9/lib/dm-validations/auto_validate.rb', line 78

def auto_generate_validations(property)
  orig_auto_generate_validations(property)
  return unless property.options[:auto_validation]

  if property.type.ancestors.include?(Types::Enum)
    validates_within property.name, options_with_message({:set => property.type.flag_map.values}, property, :within)
  end
end

#options_with_message(base_options, property, validator_name) ⇒ Object

adds message for validator



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gems/dm-validations-0.9.9/lib/dm-validations/auto_validate.rb', line 10

def options_with_message(base_options, property, validator_name)
  options = base_options.clone
  opts = property.options
  options[:message] = if opts[:messages]
    if opts[:messages].is_a?(Hash) and msg = opts[:messages][validator_name]
      msg
    else
      nil
    end
  elsif opts[:message]
    opts[:message]
  else
    nil
  end
  options
end

#orig_auto_generate_validationsObject



58
# File 'lib/gems/dm-types-0.9.9/lib/dm-types/enum.rb', line 58

alias :orig_auto_generate_validations :auto_generate_validations