Class: Wizardry::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/wizardry/instance.rb

Overview

Instance holds data specific to this page/response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_page:, object:, framework:) ⇒ Instance

Returns a new instance of Instance.

Raises:

  • (ActionController::RoutingError)


6
7
8
9
10
11
12
# 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

Instance Attribute Details

#current_pageObject

Returns the value of attribute current_page.



4
5
6
# File 'lib/wizardry/instance.rb', line 4

def current_page
  @current_page
end

#frameworkObject

Returns the value of attribute framework.



4
5
6
# File 'lib/wizardry/instance.rb', line 4

def framework
  @framework
end

#objectObject

Returns the value of attribute object.



4
5
6
# File 'lib/wizardry/instance.rb', line 4

def object
  @object
end

Instance Method Details

#complete?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/wizardry/instance.rb', line 46

def complete?
  !!object.send(framework.completion_flag)
end

#ensure_not_completeObject

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



42
43
44
# File 'lib/wizardry/instance.rb', line 42

def ensure_not_complete
  raise Wizardry::AlreadyCompletedError if complete?
end

#next_page(page = current_page) ⇒ Object



14
15
16
# 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) ⇒ Object

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



20
21
22
# File 'lib/wizardry/instance.rb', line 20

def route(from = framework.pages.first)
  @route ||= route!(from)
end

#route!(from = framework.pages.first) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# 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?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/wizardry/instance.rb', line 36

def valid_so_far?
  route.all? { |complete_page| object.valid?(complete_page.name) }
end