Class: Interactor::Contracts::BreachSet

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/interactor/contracts/breach_set.rb

Overview

A simple wrapper around set of breaches of contract constraints

Instance Method Summary collapse

Instance Method Details

#to_hashHash Also known as: to_h

Converts the breach set into a Hash for use with context failing

Examples:

class AuthenticateUser
  include Interactor
  include Interactor::Contracts

  expects do
    required(:email).filled
    required(:password).filled
  end

  promises do
    required(:user).filled
    required(:token).filled
  end

  on_breach do |breaches|
    context.fail!(breaches)
  end
end

result = AuthenticateUser.call({})
#=> #<Interactor::Context email=["email is missing"],
                          password=["password is missing"]>

result.failure?  #=> true

Returns:

  • (Hash)

    a hash with property keys and message values



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/interactor/contracts/breach_set.rb', line 39

def to_hash
  each_with_object({}) do |(property, messages), result|
    result[property] =
      if messages.is_a?(Hash)
        Hash(result[property]).merge(messages)
      else
        Array(result[property]) | messages
      end
    result
  end
end