Class: Kontrol::Application

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/kontrol/application.rb

Constant Summary

Constants included from Helpers

Helpers::HTML_ESCAPE

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#h, #link_to, #markdown, #strip_tags, #tag, #urlify

Constructor Details

#initialize(path = '.') ⇒ Application

Returns a new instance of Application.



18
19
20
# File 'lib/kontrol/application.rb', line 18

def initialize(path = '.')
  @path = File.expand_path(path)      
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/kontrol/application.rb', line 140

def method_missing(name, *args, &block)
  if match = name.to_s.match(/^(.*)_path$/)
    if route = router.__find__(match[1])
      route.generate(*args)
    else
      super
    end
  else
    super
  end
end

Class Attribute Details

.routerObject

Returns the value of attribute router.



11
12
13
# File 'lib/kontrol/application.rb', line 11

def router
  @router
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/kontrol/application.rb', line 8

def path
  @path
end

Class Method Details

.map(&block) ⇒ Object



13
14
15
# File 'lib/kontrol/application.rb', line 13

def map(&block)
  @router = Router.new(&block)
end

Instance Method Details

#call(env) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/kontrol/application.rb', line 108

def call(env)
  Thread.current['request'] = Rack::Request.new(env)
  Thread.current['response'] = Rack::Response.new([], 200, { 'Content-Type' => '' })

  route, match = router.__recognize__(request)

  if route
    method = "process_#{route.name}"
    self.class.send(:define_method, method, &route.block)
    send(method, *match.to_a[1..-1])
  else
    response.body = "<h1>404 - Page Not Found</h1>"
    response['Content-Length'] = response.body.size.to_s
    response.status = 404
  end

  response['Content-Type'] = guess_content_type if response['Content-Type'].empty?
  response.finish
end

#cookiesObject



78
# File 'lib/kontrol/application.rb', line 78

def cookies ; request.cookies             end

#deleteObject



87
# File 'lib/kontrol/application.rb', line 87

def delete  ; request.delete? and yield   end

#delete?Boolean

Returns:

  • (Boolean)


83
# File 'lib/kontrol/application.rb', line 83

def delete? ; request.delete?             end

#etag(string) ⇒ Object



51
52
53
# File 'lib/kontrol/application.rb', line 51

def etag(string)
  Digest::SHA1.hexdigest(string)
end

#getObject



85
# File 'lib/kontrol/application.rb', line 85

def get     ; request.get? and yield      end

#get?Boolean

Returns:

  • (Boolean)


81
# File 'lib/kontrol/application.rb', line 81

def get?    ; request.get?                end

#guess_content_typeObject



99
100
101
102
# File 'lib/kontrol/application.rb', line 99

def guess_content_type
  ext = File.extname(request.path_info)[1..-1]
  MIME_TYPES[ext] || 'text/html'
end

#if_modified_since(time) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/kontrol/application.rb', line 55

def if_modified_since(time)
  date = time.respond_to?(:httpdate) ? time.httpdate : time
  response['Last-Modified'] = date
  
  if request.env['HTTP_IF_MODIFIED_SINCE'] == date
    response.status = 304
  else
    yield        
  end
end

#if_none_match(etag) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/kontrol/application.rb', line 66

def if_none_match(etag)
  response['Etag'] = etag
  if request.env['HTTP_IF_NONE_MATCH'] == etag
    response.status = 304
  else
    yield
  end
end

#inspectObject



128
129
130
# File 'lib/kontrol/application.rb', line 128

def inspect
  "#<#{self.class.name} @path=#{path}>"
end

#load_template(file) ⇒ Object



22
23
24
# File 'lib/kontrol/application.rb', line 22

def load_template(file)
  ERB.new(File.read("#{self.path}/templates/#{file}"))
end

#paramsObject



77
# File 'lib/kontrol/application.rb', line 77

def params  ; request.params              end

#postObject



84
# File 'lib/kontrol/application.rb', line 84

def post    ; request.post? and yield     end

#post?Boolean

Returns:

  • (Boolean)


80
# File 'lib/kontrol/application.rb', line 80

def post?   ; request.post?               end

#putObject



86
# File 'lib/kontrol/application.rb', line 86

def put     ; request.put? and yield      end

#put?Boolean

Returns:

  • (Boolean)


82
# File 'lib/kontrol/application.rb', line 82

def put?    ; request.put?                end

#redirect(path) ⇒ Object



94
95
96
97
# File 'lib/kontrol/application.rb', line 94

def redirect(path)
  response['Location'] = path
  response.status = 301
end

#render(name, options = {}) ⇒ Object

Render named template and insert into layout with given variables.



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

def render(name, options = {})
  options = options.merge(:request => request, :params => params)
  content = render_template(name, options)
  layout = options.delete(:layout)
  
  if name[0, 1] == '_'
    return content
    
  elsif layout == false
    response.body = content
  else
    options.merge!(:content => content)
    response.body = render_template(layout || "layout.rhtml", options)
  end

  response['Content-Length'] = response.body.size.to_s
end

#render_template(file, vars) ⇒ Object

Render template with given variables.



27
28
29
30
# File 'lib/kontrol/application.rb', line 27

def render_template(file, vars)
  template = load_template(file) or raise "not found: #{path}"
  Template.render(template, self, "#{self.path}/templates/#{file}", vars)
end

#requestObject



75
# File 'lib/kontrol/application.rb', line 75

def request ; Thread.current['request']   end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
# File 'lib/kontrol/application.rb', line 132

def respond_to?(name)
  if match = name.to_s.match(/^(.*)_path$/)
    router.__find__(match[1])
  else
    super
  end
end

#responseObject



76
# File 'lib/kontrol/application.rb', line 76

def response; Thread.current['response']  end

#routerObject



104
105
106
# File 'lib/kontrol/application.rb', line 104

def router
  self.class.router
end

#sessionObject



79
# File 'lib/kontrol/application.rb', line 79

def session ; request.env['rack.session'] end

#text(s) ⇒ Object



89
90
91
92
# File 'lib/kontrol/application.rb', line 89

def text(s)
  response.body = s
  response['Content-Length'] = response.body.size.to_s
end