Class: SimpleFlow::Result

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

Overview

This class represents the result of an operation within a simple flow system.

It encapsulates the operation’s outcome (value), contextual data (context), and any errors occurred during the operation (errors). Its primary purpose is to facilitate flow control and error handling in a clean and predictable manner. The class provides mechanisms to update context and errors, halt the flow, and conditionally continue based on the operation state. This promotes creating a chainable, fluent interface for managing operation results in complex processes or workflows.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, context: {}, errors: {}, activated_steps: []) ⇒ Result

Initializes a new Result instance.

Parameters:

  • value (Object)

    the outcome of the operation.

  • context (Hash, optional) (defaults to: {})

    contextual data related to the operation.

  • errors (Hash, optional) (defaults to: {})

    errors occurred during the operation.

  • activated_steps (Array<Symbol>, optional) (defaults to: [])

    steps activated for dynamic execution.



31
32
33
34
35
36
37
# File 'lib/simple_flow/result.rb', line 31

def initialize(value, context: {}, errors: {}, activated_steps: [])
  @value = value
  @context = context
  @errors = errors
  @activated_steps = activated_steps
  @continue = true
end

Instance Attribute Details

#activated_stepsObject (readonly)

Steps that have been activated for dynamic execution.



24
25
26
# File 'lib/simple_flow/result.rb', line 24

def activated_steps
  @activated_steps
end

#contextObject (readonly)

Contextual data related to the operation.



18
19
20
# File 'lib/simple_flow/result.rb', line 18

def context
  @context
end

#errorsObject (readonly)

Errors occurred during the operation.



21
22
23
# File 'lib/simple_flow/result.rb', line 21

def errors
  @errors
end

#valueObject (readonly)

The outcome of the operation.



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

def value
  @value
end

Instance Method Details

#activate(*step_names) ⇒ Result

Activates optional steps for dynamic execution.

Parameters:

  • step_names (Symbol, Array<Symbol>)

    one or more step names to activate.

Returns:

  • (Result)

    a new Result instance with the steps added to activated_steps.



85
86
87
88
89
90
# File 'lib/simple_flow/result.rb', line 85

def activate(*step_names)
  new_activated = @activated_steps + step_names.flatten
  result = self.class.new(@value, context: @context, errors: @errors, activated_steps: new_activated)
  result.instance_variable_set(:@continue, @continue)
  result
end

#continue(new_value) ⇒ Result

Continues the flow, updating the result’s value.

Parameters:

  • new_value (Object)

    the new value to set.

Returns:

  • (Result)

    a new Result instance with the new value.



72
73
74
# File 'lib/simple_flow/result.rb', line 72

def continue(new_value)
  with_value(new_value)
end

#continue?Boolean

Checks if the operation should continue.

Returns:

  • (Boolean)

    true if the operation should continue, else false.



78
79
80
# File 'lib/simple_flow/result.rb', line 78

def continue?
  @continue
end

#halt(new_value = nil) ⇒ Result

Halts the flow, optionally updating the result’s value.

Parameters:

  • new_value (Object, nil) (defaults to: nil)

    the new value to set, if any.

Returns:

  • (Result)

    a new Result instance with continue set to false.



63
64
65
66
67
# File 'lib/simple_flow/result.rb', line 63

def halt(new_value = nil)
  result = new_value ? with_value(new_value) : self.class.new(@value, context: @context, errors: @errors, activated_steps: @activated_steps)
  result.instance_variable_set(:@continue, false)
  result
end

#with_context(key, value) ⇒ Result

Adds or updates context to the result.

Parameters:

  • key (Symbol)

    the key to store the context under.

  • value (Object)

    the value to store.

Returns:

  • (Result)

    a new Result instance with updated context.



43
44
45
46
47
# File 'lib/simple_flow/result.rb', line 43

def with_context(key, value)
  result = self.class.new(@value, context: @context.merge(key => value), errors: @errors, activated_steps: @activated_steps)
  result.instance_variable_set(:@continue, @continue)
  result
end

#with_error(key, message) ⇒ Result

Adds an error message under a specific key. If the key already exists, it appends the message to the existing errors.

Parameters:

  • key (Symbol)

    the key under which the error should be stored.

  • message (String)

    the error message.

Returns:

  • (Result)

    a new Result instance with updated errors.



54
55
56
57
58
# File 'lib/simple_flow/result.rb', line 54

def with_error(key, message)
  result = self.class.new(@value, context: @context, errors: @errors.merge(key => [*@errors[key], message]), activated_steps: @activated_steps)
  result.instance_variable_set(:@continue, @continue)
  result
end