Class: Mutations::Command
- Inherits:
-
Object
- Object
- Mutations::Command
- Defined in:
- lib/mutations/command.rb
Class Method Summary collapse
- .create_attr_methods(meth, &block) ⇒ Object
- .input_filters ⇒ Object
- .optional(&block) ⇒ Object
- .required(&block) ⇒ Object
- .run(*args) ⇒ Object
- .run!(*args) ⇒ Object
-
.validate(*args) ⇒ Object
Validates input, but doesn’t call execute.
Instance Method Summary collapse
- #has_errors? ⇒ Boolean
-
#initialize(*args) ⇒ Command
constructor
Instance methods.
- #input_filters ⇒ Object
- #run ⇒ Object
- #run! ⇒ Object
- #validation_outcome(result = nil) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Command
Instance methods
57 58 59 60 61 62 63 64 65 66 67 68 |
# 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 |
Class Method Details
.create_attr_methods(meth, &block) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# 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 |
.input_filters ⇒ Object
44 45 46 47 48 49 50 51 52 |
# 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 |
.optional(&block) ⇒ Object
27 28 29 |
# File 'lib/mutations/command.rb', line 27 def optional(&block) create_attr_methods(:optional, &block) end |
.required(&block) ⇒ Object
23 24 25 |
# File 'lib/mutations/command.rb', line 23 def required(&block) create_attr_methods(:required, &block) end |
.run(*args) ⇒ Object
31 32 33 |
# File 'lib/mutations/command.rb', line 31 def run(*args) new(*args).run end |
.run!(*args) ⇒ Object
35 36 37 |
# File 'lib/mutations/command.rb', line 35 def run!(*args) new(*args).run! end |
.validate(*args) ⇒ Object
Validates input, but doesn’t call execute. Returns an Outcome with errors anyway.
40 41 42 |
# File 'lib/mutations/command.rb', line 40 def validate(*args) new(*args).validation_outcome end |
Instance Method Details
#has_errors? ⇒ Boolean
74 75 76 |
# File 'lib/mutations/command.rb', line 74 def has_errors? !@errors.nil? end |
#input_filters ⇒ Object
70 71 72 |
# File 'lib/mutations/command.rb', line 70 def input_filters self.class.input_filters end |
#run ⇒ Object
78 79 80 81 |
# File 'lib/mutations/command.rb', line 78 def run return validation_outcome if has_errors? validation_outcome(execute) end |
#run! ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/mutations/command.rb', line 83 def run! outcome = run if outcome.success? outcome.result else raise ValidationException.new(outcome.errors) end end |