Class: Onboardable::List::Base

Inherits:
Object
  • Object
show all
Includes:
Navigation
Defined in:
lib/onboardable/list/base.rb,
sig/onboardable/list/base.rbs

Overview

The List class manages a sequence of steps in an onboarding process, tracking progress and current state.

Constant Summary collapse

PROGRESS_CALCULATION =

Default calculation for onboarding progress as a percentage.

Returns:

  • (Object)
->(step_index, steps_size) { (step_index.to_f / steps_size) * 100 }

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Navigation

#current_step?, #first_step, #first_step?, #first_step_error!, #last_step, #last_step?, #last_step_error!, #next_step, #next_step!, #next_step?, #prev_step, #prev_step!, #prev_step?

Constructor Details

#initialize(steps, current_step, options = {}) ⇒ Base

Initializes a new instance of the List with steps and a current step.

Parameters:

  • steps (Array<Step>)

    An array of steps comprising the onboarding process.

  • current_step (Step)

    The step currently active in the process.

  • options (Hash) (defaults to: {})

    Optional parameters for configuring the list.

Options Hash (options):

  • :progress_calculation (Proc)

    A custom calculation for progress percentage. Receives step_index and steps_size as arguments and returns a Float.



26
27
28
29
30
# File 'lib/onboardable/list/base.rb', line 26

def initialize(steps, current_step, options = {})
  self.steps = steps
  self.current_step = current_step
  self.options = options
end

Instance Attribute Details

#current_stepStep

Returns The current step in the list.

Returns:

  • (Step)

    The current step in the list.



17
18
19
# File 'lib/onboardable/list/base.rb', line 17

def current_step
  @current_step
end

#optionsHash

Returns The options hash for the list.

Returns:

  • (Hash)

    The options hash for the list.



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

def options
  @options
end

#stepsArray<Step>

Returns The steps in the list.

Returns:

  • (Array<Step>)

    The steps in the list.



14
15
16
# File 'lib/onboardable/list/base.rb', line 14

def steps
  @steps
end

Instance Method Details

#progress(progress_calculation = nil) ⇒ Float

Calculates and returns the onboarding progress as a percentage.

Parameters:

  • progress_calculation (Proc, nil) (defaults to: nil)

    An optional custom calculation for progress.

Returns:

  • (Float)

    The percentage of steps completed in the onboarding process.



36
37
38
39
# File 'lib/onboardable/list/base.rb', line 36

def progress(progress_calculation = nil)
  progress_calculation ||= options.fetch(:progress_calculation, PROGRESS_CALCULATION)
  Float(progress_calculation[step_index(current_step), steps.size])
end

#step_error!(step) ⇒ Object

Raises a StepError indicating an invalid step was encountered.

Parameters:

  • step (Step)

    The step that triggered the error.

Returns:

  • (Object)

Raises:

  • (StepError)

    Raises an error if the step does not exist in the list.



97
98
99
# File 'lib/onboardable/list/base.rb', line 97

def step_error!(step)
  raise StepError.new(step.to_str, steps.map(&:to_str))
end

#step_index(step) ⇒ Integer

Determines the index of a given step in the list, ensuring the step exists.

Parameters:

  • step (Step)

    The step for which the index is requested.

Returns:

  • (Integer)

    The index of the step within the list.



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

def step_index(step)
  steps.index { |item| item == step } || step_error!(step)
end

#update_each_step_status(current_step_index) ⇒ Array<Step>

Updates the status of each step in the list based on the current step index.

Parameters:

  • current_step_index (Integer)

    The index of the current step in the list.

Returns:

  • (Array<Step>)

    The updated steps array.



87
88
89
90
91
# File 'lib/onboardable/list/base.rb', line 87

def update_each_step_status(current_step_index)
  steps.each_with_index do |step, index|
    step.update_status!(index <=> current_step_index)
  end
end