Class: Onboardable::Step

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

Overview

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

Constant Summary collapse

CONVERSION_METHOD =
:to_onboarding_step
PENDING_STATUS =
:pending
CURRENT_STATUS =
:current
COMPLETED_STATUS =
:completed
DEFAULT_STATUS =
PENDING_STATUS
STATUSES =
[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.



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

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

Instance Attribute Details

#dataHash

Returns Custom data associated with the step.

Returns:

  • (Hash)

    Custom data associated with the step.



44
45
46
# File 'lib/onboardable/step.rb', line 44

def data
  @data
end

#nameString

Returns The name of the step.

Returns:

  • (String)

    The name of the step.



41
42
43
# File 'lib/onboardable/step.rb', line 41

def name
  @name
end

#statusSymbol

Returns The current status of the step.

Returns:

  • (Symbol)

    The current status of the step.



47
48
49
# File 'lib/onboardable/step.rb', line 47

def status
  @status
end

Class Method Details

.try_convert(klass) ⇒ 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:

  • (Step, nil)

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



21
22
23
24
25
26
27
# File 'lib/onboardable/step.rb', line 21

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:

  • other (Step)

    The step to compare with.

Returns:

  • (Boolean)

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



84
85
86
# File 'lib/onboardable/step.rb', line 84

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.



76
77
78
# File 'lib/onboardable/step.rb', line 76

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.



69
70
71
# File 'lib/onboardable/step.rb', line 69

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.



62
63
64
# File 'lib/onboardable/step.rb', line 62

def pending?
  status == PENDING_STATUS
end

#to_strString

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

Returns:

  • (String)

    The name of the step.



91
92
93
# File 'lib/onboardable/step.rb', line 91

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.



99
100
101
102
103
104
105
# File 'lib/onboardable/step.rb', line 99

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