module Command
Constants
- VERSION
Public Class Methods
Override for Module#included
.
@api private
# File lib/commande.rb, line 176 def self.included(base) super base.class_eval do extend ClassMethods end end
Protected Instance Methods
@api private
# File lib/commande.rb, line 393 def _outputs Hash[].tap do |result| self.class.outputs.each do |name, ivar| result[name] = instance_variable_defined?(ivar) ? instance_variable_get(ivar) : nil end end end
@api private
# File lib/commande.rb, line 388 def _prepare! @__result.prepare!(_outputs) end
Copies errors and logs from sources and prefixes with a header:
@param [Commande::Result, ApplicationRecord] source @param [String] header @return [TrueClass, FalseClass] true if successful, false otherwise
# File lib/commande.rb, line 318 def transfer(source, header: nil) transfer_logs(source, header: header) transfer_outputs(source) transfer_errors(source, header: header) transfer_success?(source) end
Copies the status of an interactor or active record object, fail!
if not successful
ATTENTION: your setter needs to be PUBLIC to be copied to.
@param [Commande::Result, ApplicationRecord] source @see transfer
@see Commande
# File lib/commande.rb, line 382 def transfer!(source, header: nil) return if transfer(source, header: header) fail! end
# File lib/commande.rb, line 363 def transfer_errors(source, header: nil) errors = source.errors errors = source.errors.full_messages if errors.respond_to?(:full_messages) Array(errors).each do |e| error header ? ::Kernel.format('%<header>s: %<error>s', header: header, error: e) : e end errors&.length&.positive? end
# File lib/commande.rb, line 326 def transfer_logs(source, header: nil) return unless source.respond_to?(:logs) Array(source.logs).each do |l| log header ? ::Kernel.format('%<header>s: %<log>s', header: header, log: l) : l end end
# File lib/commande.rb, line 333 def transfer_outputs(source) return unless source.respond_to?(:payload) # Copy into current output @__result.prepare!(source.payload) # Copy into current commande source.payload.each do |name, value| setter = :"#{name}=" if respond_to?(setter, true) __send__(setter, value) next end ivar = :"@#{name}" next unless instance_variable_defined?(ivar) instance_variable_set(ivar, value) end end
Checks the success of source and returns it
@param [Commande::Result, ActiveRecord::Base] source @return [TrueClass, FalseClass]
# File lib/commande.rb, line 358 def transfer_success?(source) return source.successful? if source.respond_to?(:successful?) source.valid? && source.persisted? end
Private Instance Methods
Interrupt the current flow without failure
# File lib/commande.rb, line 263 def end! throw :end end
Log an error without interrupting the flow.
When used, the returned result won't be successful.
@param message [String] the error message
@return false
@see Commande#error!
# File lib/commande.rb, line 278 def error(message) @__result.add_error message false end
Log an error AND interrupting the flow.
When used, the returned result won't be successful.
@param message [String] the error message
@see Commande#error
# File lib/commande.rb, line 291 def error!(message) error(message) fail! end
Fail and interrupt the current flow.
# File lib/commande.rb, line 256 def fail! @__result.fail! throw :fail end
Persist a log message
@param message [String] the log message
@return true
@see Commande#error
# File lib/commande.rb, line 304 def log(message) @__result.add_log(message) true end
Check if proceed with #call
invocation. By default it returns true
.
Developers can override it.
@return [TrueClass,FalseClass] the result of the check
# File lib/commande.rb, line 250 def valid?(*) true end