Module: Validatable

Included in:
Musicality::Meter, Musicality::Note, Musicality::Part, Musicality::Score
Defined in:
lib/musicality/validatable.rb

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 Method Summary collapse

Instance Method Details

#check_methodsObject



11
# File 'lib/musicality/validatable.rb', line 11

def check_methods; []; end

#errorsObject



4
5
6
7
8
9
# File 'lib/musicality/validatable.rb', line 4

def errors
  if @errors.nil?
    self.validate
  end
  return @errors
end

#invalid?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/musicality/validatable.rb', line 40

def invalid?
  !self.valid?
end

#valid?Boolean

Returns:

  • (Boolean)


35
36
37
38
# File 'lib/musicality/validatable.rb', line 35

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

#validatablesObject



12
# File 'lib/musicality/validatable.rb', line 12

def validatables; []; end

#validateObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/musicality/validatable.rb', line 14

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