Class: Wizardz::Wizard
- Inherits:
-
Object
- Object
- Wizardz::Wizard
- Defined in:
- lib/wizardz/wizard.rb
Constant Summary collapse
Instance Attribute Summary collapse
-
#pages ⇒ Object
readonly
Returns the value of attribute pages.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#unprocessed ⇒ Object
readonly
Returns the value of attribute unprocessed.
-
#valid_states ⇒ Object
readonly
Returns the value of attribute valid_states.
Instance Method Summary collapse
- #classes ⇒ Object
- #create ⇒ Object
- #first_page? ⇒ Boolean
- #get_page ⇒ Object
- #get_page_object ⇒ Object
-
#initialize(fund_data = {}, state = nil) ⇒ Wizard
constructor
A new instance of Wizard.
- #last_page? ⇒ Boolean
- #partial ⇒ Object
- #save_data ⇒ Object
- #states ⇒ Object
- #update(data, direction = :next) ⇒ Object
Constructor Details
#initialize(fund_data = {}, state = nil) ⇒ Wizard
Returns a new instance of Wizard.
14 15 16 17 18 19 20 21 |
# File 'lib/wizardz/wizard.rb', line 14 def initialize(fund_data={},state=nil) state = self.states.first if state.nil? raise "Invalid State ':merge' is reservered state" if self.states.include?(:merge) state = state.to_sym raise "Invalid State Assignment: #{state}" unless self.states.include?(state) self.load_data(fund_data) @state = state end |
Instance Attribute Details
#pages ⇒ Object (readonly)
Returns the value of attribute pages.
7 8 9 |
# File 'lib/wizardz/wizard.rb', line 7 def pages @pages end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
8 9 10 |
# File 'lib/wizardz/wizard.rb', line 8 def state @state end |
#unprocessed ⇒ Object (readonly)
Returns the value of attribute unprocessed.
10 11 12 |
# File 'lib/wizardz/wizard.rb', line 10 def unprocessed @unprocessed end |
#valid_states ⇒ Object (readonly)
Returns the value of attribute valid_states.
9 10 11 |
# File 'lib/wizardz/wizard.rb', line 9 def valid_states @valid_states end |
Instance Method Details
#classes ⇒ Object
35 36 37 |
# File 'lib/wizardz/wizard.rb', line 35 def classes @classes ||= self.class::STATES.map{|r| r[:class]} end |
#create ⇒ Object
39 40 41 |
# File 'lib/wizardz/wizard.rb', line 39 def create raise "Create method must be implemented in subclass" end |
#first_page? ⇒ Boolean
23 24 25 |
# File 'lib/wizardz/wizard.rb', line 23 def first_page? self.states.index(@state) <= 0 end |
#get_page ⇒ Object
75 76 77 |
# File 'lib/wizardz/wizard.rb', line 75 def get_page @pages[@state].page_data end |
#get_page_object ⇒ Object
79 80 81 |
# File 'lib/wizardz/wizard.rb', line 79 def get_page_object @pages[@state].page_object end |
#last_page? ⇒ Boolean
27 28 29 |
# File 'lib/wizardz/wizard.rb', line 27 def last_page? self.states.index(@state) >= (self.states.size - 1) end |
#partial ⇒ Object
83 84 85 |
# File 'lib/wizardz/wizard.rb', line 83 def partial @pages[@state].partial end |
#save_data ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/wizardz/wizard.rb', line 64 def save_data results = {} @valid_states.each do |state| results[state] = @pages[state].page_data if @pages[state].respond_to?(:page_data) end results[:valid_states] = @valid_states results[:unprocessed] = @unprocessed.clone results[:unprocessed].delete(@state) results end |
#states ⇒ Object
31 32 33 |
# File 'lib/wizardz/wizard.rb', line 31 def states @states ||= self.class::STATES.map{|r| r[:id]} end |
#update(data, direction = :next) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/wizardz/wizard.rb', line 43 def update(data, direction=:next) return true if data.nil? if @pages[@state].update(data) @valid_states = @pages[@state].get_valid_states(@valid_states) @state = @pages[@state].transit(direction, @valid_states) else @state = @pages[@state].transit(direction, @valid_states) if direction.to_sym == :prev return false end return true end |