Module: Attributary::DSL::Validations

Included in:
ClassMethods
Defined in:
lib/attributary/dsl/validations.rb

Instance Method Summary collapse

Instance Method Details

#_add_attributary_error(name, klass, message) ⇒ Object



35
36
37
# File 'lib/attributary/dsl/validations.rb', line 35

def _add_attributary_error(name, klass, message)
  self._attributary_errors[name] = AttributaryError.new(name, klass, message)
end

#_attributary_check_collection(attribute, value, collection) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/attributary/dsl/validations.rb', line 18

def _attributary_check_collection(attribute, value, collection)
  unless collection.include?(value)
    _attributary_handle_error(attribute, value, :collection, :message => "Attribute #{attribute} `#{value}' is not in the collection #{collection}")
    return false
  end
  true
end

#_attributary_handle_error(attribute, value, type, options = {}) ⇒ Object

Raises:

  • (error)


5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/attributary/dsl/validations.rb', line 5

def _attributary_handle_error(attribute, value, type, options = {})
  message = options[:message] || "#{attribute} value #{value} is invalid."
  error = _attributary_config.send("#{type}_error")
  if error.is_a?(Proc)
    error = error.call(attribute, value)
  end
  if quiet_errors?
    _add_attributary_error(attribute, error.class, message)
    return
  end
  raise error, message
end

#_attributary_validate_attribute(attribute, value, validator) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/attributary/dsl/validations.rb', line 26

def _attributary_validate_attribute(attribute, value, validator)
  return true if validator.nil?
  unless validator.call(value)
    _attributary_handle_error(attribute, value, :validation)
    false
  end
  true
end

#quiet_errors?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/attributary/dsl/validations.rb', line 39

def quiet_errors?
  !_attributary_config.raise_errors?
end