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
# File 'lib/doop_controller.rb', line 12

def initialize application_controller, &block
  @controller = application_controller
  @block = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

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

#answerObject



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

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



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

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



50
51
52
53
54
55
# File 'lib/doop_controller.rb', line 50

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

#change_page(path) ⇒ Object



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

def change_page path
  in_doop do |doop|
    doop.change( path )

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

    doop.ask_next
  end
  render_page
end

#current_page(path) ⇒ Object



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

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

#evaluate(block) ⇒ Object



114
115
116
117
# File 'lib/doop_controller.rb', line 114

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

#get_all_pagesObject



152
153
154
155
156
157
158
# File 'lib/doop_controller.rb', line 152

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

#get_pageObject



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

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

#get_page_pathObject



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

def get_page_path
  path = @doop.currently_asked
  current_page( path )
end

#in_doopObject



106
107
108
109
110
111
112
# File 'lib/doop_controller.rb', line 106

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

#indexObject



17
18
19
# File 'lib/doop_controller.rb', line 17

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

#inject_yaml(yaml) ⇒ Object



160
161
162
# File 'lib/doop_controller.rb', line 160

def inject_yaml yaml
  @controller.params["doop_data"] = ActiveSupport::MessageEncryptor.new(Rails.application.secrets.secret_key_base).encrypt_and_sign(yaml)
end

#loadObject



97
98
99
100
101
102
103
104
# File 'lib/doop_controller.rb', line 97

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

#load_yamlObject



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

def load_yaml
  data = params["doop_data"] 
  return ActiveSupport::MessageEncryptor.new(Rails.application.secrets.secret_key_base).decrypt_and_verify(data) if !data.nil?
  @yaml_block.call
end

#parent_of(path) ⇒ Object



72
73
74
75
76
# File 'lib/doop_controller.rb', line 72

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

#render_page(res = {}) ⇒ Object



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

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(yaml) ⇒ Object



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

def save_yaml yaml
  @controller.request["doop_data"] = ActiveSupport::MessageEncryptor.new(Rails.application.secrets.secret_key_base).encrypt_and_sign(yaml)
end

#yaml(&block) ⇒ Object



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

def yaml &block
  @yaml_block = block
end