Module: Commande::Interface

Defined in:
lib/commande.rb

Overview

Commande interface

Since:

  • 1.1.0

Instance Method Summary collapse

Instance Method Details

#call(*args, &block) ⇒ Commande::Result

Triggers the operation and return a result.

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

Examples:

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: '[email protected]', product_code: 'i23af')
result.failure? # => false
result.successful? # => true

result.product  # => #<Product product_code: i23af>
result.buyer    # => #<Buyer email: [email protected]>
result.foo      # => raises NoMethodError

Raises:

  • (NoMethodError)

    if this isn’t implemented by the including class.

Since:

  • 1.1.0



216
217
218
219
# File 'lib/commande.rb', line 216

def call(*args, &block)
  @__result = ::Commande::Result.new
  _call(*args) { super(*args, &block) }
end