Class: Mutations::CommandReturningHash
- Defined in:
- lib/mutations/command_returning_hash.rb
Direct Known Subclasses
Class Method Summary collapse
- .create_outcome_attr_methods(meth, &block) ⇒ Object
- .outcome_descriptions ⇒ Object
- .outcome_filters ⇒ Object
- .outcome_optional(&block) ⇒ Object
-
.outcome_required(&block) ⇒ Object
%i(required optional).each do |m| meth = :“outcome_#m” define_method(meth) do |&block| create_outcome_attr_methods(meth, &block) end end.
Instance Method Summary collapse
- #errors ⇒ Object
- #errors? ⇒ Boolean
- #has_outcome_errors? ⇒ Boolean
-
#initialize(*args) ⇒ CommandReturningHash
constructor
A new instance of CommandReturningHash.
- #outcome_filters ⇒ Object
- #run ⇒ Object
- #run! ⇒ Object
- #validation_outcome(result = nil) ⇒ Object
Methods inherited from Command
Constructor Details
#initialize(*args) ⇒ CommandReturningHash
Returns a new instance of CommandReturningHash.
38 39 40 41 |
# File 'lib/mutations/command_returning_hash.rb', line 38 def initialize(*args) super(*args) @outputs = {} end |
Class Method Details
.create_outcome_attr_methods(meth, &block) ⇒ Object
4 5 6 7 8 9 10 11 |
# 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 |
.outcome_descriptions ⇒ Object
33 34 35 |
# File 'lib/mutations/command_returning_hash.rb', line 33 def outcome_descriptions outcome_filters.outcome_descriptions if outcome_filters.respond_to?(:outcome_descriptions) end |
.outcome_filters ⇒ Object
29 30 31 |
# File 'lib/mutations/command_returning_hash.rb', line 29 def outcome_filters @outcome_filters ||= (CommandReturningHash == superclass) ? OutcomeHashFilter.new : superclass.outcome_filters.dup end |
.outcome_optional(&block) ⇒ Object
25 26 27 |
# File 'lib/mutations/command_returning_hash.rb', line 25 def outcome_optional(&block) create_outcome_attr_methods(:outcome_optional, &block) end |
.outcome_required(&block) ⇒ Object
%i(required optional).each do |m|
meth = :"outcome_#{m}"
define_method(meth) do |&block|
create_outcome_attr_methods(meth, &block)
end
end
21 22 23 |
# File 'lib/mutations/command_returning_hash.rb', line 21 def outcome_required(&block) create_outcome_attr_methods(:outcome_required, &block) end |
Instance Method Details
#errors ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/mutations/command_returning_hash.rb', line 55 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? ⇒ Boolean
51 52 53 |
# File 'lib/mutations/command_returning_hash.rb', line 51 def errors? has_errors? || has_outcome_errors? end |
#has_outcome_errors? ⇒ Boolean
47 48 49 |
# File 'lib/mutations/command_returning_hash.rb', line 47 def has_outcome_errors? !@outcome_errors.nil? end |
#outcome_filters ⇒ Object
43 44 45 |
# File 'lib/mutations/command_returning_hash.rb', line 43 def outcome_filters self.class.outcome_filters end |
#run ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/mutations/command_returning_hash.rb', line 64 def run return validation_outcome if has_errors? validation_outcome( execute.tap do |result| if result.is_a?(Hash) _, @outcome_errors = self.class.outcome_filters.filter(result) validate_outcome(result) unless has_outcome_errors? else add_outcome_error :self, :type, "This mutation must return Hash instance (was #{result.class})" end end ) end |
#run! ⇒ Object
78 79 80 |
# File 'lib/mutations/command_returning_hash.rb', line 78 def run! (outcome = run).success? ? outcome.result : (raise ValidationException.new(outcome.errors)) end |
#validation_outcome(result = nil) ⇒ Object
82 83 84 |
# File 'lib/mutations/command_returning_hash.rb', line 82 def validation_outcome(result = nil) Outcome.new(!errors?, filtered(result), errors, @inputs) end |