Class: Onboardable::List::Builder
- Inherits:
-
Object
- Object
- Onboardable::List::Builder
- Includes:
- Utils::Warnings
- Defined in:
- lib/onboardable/list/builder.rb,
sig/onboardable/list/builder.rbs
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
-
#current_step ⇒ Step
The current step in the building process, defaulting to the first added step.
-
#options ⇒ Hash
The options hash for the builder.
Instance Method Summary collapse
-
#add_step(step) ⇒ Step
Adds a step to the builder.
-
#build(current_step_name = nil) ⇒ Base
Constructs a new List object from the steps added to the builder.
-
#convert_to_step!(name) ⇒ Step
Retrieves a Step object from the builder's steps based on the step name.
-
#convert_to_steps! ⇒ Array<Step>
Converts the internal hash of steps to an array of Step objects.
-
#create_step(name, data = {}) ⇒ Step
(also: #step)
Creates a new Step object and adds it to the builder.
-
#create_step_from(klass) ⇒ Step
(also: #step_from)
Converts a class to a Step object and adds it to the builder.
-
#initialize(options = {}) ⇒ Builder
constructor
Initializes a new instance of ListBuilder.
-
#steps ⇒ Hash
Stores the steps added to the builder.
-
#undefined_method_error!(klass) ⇒ Object
Raises an UndefinedMethodError indicating that the conversion method is not defined for the class.
Methods included from Utils::Warnings
Constructor Details
#initialize(options = {}) ⇒ Builder
Initializes a new instance of ListBuilder.
27 28 29 |
# File 'lib/onboardable/list/builder.rb', line 27 def initialize( = {}) self. = end |
Instance Attribute Details
#current_step ⇒ Step
Returns 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 |
#options ⇒ Hash
Returns The options hash for the builder.
65 66 67 |
# File 'lib/onboardable/list/builder.rb', line 65 def @options end |
Instance Method Details
#add_step(step) ⇒ Step
Adds a step to the builder.
80 81 82 83 84 85 86 87 |
# File 'lib/onboardable/list/builder.rb', line 80 def add_step(step) step.name.then do |name| warn_about_override(name) if steps.key?(name) steps[name] = step end step.tap { self.current_step ||= step } end |
#build(current_step_name = nil) ⇒ Base
Constructs a new List object from the steps added to the builder.
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), .except(STEP_KEY) ) end |
#convert_to_step!(name) ⇒ Step
Retrieves a Step object from the builder's steps based on the step name.
102 103 104 |
# File 'lib/onboardable/list/builder.rb', line 102 def convert_to_step!(name) steps[name] || raise(StepError.new(name, steps.keys)) end |
#convert_to_steps! ⇒ Array<Step>
Converts the internal hash of steps to an array of Step objects.
93 94 95 |
# File 'lib/onboardable/list/builder.rb', line 93 def convert_to_steps! steps.any? ? steps.values : raise(EmptyStepsError) end |
#create_step(name, data = {}) ⇒ Step Also known as: step
Creates a new Step object and adds it to the builder.
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.
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 |
#steps ⇒ Hash
Stores the steps added to the builder.
15 16 17 |
# File 'lib/onboardable/list/builder.rb', line 15 def steps [STEP_KEY] ||= {} end |
#undefined_method_error!(klass) ⇒ Object
Raises an UndefinedMethodError indicating that the conversion method is not defined for the class.
110 111 112 |
# File 'lib/onboardable/list/builder.rb', line 110 def undefined_method_error!(klass) raise UndefinedMethodError.new(klass, Step::CONVERSION_METHOD) end |