class Mutations::Command
Attributes
inputs[R]
raw_inputs[R]
Public Class Methods
input_filters()
click to toggle source
# File lib/mutations/command.rb, line 44 def input_filters @input_filters ||= begin if Command == self.superclass HashFilter.new else self.superclass.input_filters.dup end end end
new(*args)
click to toggle source
Instance methods
# File lib/mutations/command.rb, line 57 def initialize(*args) @raw_inputs = args.inject({}.with_indifferent_access) do |h, arg| raise ArgumentError.new("All arguments must be hashes") unless arg.respond_to?(:to_hash) h.merge!(arg) end # Do field-level validation / filtering: @inputs, @errors = self.input_filters.filter(@raw_inputs) # Run a custom validation method if supplied: validate unless has_errors? end
optional(&block)
click to toggle source
# File lib/mutations/command.rb, line 27 def optional(&block) create_attr_methods(:optional, &block) end
required(&block)
click to toggle source
# File lib/mutations/command.rb, line 23 def required(&block) create_attr_methods(:required, &block) end
run(*args)
click to toggle source
# File lib/mutations/command.rb, line 31 def run(*args) new(*args).run end
run!(*args)
click to toggle source
# File lib/mutations/command.rb, line 35 def run!(*args) new(*args).run! end
validate(*args)
click to toggle source
Validates input, but doesn't call execute. Returns an Outcome
with errors anyway.
# File lib/mutations/command.rb, line 40 def validate(*args) new(*args).validation_outcome end
Private Class Methods
create_attr_methods(meth, &block)
click to toggle source
# File lib/mutations/command.rb, line 4 def create_attr_methods(meth, &block) self.input_filters.send(meth, &block) keys = self.input_filters.send("#{meth}_keys") keys.each do |key| define_method(key) do @inputs[key] end define_method("#{key}_present?") do @inputs.has_key?(key) end define_method("#{key}=") do |v| @inputs[key] = v end end end
Public Instance Methods
has_errors?()
click to toggle source
# File lib/mutations/command.rb, line 74 def has_errors? !@errors.nil? end
input_filters()
click to toggle source
# File lib/mutations/command.rb, line 70 def input_filters self.class.input_filters end
run()
click to toggle source
# File lib/mutations/command.rb, line 78 def run return validation_outcome if has_errors? validation_outcome(execute) end
run!()
click to toggle source
# File lib/mutations/command.rb, line 83 def run! outcome = run if outcome.success? outcome.result else raise ValidationException.new(outcome.errors) end end
validation_outcome(result = nil)
click to toggle source
# File lib/mutations/command.rb, line 92 def validation_outcome(result = nil) Outcome.new(!has_errors?, has_errors? ? nil : result, @errors, @inputs) end
Protected Instance Methods
add_error(key, kind, message = nil)
click to toggle source
add_error
(“name”, :too_short) add_error
(“colors.foreground”, :not_a_color) # => to create errors = {colors: {foreground: :not_a_color}} or, supply a custom message: add_error
(“name”, :too_short, “The name 'blahblahblah' is too short!”)
# File lib/mutations/command.rb, line 112 def add_error(key, kind, message = nil) raise ArgumentError.new("Invalid kind") unless kind.is_a?(Symbol) @errors ||= ErrorHash.new @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(last.to_sym, kind, :message => message) end end
execute()
click to toggle source
# File lib/mutations/command.rb, line 104 def execute # Meant to be overridden end
merge_errors(hash)
click to toggle source
# File lib/mutations/command.rb, line 126 def merge_errors(hash) if hash.any? @errors ||= ErrorHash.new @errors.merge!(hash) end end
validate()
click to toggle source
# File lib/mutations/command.rb, line 100 def validate # Meant to be overridden end