Class: IdempotencyLock::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/idempotency_lock/result.rb

Overview

Represents the result of an idempotency operation.

Examples:

Checking if the block was executed

result = IdempotencyLock.once("my-operation") { expensive_work }
if result.executed?
  puts "Operation ran, got: #{result.value}"
else
  puts "Operation was skipped (already ran)"
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(executed:, value: nil, skipped: false, error: nil) ⇒ Result

Returns a new instance of Result.



17
18
19
20
21
22
# File 'lib/idempotency_lock/result.rb', line 17

def initialize(executed:, value: nil, skipped: false, error: nil)
  @executed = executed
  @value = value
  @skipped = skipped
  @error = error
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



15
16
17
# File 'lib/idempotency_lock/result.rb', line 15

def error
  @error
end

#valueObject (readonly)

Returns the value of attribute value.



15
16
17
# File 'lib/idempotency_lock/result.rb', line 15

def value
  @value
end

Instance Method Details

#error?Boolean

Returns true if an error occurred during execution.

Returns:

  • (Boolean)

    true if an error occurred during execution



35
36
37
# File 'lib/idempotency_lock/result.rb', line 35

def error?
  !@error.nil?
end

#executed?Boolean

Returns true if the block was executed.

Returns:

  • (Boolean)

    true if the block was executed



25
26
27
# File 'lib/idempotency_lock/result.rb', line 25

def executed?
  @executed
end

#inspectString

Returns human-readable representation for debugging.

Returns:

  • (String)

    human-readable representation for debugging



45
46
47
48
49
50
51
# File 'lib/idempotency_lock/result.rb', line 45

def inspect
  parts = ["executed=#{@executed}"]
  parts << "skipped=#{@skipped}" if @skipped
  parts << "value=#{@value.inspect}" if @value
  parts << "error=#{@error.class}" if @error
  "#<IdempotencyLock::Result #{parts.join(" ")}>"
end

#skipped?Boolean

Returns true if the operation was skipped due to existing lock.

Returns:

  • (Boolean)

    true if the operation was skipped due to existing lock



30
31
32
# File 'lib/idempotency_lock/result.rb', line 30

def skipped?
  @skipped
end

#success?Boolean

Returns true if executed successfully without error.

Returns:

  • (Boolean)

    true if executed successfully without error



40
41
42
# File 'lib/idempotency_lock/result.rb', line 40

def success?
  @executed && !error?
end