Class: Attestor::Validations::Validator Private

Inherits:
Object
  • Object
show all
Defined in:
lib/attestor/validations/validator.rb

Overview

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

Describe a validator for class instances

Examples:

validator = Validator.new(:foo, only: :baz)

validator.used_in_context? :baz # => true
validator.validate object

Direct Known Subclasses

Follower

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.nameSymbol (readonly)

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.

The name of the item



42
43
44
# File 'lib/attestor/validations/validator.rb', line 42

def name
  @name
end

Class Method Details

.==(other) ⇒ Boolean

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.

Compares an item to another one



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

def ==(other)
  other.instance_of?(self.class) ? id.equal?(other.id) : false
end

.initialize(name, except: nil, only: nil, &block) ⇒ Object

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.



29
30
31
32
33
34
35
36
# File 'lib/attestor/validations/validator.rb', line 29

def initialize(name, except: nil, only: nil, &block)
  @name      = name.to_sym
  @whitelist = normalize(only)
  @blacklist = normalize(except)
  @block     = block
  generate_id
  freeze
end

.new(name, except: [], only: []) ⇒ Attestor::Validations::Validator

Creates a named item with blacklist or whitelist of contexts



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


.used_in_context?(context) ⇒ Boolean

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.

Checks if the item should be used in given context



58
59
60
61
# File 'lib/attestor/validations/validator.rb', line 58

def used_in_context?(context)
  symbol = context.to_sym
  whitelisted?(symbol) && !blacklisted?(symbol)
end

.validate(object) ⇒ undefined

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.

Validates given object

Raises:



71
72
73
# File 'lib/attestor/validations/validator.rb', line 71

def validate(object)
  block ? object.instance_eval(&block) : object.__send__(name)
end