Skip to content

Instantly share code, notes, and snippets.

@Dysp

Dysp/_kincom.rb Secret

Last active August 12, 2016 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dysp/f5dfd0b73aac8339ef21c4269f7be1e1 to your computer and use it in GitHub Desktop.
Save Dysp/f5dfd0b73aac8339ef21c4269f7be1e1 to your computer and use it in GitHub Desktop.
class KincomValidator
def initialize(file, options)
@file = file
@options = options
end
def validate
is_file_valid?
is_options_valid?
end
private
def is_file_valid?
validate_file_extension
end
def is_options_valid?
validate_options_hash
validate_begin_percentage(@options[:begin_percentage])
validate_prestress(@options[:prestress])
validate_stop_index(@options[:stop_index])
validate_arm(@options[:arm])
validate_name(@options[:name])
validate_time_values(@options[:time_values])
end
def validate_file_extension
if @file.class.extname(@file) != '.xlsx'
raise ArgumentError, 'File-type is wrong. Can only read .xlsx files.'
end
end
def validate_options_hash
@options.each do |key, value|
if key.nil? || value.nil?
raise ArgumentError, "The options_hash contains a nil. Shouldn't, really."
end
end
end
def validate_begin_percentage(begin_percentage)
if !begin_percentage.is_a? Float
raise ArgumentError, "Begin percentage: '#{begin_percentage}' must be a Float. It is a #{begin_percentage.class}."
end
if begin_percentage <= 0 || begin_percentage >= 1
raise ArgumentError, 'Begin percentage must be between 0 and 1'
end
end
def validate_prestress(prestress)
if !prestress.is_a? Float
raise ArgumentError, "Prestress: '#{prestress}' must be a Float. It is a #{prestress.class}."
end
end
def validate_stop_index(stop_index)
if !stop_index.is_a? Float
raise ArgumentError, "Stop_index: '#{stop_index}' must be a Float. It is a #{stop_index.class}."
end
if stop_index <= 0 || stop_index >= 1
raise ArgumentError, 'Stop_index must be between 0 and 1'
end
end
def validate_arm(arm)
if !arm.is_a? Float
raise ArgumentError, "Arm: '#{arm}' must be a Float. It is a #{arm.class}."
end
end
def validate_name(name)
if !name.is_a? String
raise ArgumentError, "Name: '#{name}' must be a String. It is a #{name.class}."
end
end
def validate_time_values(time_values)
if time_values != time_values.sort
raise ArgumentError, 'Time values should be of ascending sizes'
end
end
end
ERROR["test_kincom", TestXercise, 0.0009512920223642141]
test_kincom#TestXercise (0.00s)
NoMethodError: NoMethodError: undefined method `is_file_valid?' for KincomValidator:Module
D:/Arbejde/ResearchLab/Plugin/xercise/validation/_kincom.rb:4:in `validate'
D:/Arbejde/ResearchLab/Plugin/xercise/xercise.rb:12:in `kincom'
D:/Arbejde/ResearchLab/Plugin/myowntest/test_xerciseclass/xercise_test.rb:11:in `test_kincom'
require_relative '_kincom'
module ValidationHelper
def self.kincom
include KincomValidator
end
end
require_relative './validation/validation_helper.rb'
module Xercise
def self.kincom(file, options)
ValidationHelper.kincom
KincomValidator.validate(file, options)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment