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

  assures 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



37
38
39
40
41
42
# File 'lib/interactor/contracts/breach_set.rb', line 37

def to_hash
  reduce({}) do |result, (property, messages)|
    result[property] = Array(result[property]) | messages
    result
  end
end