Module: Interactor::Contracts::DSL

Defined in:
lib/interactor/contracts/dsl.rb

Overview

Defines the class-level DSL that enables Interactor contracts.

Instance Method Summary collapse

Instance Method Details

#config(&block) ⇒ void

This method returns an undefined value.

Sends configuration set up to the underlying contracts in the terms

Examples:

class CreatePerson
  include Interactor
  include Interactor::Contracts

  config do
    messages.backend = :i18n
    messages.top_namespace = :my_app
    messages.load_paths << File.join(__dir__, '..', 'errors.yml')
  end
end

Parameters:

  • block (Block)

    the block to execute for the underlying contracts



51
52
53
# File 'lib/interactor/contracts/dsl.rb', line 51

def config(&block)
  contract.config(&block)
end

#contractContract

The Contract to enforce on calls to the Interactor

Examples:

class CreatePerson
  include Interactor
  include Interactor::Contracts

  promises do
    required(:person).filled
  end

  contracts  #=> <#Interactor::Contracts::Contract>
end

Returns:



71
72
73
# File 'lib/interactor/contracts/dsl.rb', line 71

def contract
  @contract ||= Contract.new
end

#expects(&block) ⇒ void

This method returns an undefined value.

Defines the expectations of an Interactor and creates a before hook

Examples:

class CreatePerson
  include Interactor
  include Interactor::Contracts

  expects do
    required(:name).filled
  end

  def call
    context.person = Person.create!(:name => context.name)
  end
end

CreatePerson.call(:first_name => "Billy").success?  #=> false
CreatePerson.call(:name => "Billy").success?        #=> true

Parameters:

  • block (Block)

    the block defining the expectations



97
98
99
100
# File 'lib/interactor/contracts/dsl.rb', line 97

def expects(&block)
  contract.add_expectation(&block)
  define_expectations_hook
end

#inherit_contract(contract) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Allows for the inheritance of contracts in subclasses

Parameters:



107
108
109
110
111
112
113
114
115
# File 'lib/interactor/contracts/dsl.rb', line 107

def inherit_contract(contract)
  @contract = Contract.new(
    promises: contract.promises.clone,
    expectations: contract.expectations.clone,
    consequences: contract.consequences.clone
  )
  define_promises_hook
  define_expectations_hook
end

#on_breach(&block) ⇒ void

This method returns an undefined value.

Defines a consequence that is called when a contract is breached

Examples:

class CreatePerson
  include Interactor
  include Interactor::Contracts

  expects do
    required(:name).filled
  end

  on_breach do |breaches|
    context.fail!(:message => "invalid_#{breaches.first.property}")
  end

  def call
    context.person = Person.create!(:name => context.name)
  end
end

CreatePerson.call(:first_name => "Billy").message  #=> "invalid_name"

Parameters:

  • block (Block)

    the consequence as a block of arity 1.



142
143
144
# File 'lib/interactor/contracts/dsl.rb', line 142

def on_breach(&block)
  contract.add_consequence(block)
end

#promises(&block) ⇒ void Also known as: assures

This method returns an undefined value.

Defines the promises of an Interactor and creates an after hook

Examples:

class CreatePerson
  include Interactor
  include Interactor::Contracts

  promises do
    required(:person).filled
  end

  def call
    context.person = Person.new
  end
end

Parameters:

  • block (Block)

    the block defining the promises



28
29
30
31
# File 'lib/interactor/contracts/dsl.rb', line 28

def promises(&block)
  contract.add_promise(&block)
  define_promises_hook
end