Class: Doop::DoopController

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(application_controller, &block) ⇒ DoopController

Returns a new instance of DoopController.



12
13
14
15
16
17
# File 'lib/doop_controller.rb', line 12

def initialize application_controller, &block
  @controller = application_controller
  @block = block
  @current_page_block = method( :default_current_page )
  @all_pages_block = method( :default_all_pages )
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



122
123
124
# File 'lib/doop_controller.rb', line 122

def method_missing(method, *args, &block)
  @self_before_instance_eval.send method, *args, &block
end

Instance Attribute Details

#doopObject (readonly)

Returns the value of attribute doop.



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

def doop
  @doop
end

Instance Method Details

#all_pages(&block) ⇒ Object



130
131
132
# File 'lib/doop_controller.rb', line 130

def all_pages &block
  @all_pages_block = block
end

#answerObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/doop_controller.rb', line 23

def answer
  back_val = @controller.params["back_a_page"]
  nav_path = @controller.params["nav_path"]
  change_answer_path = @controller.params["change_answer_path"]
  return back if back_val != nil 
  return change_page nav_path if nav_path != nil
  return change change_answer_path if change_answer_path != nil

  render_page in_doop { |doop| doop.answer( @controller.params ) }
end

#backObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/doop_controller.rb', line 34

def back
  in_doop do |doop|
    page = get_page_path
    pages = get_all_pages
    i = pages.index(page)
    if i!=0
      path = pages[i-1]
      doop.change( path )
      pages[pages.index(path), pages.length].each do |n|
        doop[n]["_answered"] = false
      end

      doop.ask_next
    end
  end
  render_page
end

#change(path) ⇒ Object



52
53
54
55
56
57
# File 'lib/doop_controller.rb', line 52

def change path
  res = in_doop do
    |doop| doop.change( path ) 
  end
  render_page res
end

#change_page(path) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/doop_controller.rb', line 59

def change_page path
  in_doop do |doop|
    #path = @controller.params["path"]
    doop.change( path )

    # unanswer all pages after path
    pages = @all_pages_block.call 
    pages[pages.index(path), pages.length].each do |n|
      doop[n]["_answered"] = false
    end

    doop.ask_next
  end
  render_page
end

#current_page(&block) ⇒ Object



126
127
128
# File 'lib/doop_controller.rb', line 126

def current_page &block
  @current_page_block = block
end

#default_all_pagesObject



161
162
163
164
165
166
167
# File 'lib/doop_controller.rb', line 161

def default_all_pages 
  l = []
  doop.each_question_with_regex_filter "^\/page/\\w+$" do |question,path|
    l << path if doop[path]["_enabled"]
  end
  l
end

#default_current_page(path) ⇒ Object



156
157
158
159
# File 'lib/doop_controller.rb', line 156

def default_current_page path 
  a = path.split("/").reject {|s| s.empty? }[1] 
  "/page/#{a}"
end

#evaluate(block) ⇒ Object



117
118
119
120
# File 'lib/doop_controller.rb', line 117

def evaluate(block)
  @self_before_instance_eval = eval "self", block.binding
  instance_eval &block
end

#get_all_pagesObject



144
145
146
# File 'lib/doop_controller.rb', line 144

def get_all_pages
  @all_pages_block.call
end

#get_pageObject



134
135
136
137
# File 'lib/doop_controller.rb', line 134

def get_page
  path = @doop.currently_asked
  @doop[@current_page_block.call( path )]["_page"]
end

#get_page_pathObject



139
140
141
142
# File 'lib/doop_controller.rb', line 139

def get_page_path
  path = @doop.currently_asked
  @current_page_block.call( path )
end

#in_doopObject



109
110
111
112
113
114
115
# File 'lib/doop_controller.rb', line 109

def in_doop
  load
  res = yield(@doop)
  @save_yaml_block.call @doop.dump
  return res if res.kind_of? Hash
  {}
end

#indexObject



19
20
21
# File 'lib/doop_controller.rb', line 19

def index
  render_page in_doop { |doop| doop.ask_next }
end

#loadObject



100
101
102
103
104
105
106
107
# File 'lib/doop_controller.rb', line 100

def load
  @doop = Doop.new()
  @self_before_instance_eval = eval "self", @block.binding
  instance_exec @doop, &@block
  @doop.yaml = @load_yaml_block.call
  @doop.init
  @doop
end

#load_yaml(&block) ⇒ Object



148
149
150
# File 'lib/doop_controller.rb', line 148

def load_yaml &block
  @load_yaml_block = block
end

#parent_of(path) ⇒ Object



75
76
77
78
79
# File 'lib/doop_controller.rb', line 75

def parent_of path
  p = path.split("/")
  p.pop
  p.join("/")
end

#render_page(res = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/doop_controller.rb', line 81

def render_page res = {}

  redirect_url = res[:redirect]

  if ! redirect_url.nil?
    @controller.respond_to do |format|
      format.js { @controller.render :js => "window.location.href='#{redirect_url}'" }
      format.html { redirect_to redirect_url }
    end
    return
  end

  res[:page] = get_page
  res[:page_path] = get_page_path
  res[:page_title] = @doop[res[:page_path]]["_nav_name"]
  res[:all_pages] = get_all_pages
  @controller.render "index", :locals => { :res => res, :doop => @doop }
end

#save_yaml(&block) ⇒ Object



152
153
154
# File 'lib/doop_controller.rb', line 152

def save_yaml &block
  @save_yaml_block = block
end