lib/boxcars/boxcar.rb in boxcars-0.1.1 vs lib/boxcars/boxcar.rb in boxcars-0.1.2
- old
+ new
@@ -24,17 +24,22 @@
def output_keys
raise NotImplementedError
end
# Check that all inputs are present.
+ # @param inputs [Hash] The inputs.
+ # @raise [RuntimeError] If the inputs are not the same.
def validate_inputs(inputs:)
missing_keys = input_keys - inputs.keys
raise "Missing some input keys: #{missing_keys}" if missing_keys.any?
inputs
end
+ # check that all outputs are present
+ # @param outputs [Array<String>] The output keys.
+ # @raise [RuntimeError] If the outputs are not the same.
def validate_outputs(outputs:)
return if outputs.sort == output_keys.sort
raise "Did not get output keys that were expected, got: #{outputs}. Expected: #{output_keys}"
end
@@ -42,10 +47,32 @@
# Run the logic of this chain and return the output.
def call(inputs:)
raise NotImplementedError
end
+ # Apply the boxcar to a list of inputs.
+ # @param input_list [Array<Hash>] The list of inputs.
+ # @return [Array<Boxcars::Boxcar>] The list of outputs.
+ def apply(input_list:)
+ input_list.map { |inputs| new(**inputs) }
+ end
+
+ # Get an answer from the boxcar.
+ # @param args [Array] The positional arguments to pass to the boxcar.
+ # @param kwargs [Hash] The keyword arguments to pass to the boxcar.
+ # you can pass one or the other, but not both.
+ # @return [String] The answer to the question.
+ def run(*args, **kwargs)
+ puts "> Enterning #{name} boxcar#run".colorize(:gray, style: :bold)
+ rv = do_run(*args, **kwargs)
+ puts "< Exiting #{name} boxcar#run".colorize(:gray, style: :bold)
+ rv
+ end
+
+ private
+
+ # Get an answer from the boxcar.
def do_call(inputs:, return_only_outputs: false)
inputs = our_inputs(inputs)
output = nil
begin
output = call(inputs: inputs)
@@ -57,25 +84,9 @@
# memory&.save_convext(inputs: inputs, outputs: outputs)
return output if return_only_outputs
inputs.merge(output)
end
-
- def apply(input_list:)
- input_list.map { |inputs| new(**inputs) }
- end
-
- # Get an answer from the boxcar.
- # @param question [String] The question to ask the boxcar.
- # @return [String] The answer to the question.
- def run(*args, **kwargs)
- puts "> Enterning #{name} boxcar#run".colorize(:gray, style: :bold)
- rv = do_run(*args, **kwargs)
- puts "< Exiting #{name} boxcar#run".colorize(:gray, style: :bold)
- rv
- end
-
- private
def do_run(*args, **kwargs)
if kwargs.empty?
raise Boxcars::ArgumentError, "run supports only one positional argument." if args.length != 1