baby_erubis 2.1.0 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e4e3e54829bebc2831ffe6e689adb929da5651f1
4
- data.tar.gz: b96bd245ccfad8a9da0bf544adcd0810faf95cbc
3
+ metadata.gz: 76564972abf7edb2174fbf55fda794b9624da97e
4
+ data.tar.gz: d6a991b9b800c828ee33be148bf0349d3e1fb0c9
5
5
  SHA512:
6
- metadata.gz: c5848f552207930cd84cb1e6babb2de0e102010f623b658e2a845870ab4d7ce0232f2ed0c734bfee32d1492e9c0ec08a29608c14f810d64170785896a707999f
7
- data.tar.gz: e353121aaabdc3412b71c4dae60f164e7602bf5fedea44a0918bb631681365641e34857abc237971355786e602d7d00246648cd1e55cc02bae4ab4b2c0a5cc3a
6
+ metadata.gz: 5162aa116047a2c42dfafdc5c198b44fa09ae15220f36d11eef1e35e3b36ec8b38735183ac143db3373c8b4f66ccc57db020a9ebe9f4357c9fbdd04029a59558
7
+ data.tar.gz: 49e06c5e774e87040803671c4d959a3b1e0ea1a1e00e238a45b9f295fef70a3878ee1828915ac0b5b9c8392b883a98f4bdc9b5d935d39479102ef927c343ee55
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  BabyErubis.rb
2
2
  =============
3
3
 
4
- $Release: 2.1.0 $
4
+ $Release: 2.1.1 $
5
5
 
6
6
  BabyErubis is an yet another eRuby implementation, based on Erubis.
7
7
 
data/Rakefile CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ###
4
4
 
5
- RELEASE = ENV['rel'] || '2.1.0'
5
+ RELEASE = ENV['rel'] || '2.1.1'
6
6
  COPYRIGHT = 'copyright(c) 2014-2015 kuwata-lab.com all rights reserved'
7
7
  LICENSE = 'MIT License'
8
8
 
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/ruby
2
2
 
3
3
  ###
4
- ### $Release: 2.1.0 $
4
+ ### $Release: 2.1.1 $
5
5
  ### $License: MIT License $
6
6
  ### $Copyright: copyright(c) 2014-2015 kuwata-lab.com all rights reserved $
7
7
  ###
@@ -13,7 +13,7 @@ Gem::Specification.new do |s|
13
13
  s.name = "baby_erubis"
14
14
  s.author = "makoto kuwata"
15
15
  s.email = "kwa(at)kuwata-lab.com"
16
- s.version = "$Release: 2.1.0 $".split()[1]
16
+ s.version = "$Release: 2.1.1 $".split()[1]
17
17
  s.license = "MIT License"
18
18
  s.platform = Gem::Platform::RUBY
19
19
  s.homepage = "https://github.com/kwatch/BabyErubis/tree/ruby"
@@ -33,6 +33,7 @@ END
33
33
  files = []
34
34
  files << 'lib/baby_erubis.rb'
35
35
  files << 'lib/baby_erubis/rails.rb'
36
+ files << 'lib/baby_erubis/renderer.rb'
36
37
  files += Dir.glob('test/*.rb')
37
38
  files += ['bin/baby_erubis']
38
39
  files += %w[README.md MIT-LICENSE setup.rb baby_erubis.gemspec Rakefile]
@@ -2,7 +2,7 @@
2
2
  # -*- coding: utf-8 -*-
3
3
 
4
4
  ###
5
- ### $Release: 2.1.0 $
5
+ ### $Release: 2.1.1 $
6
6
  ### $Copyright: copyright(c) 2014-2015 kuwata-lab.com all rights reserved $
7
7
  ### $License: MIT License $
8
8
  ###
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  ###
4
- ### $Release: 2.1.0 $
4
+ ### $Release: 2.1.1 $
5
5
  ### $Copyright: copyright(c) 2014-2015 kuwata-lab.com all rights reserved $
6
6
  ### $License: MIT License $
7
7
  ###
@@ -25,7 +25,7 @@
25
25
 
26
26
  module BabyErubis
