Module: Validatable

Overview

assumes that @checks is defined as an array of no-arg lambdas, each lambda raising an error (with useful msg) when check fails

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



4
5
6
# File 'lib/music-transcription/validatable.rb', line 4

def errors
  @errors
end

Instance Method Details

#check_methodsObject



6
# File 'lib/music-transcription/validatable.rb', line 6

def check_methods; []; end

#invalid?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/music-transcription/validatable.rb', line 35

def invalid?
  !self.valid?
end

#valid?Boolean

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/music-transcription/validatable.rb', line 30

def valid?
  self.validate
  @errors.empty?
end

#validatablesObject



7
# File 'lib/music-transcription/validatable.rb', line 7

def validatables; []; end

#validateObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/music-transcription/validatable.rb', line 9

def validate
  @errors = []
  
  
  check_methods.each do |check_method|
    begin
      send(check_method)
    rescue StandardError => e
      @errors.push e
    end
  end
  
  validatables.each do |v|
    if v.respond_to?(:validate)
      @errors += v.validate
    end
  end
  
  return @errors
end