Class: Wizardz::Wizard

Inherits:
Object
  • Object
show all
Defined in:
lib/wizardz/wizard.rb

Constant Summary collapse

STATES =
[{:id => :first_state, :class => Wizardz::Page::First}]

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



23
24
25
26
# File 'lib/wizardz/wizard.rb', line 23

def method_missing(m, *args, &block)
  return @pages[m.to_sym] if self.states.include?(m.to_sym)
  super
end

Instance Attribute Details

#pagesObject (readonly)

Returns the value of attribute pages.



7
8
9
# File 'lib/wizardz/wizard.rb', line 7

def pages
  @pages
end

#stateObject (readonly)

Returns the value of attribute state.



8
9
10
# File 'lib/wizardz/wizard.rb', line 8

def state
  @state
end

#unprocessedObject (readonly)

Returns the value of attribute unprocessed.



10
11
12
# File 'lib/wizardz/wizard.rb', line 10

def unprocessed
  @unprocessed
end

#valid_statesObject (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

#classesObject



40
41
42
# File 'lib/wizardz/wizard.rb', line 40

def classes
  @classes ||= self.class::STATES.map{|r| r[:class]}
end

#createObject



44
45
46
# File 'lib/wizardz/wizard.rb', line 44

def create
  raise "Create method must be implemented in subclass"
end

#first_page?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/wizardz/wizard.rb', line 28

def first_page?
  self.states.index(@state) <= 0
end

#get_pageObject



80
81
82
# File 'lib/wizardz/wizard.rb', line 80

def get_page
  @pages[@state].page_data
end

#get_page_objectObject



84
85
86
# File 'lib/wizardz/wizard.rb', line 84

def get_page_object
  @pages[@state].page_object
end

#last_page?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/wizardz/wizard.rb', line 32

def last_page?
  self.states.index(@state) >= (self.states.size - 1)
end

#partialObject



88
89
90
# File 'lib/wizardz/wizard.rb', line 88

def partial
  @pages[@state].partial
end

#save_dataObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/wizardz/wizard.rb', line 69

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

#statesObject



36
37
38
# File 'lib/wizardz/wizard.rb', line 36

def states
  @states ||= self.class::STATES.map{|r| r[:id]}
end

#update(data, direction = :next) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/wizardz/wizard.rb', line 48

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