Class: BetterService::Workflowable::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/better_service/concerns/workflowable/context.rb

Overview

Context - Container for shared data between workflow steps

The Context object holds all data that flows through the workflow pipeline. Each step can read from and write to the context. The context also tracks the success/failure state of the workflow.

Example:

context = Context.new(user: current_user, cart_items: [...])
context.order = Order.create!(...)
context.success? # => true

context.fail!("Payment failed", payment_error: "Card declined")
context.success? # => false
context.errors # => { payment_error: "Card declined" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, **initial_data) ⇒ Context

Returns a new instance of Context.



22
23
24
25
26
27
28
# File 'lib/better_service/concerns/workflowable/context.rb', line 22

def initialize(user, **initial_data)
  @user = user
  @data = initial_data
  @errors = {}
  @failed = false
  @_called = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

Allow reading context data via method calls Example: context.order instead of context.get(:order)



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/better_service/concerns/workflowable/context.rb', line 77

def method_missing(method_name, *args)
  method_str = method_name.to_s

  if method_str.end_with?("=")
    # Setter: context.order = value
    key = method_str.chomp("=").to_sym
    @data[key] = args.first
  elsif @data.key?(method_name)
    # Getter: context.order
    @data[method_name]
  else
    super
  end
end

Instance Attribute Details

#_calledObject (readonly)

Returns the value of attribute _called.



20
21
22
# File 'lib/better_service/concerns/workflowable/context.rb', line 20

def _called
  @_called
end

#errorsObject (readonly)

Returns the value of attribute errors.



20
21
22
# File 'lib/better_service/concerns/workflowable/context.rb', line 20

def errors
  @errors
end

#userObject (readonly)

Returns the value of attribute user.



20
21
22
# File 'lib/better_service/concerns/workflowable/context.rb', line 20

def user
  @user
end

Instance Method Details

#add(key, value) ⇒ Object

Add data to context

Parameters:

  • key (Symbol)

    Key to store data under

  • value (Object)

    Value to store



64
65
66
# File 'lib/better_service/concerns/workflowable/context.rb', line 64

def add(key, value)
  @data[key] = value
end

#called!Object

Mark workflow as called (used internally)



51
52
53
# File 'lib/better_service/concerns/workflowable/context.rb', line 51

def called!
  @_called = true
end

#called?Boolean

Check if workflow has been called

Returns:

  • (Boolean)


56
57
58
# File 'lib/better_service/concerns/workflowable/context.rb', line 56

def called?
  @_called
end

#fail!(message, **errors) ⇒ Object

Mark workflow as failed with error message and optional error details

Parameters:

  • message (String)

    Error message

  • errors (Hash)

    Optional hash of detailed errors



44
45
46
47
48
# File 'lib/better_service/concerns/workflowable/context.rb', line 44

def fail!(message, **errors)
  @failed = true
  @errors[:message] = message
  @errors.merge!(errors) if errors.any?
end

#failure?Boolean

Check if workflow has failed

Returns:

  • (Boolean)


36
37
38
# File 'lib/better_service/concerns/workflowable/context.rb', line 36

def failure?
  @failed
end

#get(key) ⇒ Object

Get data from context

Parameters:

  • key (Symbol)

    Key to retrieve



71
72
73
# File 'lib/better_service/concerns/workflowable/context.rb', line 71

def get(key)
  @data[key]
end

#inspectObject

Inspect for debugging



103
104
105
# File 'lib/better_service/concerns/workflowable/context.rb', line 103

def inspect
  "#<BetterService::Workflowable::Context success=#{success?} data=#{@data.inspect} errors=#{@errors.inspect}>"
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
# File 'lib/better_service/concerns/workflowable/context.rb', line 92

def respond_to_missing?(method_name, include_private = false)
  method_str = method_name.to_s
  method_str.end_with?("=") || @data.key?(method_name) || super
end

#success?Boolean

Check if workflow has succeeded (no failure called)

Returns:

  • (Boolean)


31
32
33
# File 'lib/better_service/concerns/workflowable/context.rb', line 31

def success?
  !@failed
end

#to_hObject

Return all context data as hash



98
99
100
# File 'lib/better_service/concerns/workflowable/context.rb', line 98

def to_h
  @data.dup
end