Class: Ezframe::Controller

Inherits:
Object show all
Defined in:
lib/ezframe/controller.rb

Class Method Summary collapse

Class Method Details

.exec(request, response) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ezframe/controller.rb', line 14

def exec(request, response)
  @request = request
  model = Model.get_clone
  @request.env["model"] = model

  mylog("exec: path=#{request.path_info} params=#{request.params}")
  page_instance, method, url_params = Route::choose(request)
  mylog "page: #{page_instance.class}, method=#{method}, url_params=#{url_params}"
  if !page_instance || page_instance == 404
    file_not_found(response)
    return
  end
  @request.env["url_params"] = url_params
  warden.authenticate! if page_instance.auth
  mylog "rack.session.keys=#{request.env['rack.session'].keys}"
  page_instance.set_request(@request)
  body = page_instance.send(method)

  # 戻り値によるレスポンス生成
  if body.is_a?(Hash) || body.is_a?(Array)
    # puts  "Controller: body = #{body}"
    response.body = [ JSON.generate(body) ]
    # response.body = [ Oj.dump(body) ]
    response['Content-Type'] = 'application/json; charset=utf-8'
  else
    response.body = [ body ]
    response['Content-Type'] = 'text/html; charset=utf-8'
  end
  response.status = 200
  # puts response.body
end

.file_not_found(response) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ezframe/controller.rb', line 46

def file_not_found(response)
  response.status = 404
  response['Content-Type'] = 'text/html; charset=utf-8'
  template_file = ("#{Config[:template_dir]}/404.html")
  # puts template_file
  if File.exist?(template_file) 
    body = File.read(template_file)
  else
    body = Html.convert(Ht.p("file not found"))
  end
  response.body = [ body ]
end

.initObject



7
8
9
10
11
12
# File 'lib/ezframe/controller.rb', line 7

def init
  Config.load_files("./config")
  Model.init
  Message.init
  Auth.init_warden if defined?(Warden)
end

.login?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/ezframe/controller.rb', line 63

def login?
  !!warden.user
end

.userObject



67
68
69
# File 'lib/ezframe/controller.rb', line 67

def user
  warden.user
end

.wardenObject



59
60
61
# File 'lib/ezframe/controller.rb', line 59

def warden
  @request.env["warden"]
end