Module: Attestor::Validations

Extended by:
ClassMethods
Included in:
Policy
Defined in:
lib/attestor/validations.rb,
lib/attestor/validations/message.rb,
lib/attestor/validations/validator.rb,
lib/attestor/validations/validators.rb

Overview

API for objects to be validated

Defined Under Namespace

Modules: ClassMethods Classes: Message, Validator, Validators

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

follow_policy, validators

Class Method Details

.included(klass) ⇒ Object



89
90
91
# File 'lib/attestor/validations.rb', line 89

def self.included(klass)
  klass.instance_eval { extend ClassMethods }
end

Instance Method Details

#invalid(name, options = {}) ⇒ String #invalid(name) ⇒ String

Raises InvalidError with a corresponding message

Overloads:

  • #invalid(name, options = {}) ⇒ String

    Returns translation of symbolic name in the current object’s scope.

    Parameters:

    • name (Symbol)

      the name of the error

    • options (Hash) (defaults to: {})

      the options for symbolic name translation

    Returns:

    • (String)

      translation of symbolic name in the current object’s scope

  • #invalid(name) ⇒ String

    Returns the name converted to string.

    Parameters:

    • name (#to_s)

      the error message (not a symbol)

    Returns:

    • (String)

      the name converted to string



37
38
39
40
# File 'lib/attestor/validations.rb', line 37

def invalid(name, options = {})
  message = Message.new(name, self, options)
  fail InvalidError.new self, [message]
end

#validate(context = :all) ⇒ undefined

Calls all validators for given context

Returns:

  • (undefined)

Raises:

  • (Attestor::Validations::InvalidError)

    if validators fail

  • (NoMethodError)

    if some of validators are not implemented



14
15
16
# File 'lib/attestor/validations.rb', line 14

def validate(context = :all)
  self.class.validators.set(context).each { |item| item.validate(self) }
end