Module: ActiveModel::Validations::ClassMethods

Defined in:
lib/validates_serialized/validators/hash_validator.rb,
lib/validates_serialized/validators/array_validator.rb,
lib/validates_serialized/validators/hash_block_validator.rb,
lib/validates_serialized/validators/serialized_validator.rb,
lib/validates_serialized/validators/array_block_validator.rb,
lib/validates_serialized/validators/object_block_validator.rb

Instance Method Summary collapse

Instance Method Details

#validates_array_values(*attributes) ⇒ Object

Helper to accept arguments in the style of the validates class method

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/validates_serialized/validators/array_validator.rb', line 40

def validates_array_values(*attributes)
  defaults = attributes.extract_options!.dup
  validations = defaults.slice!(*_validates_default_keys)

  raise ArgumentError, "You need to supply at least one attribute" if attributes.empty?
  raise ArgumentError, "You need to supply at least one validation" if validations.empty?

  defaults[:attributes] = attributes

  validations.each do |key, options|
    next unless options
    key = "#{key.to_s.camelize}Validator"

    begin
      validator = key.include?('::') ? key.constantize : const_get(key)
    rescue NameError
      raise ArgumentError, "Unknown validator: '#{key}'"
    end

    validates_array_values_with(validator, defaults.merge(_parse_validates_options(options)))
  end
end

#validates_array_values!(*attributes) ⇒ Object



63
64
65
66
67
# File 'lib/validates_serialized/validators/array_validator.rb', line 63

def validates_array_values!(*attributes)
  options = attributes.extract_options!
  options[:strict] = true
  validates_array_values(*(attributes << options))
end

#validates_array_values_with(*args, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/validates_serialized/validators/array_validator.rb', line 18

def validates_array_values_with(*args, &block)
  # TODO: It would be nice for these to all make a call to 'validates_serialized_with'
  #    so that there's more code re-use
  # validates_serialized_with args.push(ArrayValidator), &block

  options = args.extract_options!
  options[:class] = self

  validator = ArrayValidator.new(args, options, &block)

  if validator.respond_to?(:attributes) && !validator.attributes.empty?
    validator.attributes.each do |attribute|
      _validators[attribute.to_sym] << validator
    end
  else
    _validators[nil] << validator
  end

  validate(validator, options)
end

#validates_each_in_array(*attr_names, &block) ⇒ Object

Helper to accept arguments in the style of the validates class method

Raises:

  • (ArgumentError)


54
55
56
57
# File 'lib/validates_serialized/validators/array_block_validator.rb', line 54

def validates_each_in_array(*attr_names, &block)
  raise ArgumentError, "You need to supply at least one attribute" if attr_names.empty?
  validates_with ArrayBlockValidator, _merge_attributes(attr_names), &block
end

#validates_each_in_array!(*attr_names, &block) ⇒ Object



59
60
61
62
63
# File 'lib/validates_serialized/validators/array_block_validator.rb', line 59

def validates_each_in_array!(*attr_names, &block)
  options = attr_names.extract_options!
  options[:strict] = true
  validates_each_in_array(*(attr_names << options), &block)
end

#validates_hash_keys(*attr_names, &block) ⇒ Object

Helper to accept arguments in the style of the validates class method

Raises:

  • (ArgumentError)


57
58
59
60
# File 'lib/validates_serialized/validators/hash_block_validator.rb', line 57

def validates_hash_keys(*attr_names, &block)
  raise ArgumentError, "You need to supply at least one attribute" if attr_names.empty?
  validates_with HashBlockValidator, _merge_attributes(attr_names), &block
end

#validates_hash_keys!(*attr_names, &block) ⇒ Object



62
63
64
65
66
# File 'lib/validates_serialized/validators/hash_block_validator.rb', line 62

def validates_hash_keys!(*attr_names, &block)
  options = attr_names.extract_options!
  options[:strict] = true
  validates_hash_keys(*(attr_names << options), &block)
end

#validates_hash_values(*attributes) ⇒ Object

Helper to accept arguments in the style of the validates class method

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/validates_serialized/validators/hash_validator.rb', line 37

def validates_hash_values(*attributes)
  defaults = attributes.extract_options!.dup
  validations = defaults.slice!(*_validates_default_keys)

  raise ArgumentError, "You need to supply at least one attribute" if attributes.empty?
  raise ArgumentError, "You need to supply at least one validation" if validations.empty?

  defaults[:attributes] = attributes

  validations.each do |key, options|
    next unless options
    key = "#{key.to_s.camelize}Validator"

    begin
      validator = key.include?('::') ? key.constantize : const_get(key)
    rescue NameError
      raise ArgumentError, "Unknown validator: '#{key}'"
    end

    validates_hash_values_with(validator, defaults.merge(_parse_validates_options(options)))
  end
end

#validates_hash_values!(*attributes) ⇒ Object



60
61
62
63
64
# File 'lib/validates_serialized/validators/hash_validator.rb', line 60

def validates_hash_values!(*attributes)
  options = attributes.extract_options!
  options[:strict] = true
  validates_hash_values(*(attributes << options))
end

#validates_hash_values_with(*args, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/validates_serialized/validators/hash_validator.rb', line 18

def validates_hash_values_with(*args, &block)
  options = args.extract_options!
  options[:class] = self

  validator = HashValidator.new(args, options, &block)

  if validator.respond_to?(:attributes) && !validator.attributes.empty?
    validator.attributes.each do |attribute|
      _validators[attribute.to_sym] << validator
    end
  else
    _validators[nil] << validator
  end

  validate(validator, options)
end

#validates_serialized(*attr_names, &block) ⇒ Object

Helper to accept arguments in the style of the validates class method

Raises:

  • (ArgumentError)


57
58
59
60
# File 'lib/validates_serialized/validators/object_block_validator.rb', line 57

def validates_serialized(*attr_names, &block)
  raise ArgumentError, "You need to supply at least one attribute" if attr_names.empty?
  validates_with ObjectBlockValidator, _merge_attributes(attr_names), &block
end

#validates_serialized!(*attr_names, &block) ⇒ Object



62
63
64
65
66
# File 'lib/validates_serialized/validators/object_block_validator.rb', line 62

def validates_serialized!(*attr_names, &block)
  options = attr_names.extract_options!
  options[:strict] = true
  validates_serialized(*(attr_names << options), &block)
end

#validates_serialized_with(*args, &block) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/validates_serialized/validators/serialized_validator.rb', line 46

def validates_serialized_with(*args, &block)
  options = args.extract_options!
  options[:class] = self
  serialized_validator_class = args.shift
  validator = serialized_validator_class.new(args, options, &block)

  if validator.respond_to?(:attributes) && !validator.attributes.empty?
    validator.attributes.each do |attribute|
      _validators[attribute.to_sym] << validator
    end
  else
    _validators[nil] << validator
  end

  validate(validator, options)
end