doozer 0.2.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/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +57 -0
- data/Rakefile +62 -0
- data/VERSION +1 -0
- data/bin/doozer +6 -0
- data/doozer.gemspec +156 -0
- data/lib/doozer.rb +35 -0
- data/lib/doozer/active_support/array.rb +14 -0
- data/lib/doozer/active_support/class.rb +221 -0
- data/lib/doozer/active_support/date_time.rb +23 -0
- data/lib/doozer/active_support/object.rb +43 -0
- data/lib/doozer/active_support/time.rb +32 -0
- data/lib/doozer/app.rb +294 -0
- data/lib/doozer/configs.rb +146 -0
- data/lib/doozer/controller.rb +340 -0
- data/lib/doozer/exceptions.rb +12 -0
- data/lib/doozer/extend.rb +10 -0
- data/lib/doozer/initializer.rb +104 -0
- data/lib/doozer/lib.rb +32 -0
- data/lib/doozer/logger.rb +12 -0
- data/lib/doozer/orm/active_record.rb +28 -0
- data/lib/doozer/orm/data_mapper.rb +22 -0
- data/lib/doozer/orm/sequel.rb +21 -0
- data/lib/doozer/partial.rb +99 -0
- data/lib/doozer/plugins/paginate/init.rb +2 -0
- data/lib/doozer/plugins/paginate/lib/paginate.rb +32 -0
- data/lib/doozer/plugins/paginate/lib/paginate/collection.rb +60 -0
- data/lib/doozer/plugins/paginate/lib/paginate/finder.rb +116 -0
- data/lib/doozer/plugins/paginate/lib/paginate/view_helpers.rb +37 -0
- data/lib/doozer/rackup/server.ru +35 -0
- data/lib/doozer/rackup/test.rb +20 -0
- data/lib/doozer/redirect.rb +12 -0
- data/lib/doozer/route.rb +292 -0
- data/lib/doozer/scripts/cluster.rb +126 -0
- data/lib/doozer/scripts/console.rb +2 -0
- data/lib/doozer/scripts/migrate.rb +108 -0
- data/lib/doozer/scripts/task.rb +60 -0
- data/lib/doozer/scripts/test.rb +23 -0
- data/lib/doozer/version.rb +8 -0
- data/lib/doozer/view_helpers.rb +251 -0
- data/lib/doozer/watcher.rb +369 -0
- data/lib/generator/generator.rb +548 -0
- data/templates/skeleton/Rakefile +3 -0
- data/templates/skeleton/app/controllers/application_controller.rb +2 -0
- data/templates/skeleton/app/controllers/index_controller.rb +7 -0
- data/templates/skeleton/app/helpers/application_helper.rb +17 -0
- data/templates/skeleton/app/views/global/_header.html.erb +7 -0
- data/templates/skeleton/app/views/global/_navigation.html.erb +6 -0
- data/templates/skeleton/app/views/index/index.html.erb +108 -0
- data/templates/skeleton/app/views/layouts/default.html.erb +23 -0
- data/templates/skeleton/config/app.yml +31 -0
- data/templates/skeleton/config/boot.rb +17 -0
- data/templates/skeleton/config/database.yml +25 -0
- data/templates/skeleton/config/environment.rb +11 -0
- data/templates/skeleton/config/rack.rb +30 -0
- data/templates/skeleton/config/routes.rb +69 -0
- data/templates/skeleton/script/cluster +4 -0
- data/templates/skeleton/script/console +15 -0
- data/templates/skeleton/script/migrate +4 -0
- data/templates/skeleton/script/task +4 -0
- data/templates/skeleton/script/test +4 -0
- data/templates/skeleton/static/404.html +16 -0
- data/templates/skeleton/static/500.html +16 -0
- data/templates/skeleton/static/css/style.css +32 -0
- data/templates/skeleton/static/favicon.ico +0 -0
- data/templates/skeleton/static/js/application.js +1 -0
- data/templates/skeleton/static/js/jquery-1.3.min.js +19 -0
- data/templates/skeleton/static/robots.txt +5 -0
- data/templates/skeleton/test/fixtures/setup.rb +6 -0
- data/templates/skeleton/test/setup.rb +33 -0
- data/test/doozer_test.rb +7 -0
- data/test/project/Rakefile +3 -0
- data/test/project/app/controllers/application_controller.rb +2 -0
- data/test/project/app/controllers/index_controller.rb +7 -0
- data/test/project/app/helpers/application_helper.rb +17 -0
- data/test/project/app/views/global/_header.html.erb +7 -0
- data/test/project/app/views/global/_navigation.html.erb +6 -0
- data/test/project/app/views/index/index.html.erb +108 -0
- data/test/project/app/views/layouts/default.html.erb +23 -0
- data/test/project/config/app.yml +31 -0
- data/test/project/config/boot.rb +17 -0
- data/test/project/config/database.yml +25 -0
- data/test/project/config/environment.rb +11 -0
- data/test/project/config/rack.rb +30 -0
- data/test/project/config/routes.rb +72 -0
- data/test/project/script/cluster +4 -0
- data/test/project/script/console +15 -0
- data/test/project/script/migrate +4 -0
- data/test/project/script/task +4 -0
- data/test/project/script/test +4 -0
- data/test/project/static/404.html +16 -0
- data/test/project/static/500.html +16 -0
- data/test/project/static/css/style.css +32 -0
- data/test/project/static/favicon.ico +0 -0
- data/test/project/static/js/application.js +1 -0
- data/test/project/static/js/jquery-1.3.min.js +19 -0
- data/test/project/static/robots.txt +5 -0
- data/test/project/test/fixtures/setup.rb +6 -0
- data/test/project/test/setup.rb +33 -0
- data/test/routing_test.rb +66 -0
- data/test/test_helper.rb +26 -0
- metadata +169 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Default DATE_FORMATS available
|
|
2
|
+
#
|
|
3
|
+
# Time::DATE_FORMATS.merge!({
|
|
4
|
+
# :db => "%Y-%m-%d %H:%M:%S",
|
|
5
|
+
# :number => "%Y%m%d%H%M%S",
|
|
6
|
+
# :time => "%H:%M",
|
|
7
|
+
# :mdy => "%B %d, %Y",
|
|
8
|
+
# :short => "%d %b %H:%M",
|
|
9
|
+
# :long => "%B %d, %Y %H:%M"
|
|
10
|
+
# })
|
|
11
|
+
# === Example Useage
|
|
12
|
+
# DateTime.now().to_format(:mdy)
|
|
13
|
+
#
|
|
14
|
+
class DateTime
|
|
15
|
+
# Helper method for string formatting DateTime.
|
|
16
|
+
def to_format(key = :default)
|
|
17
|
+
if format = ::Time::DATE_FORMATS[key]
|
|
18
|
+
strftime(format)
|
|
19
|
+
else
|
|
20
|
+
to_s
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
class Object
|
|
2
|
+
# Can you safely .dup this object?
|
|
3
|
+
# False for nil, false, true, symbols, numbers, and class objects; true otherwise.
|
|
4
|
+
def duplicable?
|
|
5
|
+
true
|
|
6
|
+
end
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
class NilClass #:nodoc:
|
|
10
|
+
def duplicable?
|
|
11
|
+
false
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
class FalseClass #:nodoc:
|
|
16
|
+
def duplicable?
|
|
17
|
+
false
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
class TrueClass #:nodoc:
|
|
22
|
+
def duplicable?
|
|
23
|
+
false
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
class Symbol #:nodoc:
|
|
28
|
+
def duplicable?
|
|
29
|
+
false
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
class Numeric #:nodoc:
|
|
34
|
+
def duplicable?
|
|
35
|
+
false
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
class Class #:nodoc:
|
|
40
|
+
def duplicable?
|
|
41
|
+
false
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Default DATE_FORMATS available
|
|
2
|
+
#
|
|
3
|
+
# Time::DATE_FORMATS.merge!({
|
|
4
|
+
# :db => "%Y-%m-%d %H:%M:%S",
|
|
5
|
+
# :number => "%Y%m%d%H%M%S",
|
|
6
|
+
# :time => "%H:%M",
|
|
7
|
+
# :mdy => "%B %d, %Y",
|
|
8
|
+
# :short => "%d %b %H:%M",
|
|
9
|
+
# :long => "%B %d, %Y %H:%M"
|
|
10
|
+
# })
|
|
11
|
+
# === Example Useage
|
|
12
|
+
# DateTime.now().to_format(:mdy)
|
|
13
|
+
#
|
|
14
|
+
class Time
|
|
15
|
+
# Helper method for string formatting Time.
|
|
16
|
+
DATE_FORMATS = {
|
|
17
|
+
:db => "%Y-%m-%d %H:%M:%S",
|
|
18
|
+
:number => "%Y%m%d%H%M%S",
|
|
19
|
+
:time => "%H:%M",
|
|
20
|
+
:mdy => "%B %d, %Y",
|
|
21
|
+
:short => "%d %b %H:%M",
|
|
22
|
+
:long => "%B %d, %Y %H:%M"
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
def to_format(key = :default)
|
|
26
|
+
if format = ::Time::DATE_FORMATS[key]
|
|
27
|
+
strftime(format)
|
|
28
|
+
else
|
|
29
|
+
to_s
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
data/lib/doozer/app.rb
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
module Doozer
|
|
2
|
+
class App
|
|
3
|
+
include Doozer::Util::Logger
|
|
4
|
+
attr_accessor :options
|
|
5
|
+
|
|
6
|
+
def initialize(args={})
|
|
7
|
+
@options=args
|
|
8
|
+
|
|
9
|
+
# load routes
|
|
10
|
+
load_routes
|
|
11
|
+
|
|
12
|
+
# load the application models, coontrollers, views, and helpers
|
|
13
|
+
load_files
|
|
14
|
+
|
|
15
|
+
# attach the file watcher for the mvc/lib/etc in development mode
|
|
16
|
+
load_watcher if Doozer::Configs.rack_env != :deployment
|
|
17
|
+
|
|
18
|
+
printf "Doozer racked up...\n"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# This method is called along the rackup chain and maps the request path to the route, controller, and view for the format type.
|
|
22
|
+
def call(env)
|
|
23
|
+
# p env.inspect
|
|
24
|
+
# [200, {"Content-Type" => "text/html"}, "DOH!!!"]
|
|
25
|
+
path = env["PATH_INFO"]
|
|
26
|
+
# match env.path_info against the route compile
|
|
27
|
+
#p env.inspect
|
|
28
|
+
route = Doozer::Routing::Routes::match(path)
|
|
29
|
+
# p "path: #{path}"
|
|
30
|
+
# p "route: #{route.inspect}"
|
|
31
|
+
if not route.nil?
|
|
32
|
+
if route.app.nil?
|
|
33
|
+
extra_params = route.extra_params(path)
|
|
34
|
+
controller_klass = handler(route.controller.to_sym)
|
|
35
|
+
controller = controller_klass.new({:env=>env, :route=>route, :extra_params=>extra_params, :port=>@options[:Port]})
|
|
36
|
+
|
|
37
|
+
# call after_initialize test for excludes
|
|
38
|
+
execution_time('controller.after_initialize',:start)
|
|
39
|
+
controller.after_initialize if not controller_klass.after_initialize_exclude.include?(route.action.to_sym)
|
|
40
|
+
execution_time(nil, :end)
|
|
41
|
+
|
|
42
|
+
begin
|
|
43
|
+
|
|
44
|
+
# call before_filter test for excludes
|
|
45
|
+
execution_time('controller.before_filter',:start)
|
|
46
|
+
controller.before_filter if not controller_klass.before_filter_exclude.include?(route.action.to_sym)
|
|
47
|
+
execution_time(nil,:end)
|
|
48
|
+
|
|
49
|
+
# call the action method
|
|
50
|
+
execution_time('controller.method(route.action).call',:start)
|
|
51
|
+
controller.method(route.action).call()
|
|
52
|
+
execution_time(nil,:end)
|
|
53
|
+
|
|
54
|
+
# call after_filter test for excludes
|
|
55
|
+
execution_time('controller.after_filter',:start)
|
|
56
|
+
controller.after_filter if not controller_klass.after_filter_exclude.include?(route.action.to_sym)
|
|
57
|
+
execution_time(nil, :end)
|
|
58
|
+
|
|
59
|
+
# render controller...
|
|
60
|
+
execution_time('controller.render_result',:start)
|
|
61
|
+
# r = Rack::Response.new(controller.render_controller(view, layout), route.status, {"Content-Type" => route.content_type})
|
|
62
|
+
r = Rack::Response.new(controller.render_result, route.status, {"Content-Type" => route.content_type})
|
|
63
|
+
execution_time(nil,:end)
|
|
64
|
+
r.set_cookie('flash',{:value=>nil, :path=>'/'})
|
|
65
|
+
r.set_cookie('session',{:value=>controller.session_to_cookie(), :path=>'/'})
|
|
66
|
+
|
|
67
|
+
# finalize the request
|
|
68
|
+
controller.finished!
|
|
69
|
+
controller = nil
|
|
70
|
+
return r.to_a
|
|
71
|
+
|
|
72
|
+
rescue Doozer::Redirect => redirect
|
|
73
|
+
# set the status to the one defined in the route which type of redirect do we need to handle?
|
|
74
|
+
status = (route.status==301) ? 301 : 302
|
|
75
|
+
# check to make sure the status wasn't manually changed in the controller
|
|
76
|
+
status = redirect.status if not redirect.status.nil?
|
|
77
|
+
|
|
78
|
+
r = Rack::Response.new("redirect...", status, {"Content-Type" => "text/html", "Location"=>redirect.url})
|
|
79
|
+
# if we get a redirect we need to do something with the flash messages...
|
|
80
|
+
r.set_cookie('flash',{:value=>controller.flash_to_cookie(), :path=>'/'}) # might need to set the domain from config app_name value
|
|
81
|
+
r.set_cookie('session',{:value=>controller.session_to_cookie(), :path=>'/'})
|
|
82
|
+
|
|
83
|
+
# finalize the request
|
|
84
|
+
controller.finished!
|
|
85
|
+
controller = nil
|
|
86
|
+
return r.to_a
|
|
87
|
+
rescue => e
|
|
88
|
+
# finalize the request
|
|
89
|
+
controller.finished!
|
|
90
|
+
controller = nil
|
|
91
|
+
|
|
92
|
+
if Doozer::Configs.rack_env == :deployment
|
|
93
|
+
logger.error("RuntimeError: #{e.to_s}")
|
|
94
|
+
for line in e.backtrace
|
|
95
|
+
logger.error(" #{line}")
|
|
96
|
+
end
|
|
97
|
+
logger.error("Printing env variables:")
|
|
98
|
+
logger.error(env.inspect)
|
|
99
|
+
return [500, {"Content-Type" => "text/html"}, @@errors[500]]
|
|
100
|
+
else
|
|
101
|
+
raise e
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
else
|
|
105
|
+
return route.app.call(env)
|
|
106
|
+
end
|
|
107
|
+
else
|
|
108
|
+
return [404, {"Content-Type" => "text/html"}, @@errors[404]]
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def execution_time(name = nil, point = :start)
|
|
113
|
+
if Doozer::Configs.rack_env == :development
|
|
114
|
+
@execution_time_name = name if name
|
|
115
|
+
@execution_time_start = Time.now().to_f if point == :start
|
|
116
|
+
@execution_time_end = Time.now().to_f if point == :end
|
|
117
|
+
logger.info("Excecution Time: #{@execution_time_name}: #{("%0.2f" % ( (@execution_time_end - @execution_time_start) * 1000).to_f)}ms") if point == :end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Load all application files for app/helpers/*, app/views/layouts/*, app/views/* and app/controllers/*
|
|
122
|
+
def load_files
|
|
123
|
+
# load models
|
|
124
|
+
load_models
|
|
125
|
+
printf "Caching files...\n"
|
|
126
|
+
@@controllers = {}
|
|
127
|
+
@@layouts={}
|
|
128
|
+
@@views={}
|
|
129
|
+
@@errors={}
|
|
130
|
+
|
|
131
|
+
# require helper files and include into Doozer::Partial
|
|
132
|
+
helper_files = Dir.glob(File.join(app_path,'app/helpers/*_helper.rb'))
|
|
133
|
+
helper_files.each {|f|
|
|
134
|
+
require f
|
|
135
|
+
key = f.split("helpers/")[1].gsub(/.rb/,'')
|
|
136
|
+
Doozer::Partial.include_view_helper(key)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
# cache contoller classes
|
|
140
|
+
controller_files = Dir.glob(File.join(app_path,'app/controllers/*_controller.rb'))
|
|
141
|
+
# we need to load the application_controller first since this might not be the first in the list...
|
|
142
|
+
if controller_files.length > 0
|
|
143
|
+
i=0
|
|
144
|
+
for f in controller_files
|
|
145
|
+
break if i==0 and f.index('application_controller.rb')
|
|
146
|
+
if f.index('application_controller.rb')
|
|
147
|
+
controller_files.insert(0, controller_files.delete(f))
|
|
148
|
+
break
|
|
149
|
+
end
|
|
150
|
+
i+=1
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
controller_files.each { |f|
|
|
155
|
+
require f
|
|
156
|
+
key = f.split("controllers/")[1].split("_controller.rb")[0]
|
|
157
|
+
if key.index("_")
|
|
158
|
+
value = key.split('_').each{ | k | k.capitalize! }.join('')
|
|
159
|
+
else
|
|
160
|
+
value = key.capitalize
|
|
161
|
+
end
|
|
162
|
+
klass_name = "#{value}Controller"
|
|
163
|
+
@@controllers[key.to_sym] = klass_name
|
|
164
|
+
# p "cache controller: #{key.to_sym}"
|
|
165
|
+
|
|
166
|
+
# importing view helpers into controller
|
|
167
|
+
controller_klass = Object.const_get(klass_name)
|
|
168
|
+
# automatically ads the application helper to the class
|
|
169
|
+
controller_klass.include_view_helper('application_helper')
|
|
170
|
+
controller_klass.include_view_helpers
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
# cache layout erb's
|
|
174
|
+
layout_files = Dir.glob(File.join(app_path,'app/views/layouts/*.erb'))
|
|
175
|
+
layout_files.each {|f|
|
|
176
|
+
key = f.split("layouts/")[1].split(".html.erb")[0].gsub(/.xml.erb/, '_xml').gsub(/.json.erb/, '_json').gsub(/.js.erb/, '_js').gsub(/.rss.erb/, '_rss').gsub(/.atom.erb/, '_atom')
|
|
177
|
+
results = []
|
|
178
|
+
File.new(f, "r").each { |line| results << line }
|
|
179
|
+
@@layouts[key.to_sym] = ERB.new(results.join(""))
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
#lood 404 and 500 pages if they exist
|
|
183
|
+
pnf = Doozer::Configs.page_not_found_url
|
|
184
|
+
if pnf
|
|
185
|
+
file = File.join(app_path,"#{pnf}")
|
|
186
|
+
results = []
|
|
187
|
+
File.new(file, "r").each { |line| results << line }
|
|
188
|
+
@@errors[404] = results.join("")
|
|
189
|
+
else
|
|
190
|
+
@@errors[404] = "<html><body>Sorry, this page can't be found.</body></html>"
|
|
191
|
+
end
|
|
192
|
+
ise = Doozer::Configs.internal_server_error_url
|
|
193
|
+
if ise
|
|
194
|
+
file = File.join(app_path,"#{ise}")
|
|
195
|
+
results = []
|
|
196
|
+
File.new(file, "r").each { |line| results << line }
|
|
197
|
+
@@errors[500] = results.join("")
|
|
198
|
+
else
|
|
199
|
+
@@errors[500] = "<html><body>There was an internal server error which borked this request.</body></html>"
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
@@controllers.each_key { | key |
|
|
203
|
+
# p key.inspect
|
|
204
|
+
files = Dir.glob(File.join(app_path,"app/views/#{key.to_s}/*.erb"))
|
|
205
|
+
files.each { | f |
|
|
206
|
+
#!!!don't cache partials here!!!
|
|
207
|
+
view = f.split("#{key.to_s}/")[1].split(".erb")[0].gsub(/\./,'_')
|
|
208
|
+
# p "check view: #{view}"
|
|
209
|
+
if not /^_/.match( view )
|
|
210
|
+
# p "cache view: #{view}"
|
|
211
|
+
results = []
|
|
212
|
+
File.new(f, "r").each { |line| results << line }
|
|
213
|
+
@@views[key] = {} if @@views[key].nil?
|
|
214
|
+
@@views[key][view.to_sym] = ERB.new(results.join(""))
|
|
215
|
+
end
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
# Load application routes
|
|
221
|
+
def load_routes
|
|
222
|
+
require File.join(app_path, 'config/routes')
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
# Load all application models in app/models
|
|
226
|
+
def load_models
|
|
227
|
+
printf "Loading models...\n"
|
|
228
|
+
Dir.glob(File.join(app_path,'app/models/*.rb')).each { | model |
|
|
229
|
+
require model
|
|
230
|
+
}
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
# Loads the file watcher for all application files while in development mode-only.
|
|
234
|
+
#
|
|
235
|
+
# This allows you to edit files without restarting the app server to pickup new changes.
|
|
236
|
+
def load_watcher
|
|
237
|
+
require 'doozer/watcher'
|
|
238
|
+
|
|
239
|
+
printf "All along the watchtower...\n"
|
|
240
|
+
watcher = FileSystemWatcher.new()
|
|
241
|
+
|
|
242
|
+
# watcher.addDirectory(File.join(File.dirname(__FILE__),'../doozer/'), "*.rb")
|
|
243
|
+
watcher.addDirectory( app_path + '/app/', "**/*")
|
|
244
|
+
watcher.addDirectory( app_path + '/app', "**/**/*")
|
|
245
|
+
watcher.addDirectory( app_path + '/config/', "*.*")
|
|
246
|
+
watcher.addDirectory( app_path + '/lib/', "*.*")
|
|
247
|
+
watcher.addDirectory( app_path + '/static/', "*.*")
|
|
248
|
+
watcher.addDirectory( app_path + '/static/', "**/**/*")
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
watcher.sleepTime = 1
|
|
252
|
+
watcher.start { |status, file|
|
|
253
|
+
if(status == FileSystemWatcher::CREATED) then
|
|
254
|
+
puts "created: #{file}"
|
|
255
|
+
load_files
|
|
256
|
+
Doozer::Partial.clear_loaded_partials
|
|
257
|
+
elsif(status == FileSystemWatcher::MODIFIED) then
|
|
258
|
+
puts "modified: #{file}"
|
|
259
|
+
load_files
|
|
260
|
+
Doozer::Partial.clear_loaded_partials
|
|
261
|
+
elsif(status == FileSystemWatcher::DELETED) then
|
|
262
|
+
puts "deleted: #{file}"
|
|
263
|
+
load_files
|
|
264
|
+
Doozer::Partial.clear_loaded_partials
|
|
265
|
+
end
|
|
266
|
+
}
|
|
267
|
+
#don't join the thread it messes up rackup threading watcher.join()
|
|
268
|
+
# p watcher.isStarted?
|
|
269
|
+
# p watcher.isStopped?
|
|
270
|
+
# p watcher.foundFiles.inspect
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
def handler(key)
|
|
274
|
+
return Object.const_get(@@controllers[key])
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
def app_path
|
|
278
|
+
Doozer::Configs.app_path
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
def self.controllers
|
|
282
|
+
@@controllers
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
def self.layouts
|
|
286
|
+
@@layouts
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
def self.views
|
|
290
|
+
@@views
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
end #App
|
|
294
|
+
end #Doozer
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
require 'rbconfig'
|
|
2
|
+
require 'logger'
|
|
3
|
+
require 'yaml'
|
|
4
|
+
|
|
5
|
+
module Doozer
|
|
6
|
+
|
|
7
|
+
# This is the main Configs class which loads root/config/app.yml and root/config/database.yml
|
|
8
|
+
#
|
|
9
|
+
# It also provides a few helper methods like logger, app_path, base_url and app_name
|
|
10
|
+
class Configs
|
|
11
|
+
@@possible_orm = [:active_record, :data_mapper, :sequel]
|
|
12
|
+
@@app_path = nil
|
|
13
|
+
|
|
14
|
+
# Load all the config files for the application. Also instantiates a default application Logger.
|
|
15
|
+
def self.load(rack_env)
|
|
16
|
+
printf "Application path: #{app_path}\n"
|
|
17
|
+
printf "Loading configs for #{rack_env}\n"
|
|
18
|
+
|
|
19
|
+
@@config = Config::CONFIG
|
|
20
|
+
rack_env = (rack_env.kind_of? String) ? rack_env.to_sym : rack_env
|
|
21
|
+
case rack_env
|
|
22
|
+
when :development
|
|
23
|
+
when :deployment
|
|
24
|
+
when :test, :none
|
|
25
|
+
rack_env = :test
|
|
26
|
+
else
|
|
27
|
+
raise ":development, :deployment, or :test are only environments allowed"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# set logging for environment
|
|
31
|
+
if [:development, :test].include?(rack_env)
|
|
32
|
+
@@logger = Logger.new(STDOUT)
|
|
33
|
+
else
|
|
34
|
+
@@logger = Logger.new("#{app_path}/log/#{rack_env}.log")
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
@@config[:rack_env] = rack_env
|
|
38
|
+
# p ":rack_env set to #{@@config[:rack_env]}"
|
|
39
|
+
|
|
40
|
+
begin
|
|
41
|
+
@@config[:database] = Configs.symbolize_keys( YAML.load(File.read(File.join(app_path,'config/database.yml'))) )
|
|
42
|
+
rescue
|
|
43
|
+
printf "--Failed to load config/database.yml \n"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
begin
|
|
47
|
+
@@config[:app] = Configs.symbolize_keys( YAML.load(File.read(File.join(app_path,'config/app.yml'))) )
|
|
48
|
+
rescue
|
|
49
|
+
printf "--Failed to load config/app.yml\n"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# We initialize the application logger in this Configs. This is then extended through to the ActiveRecord and is also available in ViewHelpers.
|
|
55
|
+
def self.logger
|
|
56
|
+
@@logger
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Hook for setting the application path.
|
|
60
|
+
#
|
|
61
|
+
# This allows the an application to be initialized from a different location then the project directory.
|
|
62
|
+
def self.set_app_path(path=nil)
|
|
63
|
+
@@app_path = path || Dir.pwd
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# This is the file path the app was loaded under. Dir.pwd moves to root in daemon mode so we cache this.
|
|
67
|
+
def self.app_path
|
|
68
|
+
set_app_path if @@app_path.nil?
|
|
69
|
+
return @@app_path
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Take a hash and turn all the keys into symbols
|
|
73
|
+
def self.symbolize_keys(hash=nil)
|
|
74
|
+
out = {}; hash.each { | k, val | out[k.to_sym] = val}
|
|
75
|
+
return out
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Return the rack environment this application was loaded with.
|
|
79
|
+
def self.rack_env
|
|
80
|
+
return @@config[:rack_env] if not @@config[:rack_env].nil?
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Input a symbol and return the config for this sym
|
|
84
|
+
def self.get(sym=nil)
|
|
85
|
+
@@config[sym]
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Return the orm mapping gem name to load
|
|
89
|
+
def self.orm
|
|
90
|
+
begin
|
|
91
|
+
return @@config[:database][:orm]
|
|
92
|
+
rescue
|
|
93
|
+
end
|
|
94
|
+
return nil
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Return the database configuration setting for the loaded environment
|
|
98
|
+
def self.db
|
|
99
|
+
return @@config[:database][@@config[:rack_env]] if not @@config[:database].nil?
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def self.orm_loaded
|
|
103
|
+
@@orm_loaded || false
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def self.orm_loaded=(t)
|
|
107
|
+
@@orm_loaded = t
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Only used for Sequel ORM for getting the db connection after connecting
|
|
111
|
+
def self.db_conn
|
|
112
|
+
@@db_conn
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Only used for Sequel ORM to set the db connection
|
|
116
|
+
def self.db_conn=(conn)
|
|
117
|
+
@@db_conn = conn
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Return the app configuration setting for the loaded environment
|
|
121
|
+
def self.app
|
|
122
|
+
return @@config[:app][@@config[:rack_env]]
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Return the app base url
|
|
126
|
+
def self.base_url
|
|
127
|
+
self.app["base_url"] || ""
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# Return the app name
|
|
131
|
+
def self.app_name
|
|
132
|
+
self.app["name"] || ""
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# Return the app 404 url
|
|
136
|
+
def self.page_not_found_url
|
|
137
|
+
self.app[404] || nil
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# Return the app 404 url
|
|
141
|
+
def self.internal_server_error_url
|
|
142
|
+
self.app[500] || nil
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
end
|
|
146
|
+
end
|