Class: Onboardable::List::Builder

Inherits:
Object
  • Object
show all
Includes:
Utils::Warnings
Defined in:
lib/onboardable/list/builder.rb

Overview

The Builder class constructs and manages an onboarding step list, adding steps and building the final list.

Constant Summary collapse

STEP_KEY =

Key used to store steps in the options hash.

:steps

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Builder

Initializes a new instance of ListBuilder.

Parameters:

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

    Options to be set for the builder.

Options Hash (options):

  • :progress_calculation (Proc)

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



27
28
29
# File 'lib/onboardable/list/builder.rb', line 27

def initialize(options = {})
  self.options = options
end

Instance Attribute Details

#current_stepStep

Returns The current step in the building process, defaulting to the first added step.

Returns:

  • (Step)

    The current step in the building process, defaulting to the first added step.



20
21
22
# File 'lib/onboardable/list/builder.rb', line 20

def current_step
  @current_step
end

Instance Method Details

#build(current_step_name = nil) ⇒ Base

Constructs a new List object from the steps added to the builder.

Parameters:

  • current_step_name (String, nil) (defaults to: nil)

    The name of the current step.

Returns:

  • (Base)

    A new List object initialized with the steps and the specified current step.



54
55
56
57
58
59
60
# File 'lib/onboardable/list/builder.rb', line 54

def build(current_step_name = nil)
  Base.new(
    convert_to_steps!,
    convert_to_step!(current_step_name || current_step.name),
    options.except(STEP_KEY)
  )
end

#create_step(name, data = {}) ⇒ Step Also known as: step

Creates a new Step object and adds it to the builder.

Parameters:

  • name (String)

    The name of the step.

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

    The data associated with the step.

Returns:

  • (Step)

    The created step.



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

def create_step(name, data = {})
  Step.new(name, data).tap { |step| add_step(step) }
end

#create_step_from(klass) ⇒ Step Also known as: step_from

Converts a class to a Step object and adds it to the builder.

Parameters:

  • klass (Class)

    The class to be converted to a step.

Returns:

  • (Step)

    The converted step.



45
46
47
# File 'lib/onboardable/list/builder.rb', line 45

def create_step_from(klass)
  (Step.try_convert(klass) || undefined_method_error!(klass)).tap { |step| add_step(step) }
end

#stepsHash

Stores the steps added to the builder.

Returns:

  • (Hash)

    A hash of steps added to the builder.



15
16
17
# File 'lib/onboardable/list/builder.rb', line 15

def steps
  options[STEP_KEY] ||= {}
end