Class: Onboardable::Step

Inherits:
Object
  • Object
show all
Defined in:
lib/onboardable/step.rb,
sig/onboardable/step.rbs

Overview

Represents a single step within an onboarding process, including its status and associated data.

Constant Summary collapse

CONVERSION_METHOD =

Method name used for converting a class to a Step object.

Returns:

  • (Symbol)
:to_onboarding_step
PENDING_STATUS =

The step is pending and has not yet been started.

Returns:

  • (Symbol)
:pending
CURRENT_STATUS =

The step is currently in progress.

Returns:

  • (Symbol)
:current
COMPLETED_STATUS =

The step has been completed.

Returns:

  • (Symbol)
:completed
DEFAULT_STATUS =

The default status for a new step.

Returns:

  • (Symbol)
PENDING_STATUS
STATUSES =

All valid statuses for a step.

Returns:

  • (::Array[Symbol])
[PENDING_STATUS, CURRENT_STATUS, COMPLETED_STATUS].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, data = {}) ⇒ Step

Initializes a new Step with a name, optional custom data, and a default status.

Parameters:

  • name (String) —

    The name of the step.

  • data (Hash) (defaults to: {}) —

    The custom data associated with the step.



63
64
65
66
67
# File 'lib/onboardable/step.rb', line 63

def initialize(name, data = {})
  self.name = name
  self.data = data
  self.status = DEFAULT_STATUS
end

Instance Attribute Details

#data ⇒ Hash

Returns Custom data associated with the step.

Returns:

  • (Hash) —

    Custom data associated with the step.



53
54
55
# File 'lib/onboardable/step.rb', line 53

def data
  @data
end

#name ⇒ String

Returns The name of the step.

Returns:

  • (String) —

    The name of the step.



50
51
52
# File 'lib/onboardable/step.rb', line 50

def name
  @name
end

#status ⇒ Symbol

Returns The current status of the step.

Returns:

  • (Symbol) —

    The current status of the step.



56
57
58
# File 'lib/onboardable/step.rb', line 56

def status
  @status
end

Class Method Details

.try_convert(klass) ⇒ Onboardable::Step?

Attempts to convert a class to a Step object using a specified conversion method.

Parameters:

  • klass (Class) —

    The class to convert to a Step.

Returns:

  • (Onboardable::Step, nil) —

    The converted Step object, or nil if the class does not respond to the conversion method.



28
29
30
31
32
33
34
# File 'lib/onboardable/step.rb', line 28

def try_convert(klass)
  return unless klass.respond_to?(CONVERSION_METHOD)

  klass.public_send(CONVERSION_METHOD).then do |step|
    step.is_a?(Step) ? step : conversion_error!(klass, step)
  end
end

Instance Method Details

#==(other) ⇒ Boolean

Compares this step to another to determine if they are equivalent, based on the step name.

Parameters:

Returns:

  • (Boolean) —

    True if both steps have the same name, false otherwise.



94
95
96
# File 'lib/onboardable/step.rb', line 94

def ==(other)
  to_str == other.to_str
end

#completed? ⇒ Boolean

Checks if the step is in the completed status.

Returns:

  • (Boolean) —

    True if the step is currently in the completed status, false otherwise.



86
87
88
# File 'lib/onboardable/step.rb', line 86

def completed?
  status == COMPLETED_STATUS
end

#current? ⇒ Boolean

Checks if the step is in the current status.

Returns:

  • (Boolean) —

    True if the step is currently in the current status, false otherwise.



79
80
81
# File 'lib/onboardable/step.rb', line 79

def current?
  status == CURRENT_STATUS
end

#pending? ⇒ Boolean

Checks if the step is in the pending status.

Returns:

  • (Boolean) —

    True if the step is currently in the pending status, false otherwise.



72
73
74
# File 'lib/onboardable/step.rb', line 72

def pending?
  status == PENDING_STATUS
end

#to_str ⇒ String

Provides a string representation of the step, using its name.

Returns:

  • (String) —

    The name of the step.



101
102
103
# File 'lib/onboardable/step.rb', line 101

def to_str
  name
end

#update_status!(comparison_result) ⇒ Symbol

Updates the status of the step based on a specified comparison result.

Parameters:

  • comparison_result (Integer) —

    The result of a comparison operation.

Returns:

  • (Symbol) —

    The new status of the step.



109
110
111
112
113
114
115
116
# File 'lib/onboardable/step.rb', line 109

def update_status!(comparison_result)
  self.status = case comparison_result
                when -1 then COMPLETED_STATUS
                when 0 then CURRENT_STATUS
                when 1 then PENDING_STATUS
                else comparison_result_error!(comparison_result)
                end
end