Class: Rory::Application
- Inherits:
-
Object
show all
- Defined in:
- lib/rory/application.rb
Overview
Main application superclass. Applications should subclass this class, but currently no additional configuration is needed - just run ‘#spin_up’ to connect the database so Sequel can do its magic.
Defined Under Namespace
Classes: RootNotConfigured
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Class Attribute Details
.root ⇒ Object
Returns the value of attribute root.
23
24
25
|
# File 'lib/rory/application.rb', line 23
def root
@root
end
|
Instance Attribute Details
#config_path ⇒ Object
Returns the value of attribute config_path.
19
20
21
|
# File 'lib/rory/application.rb', line 19
def config_path
@config_path
end
|
#db ⇒ Object
Returns the value of attribute db.
18
19
20
|
# File 'lib/rory/application.rb', line 18
def db
@db
end
|
#db_config ⇒ Object
Returns the value of attribute db_config.
18
19
20
|
# File 'lib/rory/application.rb', line 18
def db_config
@db_config
end
|
Class Method Details
.inherited(subclass) ⇒ Object
25
26
27
28
|
# File 'lib/rory/application.rb', line 25
def inherited(subclass)
super
Rory.application = subclass.instance
end
|
.initializer_default_middleware ⇒ Object
52
53
54
55
56
|
# File 'lib/rory/application.rb', line 52
def initializer_default_middleware
Rory::Application.initializers.add "rory.request_middleware" do |app|
app.request_middleware
end
end
|
35
36
37
|
# File 'lib/rory/application.rb', line 35
def initializers
@initializers ||= Initializers.new
end
|
.instance ⇒ Object
44
45
46
|
# File 'lib/rory/application.rb', line 44
def instance
@instance ||= new
end
|
.method_missing(*args, &block) ⇒ Object
30
31
32
|
# File 'lib/rory/application.rb', line 30
def method_missing(*args, &block)
instance.send(*args, &block)
end
|
.respond_to?(method, private = false) ⇒ Boolean
39
40
41
42
|
# File 'lib/rory/application.rb', line 39
def respond_to?(method, private=false)
return true if instance.respond_to?(method)
super
end
|
Instance Method Details
#auto_require_paths ⇒ Object
61
62
63
|
# File 'lib/rory/application.rb', line 61
def auto_require_paths
@auto_require_paths ||= %w(config/initializers models controllers helpers)
end
|
#call(env) ⇒ Object
178
179
180
|
# File 'lib/rory/application.rb', line 178
def call(env)
stack.call(env)
end
|
98
99
100
|
# File 'lib/rory/application.rb', line 98
def configure
yield self
end
|
#connect_db(environment = ENV['RORY_ENV']) ⇒ Object
112
113
114
115
116
|
# File 'lib/rory/application.rb', line 112
def connect_db(environment = ENV['RORY_ENV'])
@db_config = load_config_data(:database)
@db = Sequel.connect(@db_config[environment.to_s])
@db.loggers << logger
end
|
#filter_parameters(*params) ⇒ Object
143
144
145
146
|
# File 'lib/rory/application.rb', line 143
def filter_parameters(*params)
reset_stack
@parameters_to_filter = params
end
|
#load_config_data(config_type) ⇒ Object
106
107
108
109
110
|
# File 'lib/rory/application.rb', line 106
def load_config_data(config_type)
YAML.load_file(
File.expand_path(File.join(config_path, "#{config_type}.yml"))
)
end
|
#log_file ⇒ Object
182
183
184
185
|
# File 'lib/rory/application.rb', line 182
def log_file
Dir.mkdir(log_path) unless File.exists?(log_path)
File.open(log_path.join("#{ENV['RORY_ENV']}.log"), 'a').tap { |file| file.sync = true }
end
|
#log_path ⇒ Object
83
84
85
|
# File 'lib/rory/application.rb', line 83
def log_path
@log_path ||= root_path.join('log')
end
|
#logger ⇒ Object
187
188
189
|
# File 'lib/rory/application.rb', line 187
def logger
@logger ||= Logger.new(log_file)
end
|
#middleware ⇒ Object
122
123
124
|
# File 'lib/rory/application.rb', line 122
def middleware
@middleware ||= MiddlewareStack.new
end
|
#parameters_to_filter ⇒ Object
139
140
141
|
# File 'lib/rory/application.rb', line 139
def parameters_to_filter
@parameters_to_filter || [:password]
end
|
#request_logging_on? ⇒ Boolean
130
131
132
|
# File 'lib/rory/application.rb', line 130
def request_logging_on?
@request_logging != false
end
|
#request_middleware ⇒ Object
156
157
158
159
160
161
162
|
# File 'lib/rory/application.rb', line 156
def request_middleware
return unless request_logging_on?
use_middleware Rory::RequestId, :uuid_prefix => uuid_prefix
use_middleware Rack::PostBodyContentTypeParser
use_middleware Rack::CommonLogger, logger
use_middleware Rory::RequestParameterLogger, logger, :filters => parameters_to_filter
end
|
#require_all_files ⇒ Object
#reset_stack ⇒ Object
148
149
150
|
# File 'lib/rory/application.rb', line 148
def reset_stack
@stack = nil
end
|
#root ⇒ Object
71
72
73
|
# File 'lib/rory/application.rb', line 71
def root
self.class.root
end
|
#root_path ⇒ Object
75
76
77
|
# File 'lib/rory/application.rb', line 75
def root_path
root || raise(RootNotConfigured, "#{self.class.name} has no root configured")
end
|
#routes ⇒ Object
91
92
93
94
95
96
|
# File 'lib/rory/application.rb', line 91
def routes
unless @routes
load(File.join(config_path, 'routes.rb'))
end
@routes
end
|
#run_initializers ⇒ Object
#set_routes(&block) ⇒ Object
87
88
89
|
# File 'lib/rory/application.rb', line 87
def set_routes(&block)
@routes = RouteMapper.set_routes(&block)
end
|
#spin_up ⇒ Object
102
103
104
|
# File 'lib/rory/application.rb', line 102
def spin_up
connect_db
end
|
#stack ⇒ Object
168
169
170
171
172
173
174
175
176
|
# File 'lib/rory/application.rb', line 168
def stack
@stack ||= Rack::Builder.new.tap { |builder|
run_initializers
middleware.each do |m|
builder.use m.klass, *m.args, &m.block
end
builder.run dispatcher
}
end
|
#turn_off_request_logging! ⇒ Object
134
135
136
137
|
# File 'lib/rory/application.rb', line 134
def turn_off_request_logging!
reset_stack
@request_logging = false
end
|
#use_middleware(*args, &block) ⇒ Object
118
119
120
|
# File 'lib/rory/application.rb', line 118
def use_middleware(*args, &block)
middleware.use *args, &block
end
|
#uuid_prefix ⇒ Object
152
153
154
|
# File 'lib/rory/application.rb', line 152
def uuid_prefix
Support.tokenize(self.class.name.gsub("::Application", ""))
end
|