27
27
 
28
- RELEASE = '$Release: 2.1.0 $'.split(' ')[1]
28
+ RELEASE = '$Release: 2.1.1 $'.split(' ')[1]
29
29
 
30
30
 
31
31
  class TemplateError < StandardError
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  ###
4
- ### $Release: 2.1.0 $
4
+ ### $Release: 2.1.1 $
5
5
  ### $Copyright: copyright(c) 2014-2015 kuwata-lab.com all rights reserved $
6
6
  ### $License: MIT License $
7
7
  ###
@@ -0,0 +1,139 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ###
4
+ ### $Release: 2.1.1 $
5
+ ### $Copyright: copyright(c) 2014-2015 kuwata-lab.com all rights reserved $
6
+ ### $License: MIT License $
7
+ ###
8
+
9
+
10
+ module BabyErubis
11
+
12
+ ##
13
+ ## Module to define template rendering methods.
14
+ ##
15
+ ## ex:
16
+ ## class MyController
17
+ ## include BabyErubis::HtmlEscaper
18
+ ## include BabyErubis::Renderer
19
+ ##
20
+ ## ERUBY_PATH = ['.', 'templates']
21
+ ## ERUBY_LAYOUT = :_layout
22
+ ## ERUBY_HTML = BabyErubis::Html
23
+ ## ERUBY_HTML_EXT = '.html.eruby'
24
+ ## ERUBY_TEXT = BabyErubis::Text
25
+ ## ERUBY_TEXT_EXT = '.eruby'
26
+ ## ERUBY_CACHE = {}
27
+ ##
28
+ ## def index
29
+ ## @items = ['A', 'B', 'C']
30
+ ## ## renders 'templates/welcome.html.eruby'
31
+ ## html = eruby_render_html(:welcome)
32
+ ## return html
33
+ ## end
34
+ ##
35
+ ## end
36
+ ##
37
+ ##
38
+ module Renderer
39
+
40
+ ERUBY_PATH = ['.']
41
+ ERUBY_LAYOUT = :_layout
42
+ ERUBY_HTML = BabyErubis::Html
43
+ ERUBY_HTML_EXT = '.html.eruby'
44
+ ERUBY_TEXT = BabyErubis::Text
45
+ ERUBY_TEXT_EXT = '.eruby'
46
+ ERUBY_CACHE = {}
47
+
48
+ def eruby_render_html(template_name, layout: true, encoding: 'utf-8')
49
+ return _eruby_render_template(template_name, layout) {|tmpl_name|
50
+ ext = self.class.const_get :ERUBY_HTML_EXT
51
+ _eruby_find_template("#{tmpl_name}#{ext}") {|fpath|
52
+ tmpl_class = self.class.const_get :ERUBY_HTML
53
+ tmpl_class.new.from_file(fpath, encoding)
54
+ }
55
+ }
56
+ end
57
+
58
+ def eruby_render_text(template_name, layout: false, encoding: 'utf-8')
59
+ return _eruby_render_template(template_name, layout) {|tmpl_name|
60
+ ext = self.class.const_get :ERUBY_TEXT_EXT
61
+ _eruby_find_template("#{tmpl_name}#{ext}") {|fpath|
62
+ tmpl_class = self.class.const_get :ERUBY_TEXT
63
+ tmpl_class.new.from_file(fpath, encoding)
64
+ }
65
+ }
66
+ end
67
+
68
+ private
69
+
70
+ def _eruby_find_template(filename)
71
+ cache = self.class.const_get :ERUBY_CACHE
72
+ paths = self.class.const_get :ERUBY_PATH
73
+ dir = paths.find {|path| cache.key?("#{path}/#{filename}") } \
74
+ || paths.find {|path| File.file?("#{path}/#{filename}") } or
75
+ raise BabyErubis::TemplateError.new("#{filename}: template not found in #{paths.inspect}.")
76
+ fpath = "#{dir}/#{filename}"
77
+ #
78
+ now = Time.now
79
+ template = _eruby_load_template(cache, fpath, now)
80
+ unless template
81
+ mtime = File.mtime(fpath)
82
+ template = yield fpath
83
+ ## retry when file timestamp changed during template loading
84
+ unless mtime == (mtime2 = File.mtime(fpath))
85
+ mtime = mtime2
86
+ template = yield fpath
87
+ mtime == File.mtime(fpath) or
88
+ raise "#{fpath}: timestamp changes too frequently. something wrong."
89
+ end
90
+ _eruby_store_template(cache, fpath, template, mtime, now)
91
+ end
92
+ return template
93
+ end
94
+
95
+ def _eruby_load_template(cache, fpath, now)
96
+ tuple = cache[fpath]
97
+ template, timestamp, last_checked = tuple
98
+ return nil unless template
99
+ ## skip timestamp check in order to reduce syscall (= File.mtime())
100
+ interval = now - last_checked
101
+ return template if interval < 0.5
102
+ ## check timestamp only for 5% request in order to avoid thundering herd
103
+ return template if interval < 1.0 && rand() > 0.05
104
+ ## update last_checked in cache when file timestamp is not changed
105
+ if timestamp == File.mtime(fpath)
106
+ tuple[2] = now
107
+ return template
108
+ ## remove cache entry when file timestamp is changed
109
+ else
110
+ cache[fpath] = nil
111
+ return nil
112
+ end
113
+ end
114
+
115
+ def _eruby_store_template(cache, fpath, template, timestamp, last_checked)
116
+ cache[fpath] = [template, timestamp, last_checked]
117
+ end
118
+
119
+ def _eruby_render_template(template_name, layout)
120
+ template = yield template_name
121
+ s = template.render(self)
122
+ unless @_layout.nil?
123
+ layout = @_layout; @_layout = nil
124
+ end
125
+ while layout
126
+ layout = self.class.const_get :ERUBY_LAYOUT if layout == true
127
+ template = yield layout
128
+ @_content = s
129
+ s = template.render(self)
130
+ @_content = nil
131
+ layout = @_layout; @_layout = nil
132
+ end
133
+ return s
134
+ end
135
+
136
+ end
137
+
138
+
139
+ end
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  ###
4
- ### $Release: 2.1.0 $
4
+ ### $Release: 2.1.1 $
5
5
  ### $Copyright: copyright(c) 2014-2015 kuwata-lab.com all rights reserved $
