Class: Teckel::Operation::Result

Inherits:
Object
  • Object
show all
Includes:
Result
Defined in:
lib/teckel/operation/result.rb

Overview

The optional, default result object for Teckel::Operations. Wraps output and error into a Result.

Examples:

class CreateUser
  include Teckel::Operation

  result!    # Shortcut to use this Result object

  input  Types::Hash.schema(name: Types::String, age: Types::Coercible::Integer)
  output Types.Instance(User)
  error  Types::Hash.schema(message: Types::String, errors: Types::Array.of(Types::Hash))

  def call(input)
    user = User.new(name: input[:name], age: input[:age])
    if user.save
      success!(user) # exits early with success, prevents any further execution
    else
      fail!(message: "Could not save User", errors: user.errors)
    end
  end
end

# A success call:
CreateUser.call(name: "Bob", age: 23).is_a?(Teckel::Operation::Result) #=> true
CreateUser.call(name: "Bob", age: 23).success.is_a?(User) #=> true

# A failure call:
CreateUser.call(name: "Bob", age: 10).is_a?(Teckel::Operation::Result) #=> true
CreateUser.call(name: "Bob", age: 10).failure.is_a?(Hash) #=> true

Direct Known Subclasses

Chain::Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, successful) ⇒ Result

Returns a new instance of Result.

Parameters:

  • value (Object)

    The result value

  • successful (Boolean)

    whether this is a successful result



42
43
44
45
# File 'lib/teckel/operation/result.rb', line 42

def initialize(value, successful)
  @value = value
  @successful = successful
end

Instance Attribute Details

#valueMixed (readonly)

Returns the value/payload.

Returns:

  • (Mixed)

    the value/payload



55
56
57
# File 'lib/teckel/operation/result.rb', line 55

def value
  @value
end

Instance Method Details

#failure(default = nil) {|Mixed| ... } ⇒ Mixed

Get the error/failure value

Parameters:

  • default (Mixed) (defaults to: nil)

    return this default value if it’s not a failure result

Yields:

  • (Mixed)

    If a block is given and this is not a failure result, the value is yielded to the block

Returns:

  • (Mixed)

    the value/payload



61
62
63
64
65
66
# File 'lib/teckel/operation/result.rb', line 61

def failure(default = nil, &block)
  return value unless @successful
  return yield(value) if block

  default
end

#success(default = nil) {|Mixed| ... } ⇒ Mixed

Get the success value

Parameters:

  • default (Mixed) (defaults to: nil)

    return this default value if it’s not a success result

Yields:

  • (Mixed)

    If a block is given and this is not a success result, the value is yielded to the block

Returns:

  • (Mixed)

    the value/payload



72
73
74
75
76
77
# File 'lib/teckel/operation/result.rb', line 72

def success(default = nil, &block)
  return value if @successful
  return yield(value) if block

  default
end

#successful?Boolean

Whether this is a success result

Returns:

  • (Boolean)


49
50
51
# File 'lib/teckel/operation/result.rb', line 49

def successful?
  @successful
end