Module: Adama::Validator

Defined in:
lib/adama/validator.rb

Defined Under Namespace

Modules: ClassMethods Classes: PresenceValidator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



3
4
5
# File 'lib/adama/validator.rb', line 3

def self.prepended(base)
  base.extend ClassMethods
end

Instance Method Details

#errorsObject



35
36
37
# File 'lib/adama/validator.rb', line 35

def errors
  @errors ||= {}
end

#initialize(**kwargs) ⇒ Object

This module is meant to be prepended to another module Call the child class initializer first, this will set kwargs Then validate



30
31
32
33
# File 'lib/adama/validator.rb', line 30

def initialize(**kwargs)
  super(**kwargs)
  validate!
end

#valid?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/adama/validator.rb', line 39

def valid?
  @valid
end

#validate!Object

Iterate over the validators registered, and for each validator call ‘validate!` passing in the instance of the class this module was prepended to.



46
47
48
49
50
51
52
53
# File 'lib/adama/validator.rb', line 46

def validate!
  @valid = true
  self.class.validators.each do |validator|
    validator.validate! self
    merge_errors validator.errors
    @valid = validator.valid? && @valid
  end
end