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.
21
22
23
|
# File 'lib/rory/application.rb', line 21
def root
@root
end
|
Instance Attribute Details
#config_path ⇒ Object
Returns the value of attribute config_path.
17
18
19
|
# File 'lib/rory/application.rb', line 17
def config_path
@config_path
end
|
#db ⇒ Object
Returns the value of attribute db.
16
17
18
|
# File 'lib/rory/application.rb', line 16
def db
@db
end
|
#db_config ⇒ Object
Returns the value of attribute db_config.
16
17
18
|
# File 'lib/rory/application.rb', line 16
def db_config
@db_config
end
|
Class Method Details
.inherited(subclass) ⇒ Object
23
24
25
26
|
# File 'lib/rory/application.rb', line 23
def inherited(subclass)
super
Rory.application = subclass.instance
end
|
.instance ⇒ Object
37
38
39
|
# File 'lib/rory/application.rb', line 37
def instance
@instance ||= new
end
|
.method_missing(*args, &block) ⇒ Object
28
29
30
|
# File 'lib/rory/application.rb', line 28
def method_missing(*args, &block)
instance.send(*args, &block)
end
|
.respond_to?(method) ⇒ Boolean
32
33
34
35
|
# File 'lib/rory/application.rb', line 32
def respond_to?(method)
return true if instance.respond_to?(method)
super
end
|
Instance Method Details
#auto_require_paths ⇒ Object
46
47
48
|
# File 'lib/rory/application.rb', line 46
def auto_require_paths
@auto_require_paths ||= %w(models controllers helpers)
end
|
#call(env) ⇒ Object
156
157
158
|
# File 'lib/rory/application.rb', line 156
def call(env)
stack.call(env)
end
|
83
84
85
|
# File 'lib/rory/application.rb', line 83
def configure
yield self
end
|
#connect_db(environment = ENV['RORY_ENV']) ⇒ Object
97
98
99
100
101
|
# File 'lib/rory/application.rb', line 97
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
129
130
131
132
|
# File 'lib/rory/application.rb', line 129
def filter_parameters(*params)
reset_stack
@parameters_to_filter = params
end
|
#load_config_data(config_type) ⇒ Object
91
92
93
94
95
|
# File 'lib/rory/application.rb', line 91
def load_config_data(config_type)
YAML.load_file(
File.expand_path(File.join(config_path, "#{config_type}.yml"))
)
end
|
#log_file ⇒ Object
160
161
162
163
|
# File 'lib/rory/application.rb', line 160
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
68
69
70
|
# File 'lib/rory/application.rb', line 68
def log_path
@log_path ||= root_path.join('log')
end
|
#logger ⇒ Object
165
166
167
|
# File 'lib/rory/application.rb', line 165
def logger
@logger ||= Logger.new(log_file)
end
|
#middleware ⇒ Object
108
109
110
|
# File 'lib/rory/application.rb', line 108
def middleware
@middleware ||= []
end
|
#parameters_to_filter ⇒ Object
125
126
127
|
# File 'lib/rory/application.rb', line 125
def parameters_to_filter
@parameters_to_filter || [:password]
end
|
#request_logging_on? ⇒ Boolean
116
117
118
|
# File 'lib/rory/application.rb', line 116
def request_logging_on?
@request_logging != false
end
|
#require_all_files ⇒ Object
#reset_stack ⇒ Object
134
135
136
|
# File 'lib/rory/application.rb', line 134
def reset_stack
@stack = nil
end
|
#root ⇒ Object
56
57
58
|
# File 'lib/rory/application.rb', line 56
def root
self.class.root
end
|
#root_path ⇒ Object
60
61
62
|
# File 'lib/rory/application.rb', line 60
def root_path
root || raise(RootNotConfigured, "#{self.class.name} has no root configured")
end
|
#routes ⇒ Object
76
77
78
79
80
81
|
# File 'lib/rory/application.rb', line 76
def routes
unless @routes
load(File.join(config_path, 'routes.rb'))
end
@routes
end
|
#set_routes(&block) ⇒ Object
72
73
74
|
# File 'lib/rory/application.rb', line 72
def set_routes(&block)
@routes = RouteMapper.set_routes(&block)
end
|
#spin_up ⇒ Object
87
88
89
|
# File 'lib/rory/application.rb', line 87
def spin_up
connect_db
end
|
#stack ⇒ Object
146
147
148
149
150
151
152
153
154
|
# File 'lib/rory/application.rb', line 146
def stack
@stack ||= Rack::Builder.new.tap { |builder|
use_default_middleware
middleware.each do |args, block|
builder.use *args, &block
end
builder.run dispatcher
}
end
|
#turn_off_request_logging! ⇒ Object
120
121
122
123
|
# File 'lib/rory/application.rb', line 120
def turn_off_request_logging!
reset_stack
@request_logging = false
end
|
#use_default_middleware ⇒ Object
138
139
140
141
142
143
144
|
# File 'lib/rory/application.rb', line 138
def use_default_middleware
if request_logging_on?
use_middleware Rack::PostBodyContentTypeParser
use_middleware Rack::CommonLogger, logger
use_middleware Rory::RequestParameterLogger, logger, :filters => parameters_to_filter
end
end
|
#use_middleware(*args, &block) ⇒ Object
103
104
105
106
|
# File 'lib/rory/application.rb', line 103
def use_middleware(*args, &block)
@stack = nil
middleware << [args, block]
end
|