Skip to content

Commit

Permalink
Updated the boot process to use the new configuration class and remov…
Browse files Browse the repository at this point in the history
…ed old configuration directives. Updated specs to reflect internal changes. Cleaned up default configurations. [#38 state:resolved]
  • Loading branch information
mtodd committed Jun 18, 2008
1 parent 8706ed7 commit a39f1e5
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 246 deletions.
20 changes: 14 additions & 6 deletions lib/halcyon.rb
Expand Up @@ -25,23 +25,31 @@ module Halcyon
autoload :Logging, 'halcyon/logging'
autoload :Runner, 'halcyon/runner'

include Halcyon::Config::Helpers
include Config::Helpers

class << self

attr_accessor :logger
attr_accessor :config
attr_writer :config

def version
VERSION.join('.')
end

# The root directory of the current application.
#
# Returns String:root_directory
# The default <tt>root</tt> setting, overwritten by a configuration helper.
# This is so that the paths can be loaded when first booting the app.
# This won't be necessary for certain cases where the paths the root is
# manually configured, but for all other cases it can cause problems.
#
def root
self.config[:root] || Dir.pwd rescue Dir.pwd
Dir.pwd
end

# Configuration accessor which creates a configuration object when
# necessary.
#
def config
@config ||= Halcyon::Config.new
end

# Tests for Windows platform (to compensate for numerous Windows-specific
Expand Down
88 changes: 44 additions & 44 deletions lib/halcyon/application.rb
Expand Up @@ -7,21 +7,13 @@ module Halcyon
# Manages shutting down and starting up hooks, routing, dispatching, etc.
# Also restricts the requests to acceptable clients, defaulting to all.
#
class Application
include Exceptions
class Application

autoload :Hooks, 'halcyon/application/hooks'
autoload :Router, 'halcyon/application/router'

attr_accessor :session

DEFAULT_OPTIONS = {
:root => Dir.pwd,
:logging => {
:type => 'Logger',
:level => 'info'
},
:allow_from => :all
}.to_mash
include Exceptions
include Hooks

# Initializes the app:
# * runs startup hooks
Expand All @@ -30,7 +22,7 @@ class Application
def initialize
self.logger.info "Starting up..."

self.hooks[:startup].call(Halcyon.config) if self.hooks[:startup]
Halcyon.hooks[:startup].each {|hook| hook.call(Halcyon.config) }

# clean after ourselves and get prepared to start serving things
self.logger.debug "Starting GC."
Expand All @@ -40,7 +32,7 @@ def initialize

at_exit do
self.logger.info "Shutting down #{$$}."
self.hooks[:shutdown].call(Halcyon.config) if self.hooks[:shutdown]
Halcyon.hooks[:shutdown].each {|hook| hook.call(Halcyon.config) }
self.logger.info "Done."
end
end
Expand Down Expand Up @@ -199,22 +191,8 @@ def filter_params_for_log(request, env)
request.params.merge(env['halcyon.route'])
end

# See the documentation for generated apps in <tt>config/initialze/hooks.rb</tt>
#
def hooks
self.class.hooks
end

class << self

attr_accessor :hooks

# See the documentation for generated apps in <tt>config/initialze/hooks.rb</tt>
#
def hooks
@hooks ||= {}
end

# Defines routes for the application.
#
# Refer to Halcyon::Application::Router for documentation and resources.
Expand All @@ -227,23 +205,45 @@ def route
end
end

# Sets the startup hook to the proc.
#
# Use this to initialize application-wide resources, such as database
# connections.
#
# Use initializers where possible.
#
def startup &hook
self.hooks[:startup] = hook
end

# Sets the shutdown hook to the proc.
# Runs through the bootup process. This involves:
# * establishing configuration directives
# * loading required libraries
#
# Close any resources opened in the +startup+ hook.
#
def shutdown &hook
self.hooks[:shutdown] = hook
def boot
Halcyon.config ||= Halcyon::Config.new

# Set application name
Halcyon.app = Halcyon.config[:app] || Halcyon.root.split('/').last.camel_case

# Setup logger
if Halcyon.config[:logger]
Halcyon.config[:logging] = (Halcyon.config[:logging] || Halcyon::Config.defaults[:logging]).merge({
:type => Halcyon.config[:logger].class.to_s,
:logger => Halcyon.config[:logger]
})
end
Halcyon::Logging.set(Halcyon.config[:logging][:type])
Halcyon.logger = Halcyon::Logger.setup(Halcyon.config[:logging])

# Run initializers
Dir.glob(%(requires hooks routes *).map{|init|Halcyon.paths[:init]/init+'.rb'}).each do |initializer|
self.logger.debug "Init: #{File.basename(initializer).chomp('.rb').camel_case}" if
require initializer.chomp('.rb')
end

# Setup autoloads for Controllers found in Halcyon.root/'app' (by default)
Dir.glob([Halcyon.paths[:controller]/'application.rb', Halcyon.paths[:controller]/'*.rb']).each do |controller|
self.logger.debug "Load: #{File.basename(controller).chomp('.rb').camel_case} Controller" if
require controller.chomp('.rb')
end

# Setup autoloads for Models found in Halcyon.root/'app'/'models' (by default)
Dir.glob(Halcyon.paths[:model]/'*.rb').each do |model|
self.logger.debug "Load: #{File.basename(model).chomp('.rb').camel_case} Model" if
require model.chomp('.rb')
end

yield if block_given?
end

end
Expand Down
38 changes: 38 additions & 0 deletions lib/halcyon/application/hooks.rb
@@ -0,0 +1,38 @@
module Halcyon
class Application

# Helper that provides access to setting and running hooks.
#
module Hooks

# Extends the target with the class methods necessary.
def self.included(target)
target.extend(ClassMethods)
end

module ClassMethods

# Sets the startup hook to the proc.
#
# Use this to initialize application-wide resources, such as database
# connections.
#
# Use initializers where possible.
#
def startup &hook
Halcyon.hooks[:startup] << hook
end

# Sets the shutdown hook to the proc.
#
# Close any resources opened in the +startup+ hook.
#
def shutdown &hook
Halcyon.hooks[:shutdown] << hook
end

end

end
end
end
96 changes: 54 additions & 42 deletions lib/halcyon/config.rb
Expand Up @@ -169,50 +169,10 @@ def to_hash
self.config.to_hash
end

# Default configuration values.
#
# Defaults to the configuration for <tt>:development</tt>.
# Shortcut for Halcyon::Config.defaults.
#
def defaults(env = nil)
base = {
:allow_from => 'all',
:logging => {
:type => 'Logger',
:level => 'debug'
},
:root => Dir.pwd,
:app => nil,
:environment => :development,
:paths => Paths.new
}
case (env || :development)
when :development
base.merge({
:environment => :development
})
when :test
base.merge({
:environment => :test,
:logging => {
:type => 'Logger',
:level => 'warn',
:file => 'log/test.log'
}
})
when :console
base.merge({
:environment => :console
})
when :production
base.merge({
:environment => :production,
:logging => {
:type => 'Logger',
:level => 'warn',
:file => 'log/production.log'
}
})
end
Halcyon::Config.defaults(env)
end

def inspect
Expand All @@ -221,6 +181,58 @@ def inspect
"#<Halcyon::Config#{attrs}>"
end

class << self

# Default configuration values.
#
# Defaults to the configuration for <tt>:development</tt>.
#
def defaults(env = nil)
base = {
:app => nil,
:root => Dir.pwd,
:environment => :development,
:allow_from => 'all',
:logging => {
:type => 'Logger',
:level => 'debug'
},
:paths => Paths.new,
:hooks => Hash.new([])
}
case (env || :development)
when :development
base.merge({
:environment => :development
})
when :test
base.merge({
:app => 'Specs',
:environment => :test,
:logging => {
:type => 'Logger',
:level => 'warn',
:file => 'log/test.log'
}
})
when :console
base.merge({
:environment => :console
})
when :production
base.merge({
:environment => :production,
:logging => {
:type => 'Logger',
:level => 'warn',
:file => 'log/production.log'
}
})
end
end

end

end

end
6 changes: 6 additions & 0 deletions lib/halcyon/config/helpers.rb
Expand Up @@ -82,6 +82,12 @@ def paths
self.config[:paths]
end

# Provides a proxy to the hooks hash.
#
def hooks
self.config[:hooks]
end

end

# Provides dynamic creation of configuration attribute accessors.
Expand Down

0 comments on commit a39f1e5

Please sign in to comment.