Class: Wizardry::Instance
- Inherits:
-
Object
- Object
- Wizardry::Instance
- Defined in:
- lib/wizardry/instance.rb
Overview
Instance holds data specific to this page/response
Instance Attribute Summary collapse
-
#current_page ⇒ Object
Returns the value of attribute current_page.
-
#framework ⇒ Object
Returns the value of attribute framework.
-
#object ⇒ Object
Returns the value of attribute object.
Instance Method Summary collapse
- #complete? ⇒ Boolean
-
#ensure_not_complete ⇒ Object
check this wizard hasn't already been completed using the object's :completion_flag.
-
#initialize(current_page:, object:, framework:) ⇒ Instance
constructor
A new instance of Instance.
- #next_page(page = current_page) ⇒ Object
-
#route(from = framework.pages.first) ⇒ Object
find all the pages we've visited on our way to the current page.
- #route!(from = framework.pages.first) ⇒ Object
- #valid_so_far? ⇒ Boolean
Constructor Details
#initialize(current_page:, object:, framework:) ⇒ Instance
Returns a new instance of Instance.
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_page ⇒ Object
Returns the value of attribute current_page.
4 5 6 |
# File 'lib/wizardry/instance.rb', line 4 def current_page @current_page end |
#framework ⇒ Object
Returns the value of attribute framework.
4 5 6 |
# File 'lib/wizardry/instance.rb', line 4 def framework @framework end |
#object ⇒ Object
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
46 47 48 |
# File 'lib/wizardry/instance.rb', line 46 def complete? !!object.send(framework.completion_flag) end |
#ensure_not_complete ⇒ Object
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
36 37 38 |
# File 'lib/wizardry/instance.rb', line 36 def valid_so_far? route.all? { |complete_page| object.valid?(complete_page.name) } end |