Sha256: 9444aa9d8eff0a68db3e5c9ef5c8e71ce891a90d56b154ca4b5b5c9565bdd692

Contents?: true

Size: 942 Bytes

Versions: 8

Compression:

Stored size: 942 Bytes

Contents

# assumes that @checks is defined as an array of no-arg lambdas, each
# lambda raising an error (with useful msg) when check fails
module Validatable
  attr_reader :errors
  
  def check_methods
    if instance_variable_defined?(:@check_methods)
      methods = instance_variable_get(:@check_methods)
    else
      methods = []
    end
    
    if self.class.class_variable_defined?(:@@check_methods)
      methods += self.class.class_variable_get(:@@check_methods)
    end
    
    return methods
  end
  
  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|
      @errors += v.validate
    end
    
    return @errors
  end
  
  def validatables
    []
  end
  
  def valid?
    self.validate
    @errors.empty?
  end
  
  def invalid?
    !self.valid?
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
music-transcription-0.17.1 lib/music-transcription/validatable.rb
music-transcription-0.17.0 lib/music-transcription/validatable.rb
music-transcription-0.16.0 lib/music-transcription/validatable.rb
music-transcription-0.15.0 lib/music-transcription/validatable.rb
music-transcription-0.14.0 lib/music-transcription/validatable.rb
music-transcription-0.13.0 lib/music-transcription/validatable.rb
music-transcription-0.11.0 lib/music-transcription/validatable.rb
music-transcription-0.10.0 lib/music-transcription/validatable.rb