kontrol 0.3 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -20,13 +20,13 @@ module Kontrol
20
20
  end
21
21
 
22
22
  def load_template(file)
23
- ERB.new(File.read("#{self.path}/templates/#{file}"))
23
+ @templates[file] ||= Template.new(self, "#{self.path}/templates/#{file}")
24
24
  end
25
25
 
26
26
  # Render template with given variables.
27
- def render_template(file, vars)
28
- template = load_template(file) or raise "not found: #{path}"
29
- Template.render(template, self, "#{self.path}/templates/#{file}", vars)
27
+ def render_template(file, variables)
28
+ template = load_template(file) or raise "template not found: #{path}"
29
+ template.render(variables)
30
30
  end
31
31
 
32
32
  # Render named template and insert into layout with given variables.
@@ -1,27 +1,51 @@
1
1
  module Kontrol
2
2
 
3
+ class View
4
+
5
+ def initialize(variables)
6
+ variables.each do |k, v|
7
+ instance_variable_set "@#{k}", v
8
+ end
9
+ end
10
+
11
+ def __binding__
12
+ binding
13
+ end
14
+
15
+ def method_missing(id, *args, &block)
16
+ if @__app__.respond_to?(id)
17
+ @__app__.send(id, *args, &block)
18
+ else
19
+ super
20
+ end
21
+ end
22
+ end
23
+
3
24
  # This class renders an ERB template for a set of attributes, which
4
25
  # are accessible as instance variables.
5
26
  class Template
6
27
  include Helpers
7
28
 
8
- # Initialize this template with an ERB instance.
9
- def initialize(app, vars)
10
- @__app__ = app
29
+ def initialize(app, file)
30
+ @app = app
31
+ @file = file
11
32
 
12
- vars.each do |k, v|
13
- instance_variable_set "@#{k}", v
14
- end
15
- end
16
-
17
- def __binding__
18
- binding
33
+ load
19
34
  end
20
35
 
21
- def self.render(erb, app, file, vars)
22
- template = Template.new(app, vars)
36
+ def load
37
+ @mtime = File.mtime(file)
38
+ @erb = ERB.new(File.read(file))
39
+ end
40
+
41
+ def changed?
42
+ File.mtime(@file) != @mtime
43
+ end
44
+
45
+ def render(variables)
46
+ load if ENV['RACK_ENV'] != 'production' and changed?
23
47
 
24
- return erb.result(template.__binding__)
48
+ @erb.result(View.new(@app, variables).__binding__)
25
49
 
26
50
  rescue => e
27
51
  e.backtrace.each do |s|
@@ -30,13 +54,6 @@ module Kontrol
30
54
  raise e
31
55
  end
32
56
 
33
- def method_missing(id, *args, &block)
34
- if @__app__.respond_to?(id)
35
- return @__app__.send(id, *args, &block)
36
- end
37
- super
38
- end
39
-
40
57
  end
41
58
 
42
59
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kontrol
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.3"
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthias Georgi
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-07 00:00:00 +01:00
12
+ date: 2010-02-08 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15