6
6
  ### $License: MIT License $
7
7
  ###
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  ###
4
- ### $Release: 2.1.0 $
4
+ ### $Release: 2.1.1 $
5
5
  ### $Copyright: copyright(c) 2014-2015 kuwata-lab.com all rights reserved $
6
6
  ### $License: MIT License $
7
7
  ###
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  ###
4
- ### $Release: 2.1.0 $
4
+ ### $Release: 2.1.1 $
5
5
  ### $Copyright: copyright(c) 2014-2015 kuwata-lab.com all rights reserved $
6
6
  ### $License: MIT License $
7
7
  ###
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  ###
4
- ### $Release: 2.1.0 $
4
+ ### $Release: 2.1.1 $
5
5
  ### $Copyright: copyright(c) 2014-2015 kuwata-lab.com all rights reserved $
6
6
  ### $License: MIT License $
7
7
  ###
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  ###
4
- ### $Release: 2.1.0 $
4
+ ### $Release: 2.1.1 $
5
5
  ### $Copyright: copyright(c) 2014-2015 kuwata-lab.com all rights reserved $
6
6
  ### $License: MIT License $
7
7
  ###
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  ###
4
- ### $Release: 2.1.0 $
4
+ ### $Release: 2.1.1 $
5
5
  ### $Copyright: copyright(c) 2014-2015 kuwata-lab.com all rights reserved $
6
6
  ### $License: MIT License $
7
7
  ###
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: baby_erubis
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - makoto kuwata
@@ -32,6 +32,7 @@ files:
32
32
  - bin/baby_erubis
33
33
  - lib/baby_erubis.rb
34
34
  - lib/baby_erubis/rails.rb
35
+ - lib/baby_erubis/renderer.rb
35
36
  - setup.rb
36
37
  - test/context_test.rb
37
38
  - test/rails_test.rb