class Mutations::CommandReturningHash
Attributes
inputs[R]
outputs[R]
raw_inputs[R]
Public Class Methods
new(*args)
click to toggle source
Calls superclass method
# File lib/mutations/command_returning_hash.rb, line 41 def initialize(*args) super(*args) @outputs = {} end
outcome_descriptions()
click to toggle source
# File lib/mutations/command_returning_hash.rb, line 35 def outcome_descriptions outcome_filters.outcome_descriptions if outcome_filters.respond_to?(:outcome_descriptions) end
Also aliased as: output_descriptions
outcome_filters()
click to toggle source
# File lib/mutations/command_returning_hash.rb, line 31 def outcome_filters @outcome_filters ||= (CommandReturningHash == superclass) ? OutcomeHashFilter.new : superclass.outcome_filters.dup end
outcome_optional(&block)
click to toggle source
# File lib/mutations/command_returning_hash.rb, line 26 def outcome_optional(&block) create_outcome_attr_methods(:outcome_optional, &block) end
Also aliased as: optional_output
outcome_required(&block)
click to toggle source
%i(required optional).each do |m|
meth = :"outcome_#{m}" define_method(meth) do |&block| create_outcome_attr_methods(meth, &block) end
end
# File lib/mutations/command_returning_hash.rb, line 21 def outcome_required(&block) create_outcome_attr_methods(:outcome_required, &block) end
Also aliased as: required_output
Private Class Methods
create_outcome_attr_methods(meth, &block)
click to toggle source
# File lib/mutations/command_returning_hash.rb, line 4 def create_outcome_attr_methods(meth, &block) outcome_filters.send(meth, &block) keys = outcome_filters.send("#{meth}_keys") keys.each do |key| define_method("outcome_#{key}") { @outputs[key] } define_method("outcome_#{key}_present?") { @outputs.key?(key) } end end
Public Instance Methods
errors()
click to toggle source
# File lib/mutations/command_returning_hash.rb, line 58 def errors return nil unless errors? ErrorHash.new.tap do |h| h.merge! @errors if has_errors? h.merge! @outcome_errors if has_outcome_errors? end end
errors?()
click to toggle source
# File lib/mutations/command_returning_hash.rb, line 54 def errors? has_errors? || has_outcome_errors? end
has_outcome_errors?()
click to toggle source
# File lib/mutations/command_returning_hash.rb, line 50 def has_outcome_errors? !@outcome_errors.nil? end
outcome_filters()
click to toggle source
# File lib/mutations/command_returning_hash.rb, line 46 def outcome_filters self.class.outcome_filters end
run()
click to toggle source
# File lib/mutations/command_returning_hash.rb, line 67 def run return validation_outcome if has_errors? validation_outcome( execute.tap do |result| case result when Hash _, @outcome_errors = self.class.outcome_filters.filter(result) validate_outcome(result) unless has_outcome_errors? when NilClass then nil else add_outcome_error :self, :type, "This mutation must return Hash instance (was #{result.class})" end end ) end
run!()
click to toggle source
# File lib/mutations/command_returning_hash.rb, line 82 def run! (outcome = run).success? ? outcome.result : (raise ValidationException.new(outcome.errors)) end
validation_outcome(result = nil)
click to toggle source
# File lib/mutations/command_returning_hash.rb, line 86 def validation_outcome(result = nil) Outcome.new(!errors?, filtered(result), errors, @inputs) end
Protected Instance Methods
add_outcome_error(key, kind, message = nil)
click to toggle source
add_outcome_error
(“name”, :too_short) add_outcome_error
(“colors.foreground”, :not_a_color) # => to create errors = {colors: {foreground: :not_a_color}} or, supply a custom message: add_outcome_error
(“name”, :too_short, “The name 'blahblahblah' is too short!”)
# File lib/mutations/command_returning_hash.rb, line 106 def add_outcome_error(key, kind, message = nil) raise ArgumentError.new("Invalid kind") unless kind.is_a?(Symbol) @outcome_errors ||= ErrorHash.new @outcome_errors.tap do |errs| path = key.to_s.split(".") last = path.pop inner = path.inject(errs) do |cur_errors, part| cur_errors[part.to_sym] ||= ErrorHash.new end inner[last] = ErrorAtom.new(key, kind, message: message) end end
filtered(result)
click to toggle source
# File lib/mutations/command_returning_hash.rb, line 98 def filtered result @outputs = result.is_a?(Hash) && has_outcome_errors? ? result.reject { |k, _| @outcome_errors[k.to_sym] } : result end
merge_outcome_errors(hash)
click to toggle source
# File lib/mutations/command_returning_hash.rb, line 120 def merge_outcome_errors(hash) (@outcome_errors ||= ErrorHash.new).tap do |errs| errs.merge!(hash) if hash.any? end end
validate_outcome(outcome)
click to toggle source
# File lib/mutations/command_returning_hash.rb, line 94 def validate_outcome outcome # Meant to be overridden end