sinatra-packrat 0.0.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/sinatra/packrat.rb +147 -0
- metadata +63 -0
@@ -0,0 +1,147 @@
|
|
1
|
+
# Gems includes
|
2
|
+
require "sinatra/base"
|
3
|
+
require "yaml"
|
4
|
+
|
5
|
+
module Sinatra
|
6
|
+
module Packrat
|
7
|
+
|
8
|
+
module Helpers
|
9
|
+
# this allows for multiple view directories
|
10
|
+
def find_template(views, name, engine, &block)
|
11
|
+
Array(views).each { |v| super(v, name, engine, &block) }
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
# Overwrite Sinatra::Base private method!!! (is this robust?)
|
16
|
+
#
|
17
|
+
# Allow settings.public to be an Array (or Enumerable...)
|
18
|
+
# for multiple-path static file lookup.
|
19
|
+
def static!
|
20
|
+
return if settings.public.nil?
|
21
|
+
if settings.public.respond_to? :each
|
22
|
+
settings.public.each do |dir|
|
23
|
+
public_dir = File.expand_path(dir)
|
24
|
+
path = File.expand_path(public_dir + unescape(request.path_info))
|
25
|
+
next unless path.start_with?(public_dir) and File.file?(path)
|
26
|
+
|
27
|
+
env['sinatra.static_file'] = path
|
28
|
+
send_file path, :disposition => nil
|
29
|
+
|
30
|
+
break
|
31
|
+
end
|
32
|
+
else
|
33
|
+
super
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.registered(app)
|
39
|
+
app.helpers Packrat::Helpers
|
40
|
+
app.enable :static # this is required for multiple public directories
|
41
|
+
app.set :packrat, {}
|
42
|
+
|
43
|
+
klass = app
|
44
|
+
|
45
|
+
Kernel.class_exec do
|
46
|
+
define_method :packrat do |&block|
|
47
|
+
klass.class_exec &block
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# Module initialization
|
53
|
+
|
54
|
+
# Module views
|
55
|
+
# in order for sinatra to be able to find the views of our modules,
|
56
|
+
# we've modified the "find templates" helper above to allow for multiple
|
57
|
+
# view directories. this allows us to add each module's view directory to an array
|
58
|
+
# and the set sinatra's "views" directory with that array.
|
59
|
+
|
60
|
+
# Module public (static) assets
|
61
|
+
# in order for sinatra to be able to find the static assets of our modules,
|
62
|
+
# we've extended sinatra (in "lib/sinatra_extend") to allow for multiple
|
63
|
+
# public directories. this allows us to add each module's public directory to an array
|
64
|
+
# and the set sinatra's "public" directory with that array.
|
65
|
+
|
66
|
+
def add_view_path(path)
|
67
|
+
cur_view = settings.views || []
|
68
|
+
|
69
|
+
if cur_view.class == String
|
70
|
+
cur_view = Array(cur_view)
|
71
|
+
end
|
72
|
+
|
73
|
+
cur_view << path
|
74
|
+
set :views, cur_view
|
75
|
+
end
|
76
|
+
|
77
|
+
def add_public_path(path)
|
78
|
+
cur_view = settings.public || []
|
79
|
+
|
80
|
+
if cur_view.class == String
|
81
|
+
cur_view = Array(cur_view)
|
82
|
+
end
|
83
|
+
|
84
|
+
cur_view << path
|
85
|
+
set :public, cur_view
|
86
|
+
end
|
87
|
+
|
88
|
+
def register_modules_from_yaml(path)
|
89
|
+
config = YAML.load_file( path )
|
90
|
+
caller_path = File.dirname caller[0]
|
91
|
+
|
92
|
+
config["modules"].each do |mod|
|
93
|
+
|
94
|
+
case mod
|
95
|
+
when String
|
96
|
+
path = mod
|
97
|
+
when Hash
|
98
|
+
path = mod["path"]
|
99
|
+
override_settings = mod["settings"]
|
100
|
+
end
|
101
|
+
|
102
|
+
full_module_path = File.join( caller_path, path )
|
103
|
+
|
104
|
+
if override_settings
|
105
|
+
register_module( full_module_path, override_settings )
|
106
|
+
else
|
107
|
+
register_module full_module_path
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def register_module(base_path, override_config = {})
|
113
|
+
|
114
|
+
# if config file, load / add it to namespaced config hash
|
115
|
+
config_path = File.join( base_path, "/config.yml" )
|
116
|
+
|
117
|
+
if File.exists? config_path
|
118
|
+
module_config = YAML.load_file(config_path)
|
119
|
+
end
|
120
|
+
|
121
|
+
if module_config
|
122
|
+
module_id = module_config["module_id"]
|
123
|
+
|
124
|
+
# put the settings in a namespaces 'm_#{module_id}' location as
|
125
|
+
# well as a consolidated place (in case we want to inspect all)
|
126
|
+
unless module_id.nil?
|
127
|
+
module_config.delete "module_id"
|
128
|
+
module_config.merge! override_config
|
129
|
+
settings.packrat[module_id.to_sym] = module_config
|
130
|
+
set "m_#{module_id}".to_sym,module_config
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
route_path = File.join base_path, "routes.rb"
|
135
|
+
require route_path if File.exists? route_path
|
136
|
+
|
137
|
+
view_path = File.join base_path, 'views'
|
138
|
+
add_view_path(view_path) if File.directory?(view_path)
|
139
|
+
|
140
|
+
public_path = File.join base_path, 'public'
|
141
|
+
add_public_path(public_path) if File.directory?(public_path)
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|
145
|
+
|
146
|
+
register Packrat
|
147
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-packrat
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 0.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Brennan Roberts
|
13
|
+
- Brandon Harvey
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-09-21 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description:
|
23
|
+
email: brennan@oblong.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- lib/sinatra/packrat.rb
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: http://rubygems.org/gems/sinatra-packrat
|
34
|
+
licenses: []
|
35
|
+
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
segments:
|
53
|
+
- 0
|
54
|
+
version: "0"
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.3.6
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: Sinatra extension to enable modular application design
|
62
|
+
test_files: []
|
63
|
+
|