From edcd0c900e812f0db6dc9df611be3e1cf33084c1 Mon Sep 17 00:00:00 2001 From: Maedi Prichard Date: Sat, 3 Oct 2020 15:59:26 +1000 Subject: [PATCH] Access correct input/output structure. --- lib/Ruler.rb | 71 +++++++++++++++++++++----------------------------- lib/reflekt.rb | 3 +-- 2 files changed, 31 insertions(+), 43 deletions(-) diff --git a/lib/Ruler.rb b/lib/Ruler.rb index 2a9bf7a..238e421 100644 --- a/lib/Ruler.rb +++ b/lib/Ruler.rb @@ -11,32 +11,22 @@ def initialize() @controls = nil @inputs = [] - @outputs = [] + @output = nil end def load(controls) @controls = controls - @controls.each do |control_key, control| + @controls.each_with_index do |control, index| - # TODO: Figure out why timestamp, "p" and "nil" items leaking through. - unless control.class == Hash || control.class == Array - next - end - - if control.class == Array - if control.empty? - next - end - unless control.first.nil? - control = control.first - end - end - - unless control[INPUT].nil? || control[INPUT].empty? + p '-----' + p control + p '-----' - control[INPUT].each_with_index do |input, index| + # Multiple inputs. + control[INPUT].each_with_index do |input, index| + unless input.nil? # Create rule. if @inputs[index].nil? @@ -54,29 +44,30 @@ def load(controls) rule.add_value(input[VALUE]) end + index = index + 1 end end - unless control[OUTPUT].nil? || control[OUTPUT].empty? - control[OUTPUT].each_with_index do |output, index| - - # Create rule. - if @outputs[index].nil? - rule = Rule.new() - @outputs[index] = rule - else - rule = @outputs[index] - end + # Singular output. + output = control[OUTPUT] + unless control[OUTPUT].nil? - # Add rules to rule. - unless output[TYPE].nil? || output[TYPE].empty? - rule.add_type(output[TYPE]) - end - unless output[VALUE].nil? || output[VALUE].empty? - rule.add_value(output[VALUE]) - end + # Create rule. + if @output.nil? + rule = Rule.new() + @output = rule + else + rule = @output + end + ## Add rules to rule. + unless output[TYPE].nil? || output[TYPE].empty? + rule.add_type(output[TYPE]) + end + unless output[VALUE].nil? || output[VALUE].empty? + rule.add_value(output[VALUE]) end + end end @@ -112,12 +103,10 @@ def validate_input(klass, method, inputs) def validate_output(klass, method, outputs) result = true - outputs.each_with_index do |value, index| - rule = @outputs[index] - if rule.is_number? && value.class == Integer - result = false if value < rule.min - result = false if value > rule.max - end + rule = @output + if rule.is_number? && value.class == Integer + result = false if value < rule.min + result = false if value > rule.max end return result end diff --git a/lib/reflekt.rb b/lib/reflekt.rb index dcf8ab7..283336e 100644 --- a/lib/reflekt.rb +++ b/lib/reflekt.rb @@ -206,8 +206,7 @@ def self.reflekt_setup_class() if method_values.key? "controls" ruler = Ruler.new() - # TODO: Remove array with one value. - ruler.load(method_values['controls'].first) + ruler.load(method_values['controls']) ruler.train() @@reflekt_rules[class_name][method_name] = ruler