chuusha 0.1.0
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.
- data/lib/chuusha.rb +110 -0
- metadata +89 -0
data/lib/chuusha.rb
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'erubis'
|
3
|
+
|
4
|
+
module Chuusha
|
5
|
+
class Rack
|
6
|
+
def initialize(app, root_dir, config=nil)
|
7
|
+
@app = app
|
8
|
+
@root_dir = root_dir
|
9
|
+
@config = Config.new(config)
|
10
|
+
cache_everything if @config.cache_on_load?
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(env)
|
14
|
+
renderer = Renderer.new(@config, @root_dir + env["PATH_INFO"])
|
15
|
+
return renderer.respond if renderer.template_exists?
|
16
|
+
@app.call(env)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
def cache_everything
|
21
|
+
template_files.each do |file|
|
22
|
+
renderer = Renderer.new(@config, file)
|
23
|
+
renderer.write_cached_copy
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def template_files
|
28
|
+
Dir[@root_dir + "/**/*.erb"].map do |f|
|
29
|
+
f.gsub(/\.erb$/, '')
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class Config
|
35
|
+
attr_reader :variables
|
36
|
+
|
37
|
+
def initialize(config)
|
38
|
+
@config = load_config(config)
|
39
|
+
@variables = @config["variables"] || {}
|
40
|
+
@cache = @config["cache"] || {}
|
41
|
+
default_cache_on_load_to_true
|
42
|
+
end
|
43
|
+
|
44
|
+
def cache_envs
|
45
|
+
@cache["envs"] || ["production"]
|
46
|
+
end
|
47
|
+
|
48
|
+
def cache?
|
49
|
+
# If we're in rails, always cache
|
50
|
+
ENV['RAILS_ENV'] || cache_envs.include?(ENV['RACK_ENV'])
|
51
|
+
end
|
52
|
+
|
53
|
+
def cache_on_load?
|
54
|
+
@cache["on_load"] && cache?
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
def default_cache_on_load_to_true
|
59
|
+
@cache["on_load"] = if @cache["on_load"].nil?
|
60
|
+
true
|
61
|
+
else
|
62
|
+
@cache["on_load"]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def load_config(config)
|
67
|
+
case config
|
68
|
+
when String
|
69
|
+
YAML.load_file(config)
|
70
|
+
when Hash
|
71
|
+
config
|
72
|
+
else
|
73
|
+
{}
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
class Renderer
|
79
|
+
def initialize(config, path)
|
80
|
+
@config = config
|
81
|
+
@outfile = path
|
82
|
+
@path = @outfile + ".erb"
|
83
|
+
@evaluated = nil
|
84
|
+
end
|
85
|
+
|
86
|
+
def template_exists?
|
87
|
+
::File.exist?(@path)
|
88
|
+
end
|
89
|
+
|
90
|
+
def evaluated
|
91
|
+
return @evaluated if @evaluated
|
92
|
+
eruby = Erubis::Eruby.new(::File.read(@path))
|
93
|
+
@evaluated = eruby.result(@config.variables)
|
94
|
+
end
|
95
|
+
|
96
|
+
def write_cached_copy
|
97
|
+
::File.open(@outfile, "w") { |f| f.write evaluated }
|
98
|
+
end
|
99
|
+
|
100
|
+
def render
|
101
|
+
write_cached_copy if @config.cache?
|
102
|
+
evaluated
|
103
|
+
end
|
104
|
+
|
105
|
+
def respond
|
106
|
+
# TODO: Should get right content-type
|
107
|
+
[200, {"Content-Type" => "text/html"}, render]
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: chuusha
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Trotter Cashion
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-04-25 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rack
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 1
|
30
|
+
- 0
|
31
|
+
version: 1.1.0
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: erubis
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 2
|
43
|
+
- 6
|
44
|
+
- 5
|
45
|
+
version: 2.6.5
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
48
|
+
description: " Chuusha allows you to evaluate both javascript and css templates \n within the same context, meaning that you can share constants between the\n templates.\n"
|
49
|
+
email: cashion@gmail.com
|
50
|
+
executables: []
|
51
|
+
|
52
|
+
extensions: []
|
53
|
+
|
54
|
+
extra_rdoc_files: []
|
55
|
+
|
56
|
+
files:
|
57
|
+
- lib/chuusha.rb
|
58
|
+
has_rdoc: true
|
59
|
+
homepage: http://trottercashion.com
|
60
|
+
licenses: []
|
61
|
+
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.3.6
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Run templates through erb and cache the resulting static file
|
88
|
+
test_files: []
|
89
|
+
|