Module: Attestor::Validations

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

Overview

API for objects to be validated

Defined Under Namespace

Modules: ClassMethods, Reporter Classes: Context, Delegator, Message, Validator, Validators

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

validates, validations, validators

Class Method Details

.included(klass) ⇒ Object



138
139
140
# File 'lib/attestor/validations.rb', line 138

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



48
49
50
51
# File 'lib/attestor/validations.rb', line 48

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 and return validation results

Parameters:

  • context (#to_sym) (defaults to: :all)

Returns:

  • (undefined)


25
26
27
# File 'lib/attestor/validations.rb', line 25

def validate(context = :all)
  self.class.validators.set(context).validate self
end

#validate!(context = :all) ⇒ undefined

Calls all validators for given context

Parameters:

  • context (#to_sym) (defaults to: :all)

Returns:

  • (undefined)

Raises:

  • (Attestor::Validations::InvalidError)

    if validators fail

  • (NoMethodError)

    if some of validators are not implemented



16
17
18
# File 'lib/attestor/validations.rb', line 16

def validate!(context = :all)
  self.class.validators.set(context).validate! self
end