class Wizardry::Instance

Instance holds data specific to this page/response

Attributes

current_page[RW]
framework[RW]
object[RW]

Public Class Methods

new(current_page:, object:, framework:) click to toggle source
# File lib/wizardry/instance.rb, line 6
def initialize(current_page:, object:, framework:)
  @object       = object
  @framework    = framework
  @current_page = @framework.pages.detect { |p| p.name == current_page.to_sym }

  raise(ActionController::RoutingError, %(Wizard page #{current_page} not found)) unless @current_page
end

Public Instance Methods

complete?() click to toggle source
# File lib/wizardry/instance.rb, line 46
def complete?
  !!object.send(framework.completion_flag)
end
ensure_not_complete() click to toggle source

check this wizard hasn’t already been completed using the object’s :completion_flag

# File lib/wizardry/instance.rb, line 42
def ensure_not_complete
  raise Wizardry::AlreadyCompletedError if complete?
end
next_page(page = current_page) click to toggle source
# File lib/wizardry/instance.rb, line 14
def next_page(page = current_page)
  next_branch_page(page) || next_trunk_page(page)
end
route(from = framework.pages.first) click to toggle source

find all the pages we’ve visited on our way to the current page

# File lib/wizardry/instance.rb, line 20
def route(from = framework.pages.first)
  @route ||= route!(from)
end
route!(from = framework.pages.first) click to toggle source
# File lib/wizardry/instance.rb, line 24
def route!(from = framework.pages.first)
  page = from

  @route = [].tap do |completed|
    until page == current_page
      completed << page

      page = next_page(page)
    end
  end
end
valid_so_far?() click to toggle source
# File lib/wizardry/instance.rb, line 36
def valid_so_far?
  route.all? { |complete_page| object.valid?(complete_page.name) }
end

Private Instance Methods

next_branch_page(page) click to toggle source
# File lib/wizardry/instance.rb, line 52
def next_branch_page(page)
  next_page = page.next_pages.detect do |p|
    p.condition.blank? || p.condition.call(object)
  end

  return unless next_page

  framework.page(next_page.name)
end
next_trunk_page(page) click to toggle source

if the branch ends continue along the trunk from where we left off

# File lib/wizardry/instance.rb, line 64
def next_trunk_page(page)
  page_index = framework.pages.index(page)

  framework.pages.detect.with_index do |p, i|
    next if p.branch?

    i > page_index
  end
end