Module: Attestor::Validations::ClassMethods

Included in:
Policy, Attestor::Validations
Defined in:
lib/attestor/validations.rb

Instance Method Summary collapse

Instance Method Details

#validate(name, except: nil, only: nil) ⇒ Attestor::Validators #validate(except: nil, only: nil, &block) ⇒ Attestor::Validators

Uses an instance method or block for validation

Mutates the class by changing its #validators attribute!

Overloads:

  • #validate(name, except: nil, only: nil) ⇒ Attestor::Validators

    Uses the instance method for validation

    Parameters:

    • name (#to_sym)

      The name of the instance method

  • #validate(except: nil, only: nil, &block) ⇒ Attestor::Validators

    Uses the block (in the scope of the instance) for validation

    Parameters:

    • block (Proc)

Parameters:

  • options (Hash)

    a customizable set of options

Returns:

  • (Attestor::Validators)

    the updated list of validators



86
87
88
# File 'lib/attestor/validations.rb', line 86

def validate(*args, &block)
  @validators = validators.add_validator(*args, &block)
end

#validates(name, except: nil, only: nil) ⇒ Attestor::Validators #validates(except: nil, only: nil, &block) ⇒ Attestor::Validators

Delegates a validation to instance method or block

Mutates the class by changing its #validators attribute!

Overloads:

  • #validates(name, except: nil, only: nil) ⇒ Attestor::Validators

    Delegates a validation to instance method

    Parameters:

    • name (#to_sym)

      The name of the instance method that should respond to #validate

  • #validates(except: nil, only: nil, &block) ⇒ Attestor::Validators

    Uses the block (in the scope of the instance) for validation

    Parameters:

    • block (Proc)

      The block that should respond to #validate

Parameters:

  • options (Hash)

    a customizable set of options

Returns:

  • (Attestor::Validators)

    the updated list of validators



110
111
112
# File 'lib/attestor/validations.rb', line 110

def validates(*args, &block)
  @validators = validators.add_delegator(*args, &block)
end

#validations(*options, &block) ⇒ undefined

Groups validations assigned to shared context

Examples:

validations only: :foo do
  validates :bar
  validate  { invalid :foo unless baz }
end

# this is equal to:

validates :bar, only: :foo
validate  { invalid :foo unless baz }, only: :foo

Parameters:

  • options (Hash)
  • block (Proc)

Returns:

  • (undefined)


131
132
133
# File 'lib/attestor/validations.rb', line 131

def validations(*options, &block)
  Context.new(self, Hash[*options]).instance_eval(&block) if block_given?
end

#validatorsAttestor::Validators

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a collection of applied validators

Returns:

  • (Attestor::Validators)


61
62
63
# File 'lib/attestor/validations.rb', line 61

def validators
  @validators ||= Validators.new
end