module Commande::Interface

Commande interface @since 1.1.0

Public Instance Methods

call(*args, &block) click to toggle source

Triggers the operation and return a result.

All the instance variables marked as output will be available in the result.

@return [Commande::Result] the result of the operation

@raise [NoMethodError] if this isn't implemented by the including class.

@example Expose instance variables in result payload as output

class Purchase
  include Commande
  output :buyer, :product, :transaction

  def call(buyer:, product_code:)
    @product = Product.find_by(product_code: product_code)
    @buyer = Buyer.find_by(email: buyer)
    @transaction = Transaction.create(buyer: @buyer, product: @product)
  end
end

result = Purchase.new.call(buyer: 'john@smith.com', product_code: 'i23af')
result.failure? # => false
result.successful? # => true

result.product  # => #<Product product_code: i23af>
result.buyer    # => #<Buyer email: john@smith.com>
result.foo      # => raises NoMethodError
Calls superclass method
# File lib/commande.rb, line 216
def call(*args, &block)
  @__result = ::Commande::Result.new
  _call(*args) { super(*args, &block) }
end

Private Instance Methods

_call(*args) { || ... } click to toggle source

@api private

# File lib/commande.rb, line 224
def _call(*args)
  catch :end do
    catch :fail do
      validate!(*args)
      yield
    end
  end

  _prepare!
end
validate!(*args) click to toggle source

@since 1.1.0

# File lib/commande.rb, line 236
def validate!(*args)
  fail! unless valid?(*args)
end