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.
22
23
24
|
# File 'lib/rory/application.rb', line 22
def root
@root
end
|
Instance Attribute Details
#config_path ⇒ Object
Returns the value of attribute config_path.
18
19
20
|
# File 'lib/rory/application.rb', line 18
def config_path
@config_path
end
|
#db ⇒ Object
Returns the value of attribute db.
17
18
19
|
# File 'lib/rory/application.rb', line 17
def db
@db
end
|
#db_config ⇒ Object
Returns the value of attribute db_config.
17
18
19
|
# File 'lib/rory/application.rb', line 17
def db_config
@db_config
end
|
Class Method Details
.inherited(subclass) ⇒ Object
24
25
26
27
|
# File 'lib/rory/application.rb', line 24
def inherited(subclass)
super
Rory.application = subclass.instance
end
|
.initializer_default_middleware ⇒ Object
51
52
53
54
55
|
# File 'lib/rory/application.rb', line 51
def initializer_default_middleware
Rory::Application.initializers.add "rory.request_middleware" do |app|
app.request_middleware
end
end
|
34
35
36
|
# File 'lib/rory/application.rb', line 34
def initializers
@initializers ||= Initializers.new
end
|
.instance ⇒ Object
43
44
45
|
# File 'lib/rory/application.rb', line 43
def instance
@instance ||= new
end
|
.method_missing(*args, &block) ⇒ Object
29
30
31
|
# File 'lib/rory/application.rb', line 29
def method_missing(*args, &block)
instance.send(*args, &block)
end
|
.respond_to?(method, private = false) ⇒ Boolean
38
39
40
41
|
# File 'lib/rory/application.rb', line 38
def respond_to?(method, private=false)
return true if instance.respond_to?(method)
super
end
|
Instance Method Details
#auto_require_paths ⇒ Object
60
61
62
|
# File 'lib/rory/application.rb', line 60
def auto_require_paths
@auto_require_paths ||= %w(config/initializers models controllers helpers)
end
|
#call(env) ⇒ Object
177
178
179
|
# File 'lib/rory/application.rb', line 177
def call(env)
stack.call(env)
end
|
97
98
99
|
# File 'lib/rory/application.rb', line 97
def configure
yield self
end
|
#connect_db(environment = ENV['RORY_ENV']) ⇒ Object
111
112
113
114
115
|
# File 'lib/rory/application.rb', line 111
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
142
143
144
145
|
# File 'lib/rory/application.rb', line 142
def filter_parameters(*params)
reset_stack
@parameters_to_filter = params
end
|
#load_config_data(config_type) ⇒ Object
105
106
107
108
109
|
# File 'lib/rory/application.rb', line 105
def load_config_data(config_type)
YAML.load_file(
File.expand_path(File.join(config_path, "#{config_type}.yml"))
)
end
|
#log_file ⇒ Object
181
182
183
184
|
# File 'lib/rory/application.rb', line 181
def log_file
Dir.mkdir(log_path) unless File.exist?(log_path)
File.open(log_path.join("#{ENV['RORY_ENV']}.log"), 'a').tap { |file| file.sync = true }
end
|
#log_path ⇒ Object
82
83
84
|
# File 'lib/rory/application.rb', line 82
def log_path
@log_path ||= root_path.join('log')
end
|
#logger ⇒ Object
186
187
188
|
# File 'lib/rory/application.rb', line 186
def logger
@logger ||= Logger.new(log_file)
end
|
#middleware ⇒ Object
121
122
123
|
# File 'lib/rory/application.rb', line 121
def middleware
@middleware ||= MiddlewareStack.new
end
|
#parameters_to_filter ⇒ Object
138
139
140
|
# File 'lib/rory/application.rb', line 138
def parameters_to_filter
@parameters_to_filter || [:password]
end
|
#request_logging_on? ⇒ Boolean
129
130
131
|
# File 'lib/rory/application.rb', line 129
def request_logging_on?
@request_logging != false
end
|
#request_middleware ⇒ Object
155
156
157
158
159
160
161
|
# File 'lib/rory/application.rb', line 155
def request_middleware
return unless request_logging_on?
use_middleware Rory::RequestId, :uuid_prefix => uuid_prefix
use_middleware Rack::JSONBodyParser
use_middleware Rack::CommonLogger, logger
use_middleware Rory::RequestParameterLogger, logger, :filters => parameters_to_filter
end
|
#require_all_files ⇒ Object
#reset_stack ⇒ Object
147
148
149
|
# File 'lib/rory/application.rb', line 147
def reset_stack
@stack = nil
end
|
#root ⇒ Object
70
71
72
|
# File 'lib/rory/application.rb', line 70
def root
self.class.root
end
|
#root_path ⇒ Object
74
75
76
|
# File 'lib/rory/application.rb', line 74
def root_path
root || raise(RootNotConfigured, "#{self.class.name} has no root configured")
end
|
#routes ⇒ Object
90
91
92
93
94
95
|
# File 'lib/rory/application.rb', line 90
def routes
unless @routes
load(File.join(config_path, 'routes.rb'))
end
@routes
end
|
#run_initializers ⇒ Object
#set_routes(&block) ⇒ Object
86
87
88
|
# File 'lib/rory/application.rb', line 86
def set_routes(&block)
@routes = RouteMapper.set_routes(&block)
end
|
#spin_up ⇒ Object
101
102
103
|
# File 'lib/rory/application.rb', line 101
def spin_up
connect_db
end
|
#stack ⇒ Object
167
168
169
170
171
172
173
174
175
|
# File 'lib/rory/application.rb', line 167
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
133
134
135
136
|
# File 'lib/rory/application.rb', line 133
def turn_off_request_logging!
reset_stack
@request_logging = false
end
|
#use_middleware(*args, &block) ⇒ Object
117
118
119
|
# File 'lib/rory/application.rb', line 117
def use_middleware(*args, &block)
middleware.use *args, &block
end
|
#uuid_prefix ⇒ Object
151
152
153
|
# File 'lib/rory/application.rb', line 151
def uuid_prefix
Support.tokenize(self.class.name.gsub("::Application", ""))
end
|