Skip to content

Commit

Permalink
Added some specs for the Halcyon base class functionality and impleme…
Browse files Browse the repository at this point in the history
…nted configurable load paths. [#33 state:resolved]
  • Loading branch information
mtodd committed May 7, 2008
1 parent d557d43 commit f2ed44c
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 24 deletions.
2 changes: 2 additions & 0 deletions lib/halcyon.rb
Expand Up @@ -9,6 +9,7 @@
# Halcyon.app #=> AppName
# Halcyon.root #=> Dir.pwd
# Halcyon.config #=> {:allow_from => :all, :logging => {...}, ...}
# Halcyon.paths #=> {:config => Halcyon.root/'config', ...}
# Halcyon.logger #=> #<Logger>
# Halcyon.version #=> "0.5.0"
module Halcyon
Expand All @@ -27,6 +28,7 @@ class << self
attr_accessor :app
attr_accessor :logger
attr_accessor :config
attr_accessor :paths

def version
VERSION.join('.')
Expand Down
17 changes: 14 additions & 3 deletions lib/halcyon/runner.rb
Expand Up @@ -35,13 +35,24 @@ def run!(argv=ARGV)
# +file+ the name of the config file path (without the <tt>.yml</tt>
# extension)
def config_path(file = "config")
Halcyon.root/'config'/"#{file}.yml"
Halcyon.paths[:config]/"#{file}.yml"
end

end

# Initializes the application and application resources.
def initialize
# Set the default application paths, not overwriting manually set paths
Halcyon.paths = {
:controller => Halcyon.root/'app',
:model => Halcyon.root/'app'/'models',
:lib => Halcyon.root/'lib',
:config => Halcyon.root/'config',
:init => Halcyon.root/'config'/'{init,initialize}',
:log => Halcyon.root/'log'
}.to_mash.merge(Halcyon.paths || {})

# Load the configuration if none is set already
if Halcyon.config.nil?
if File.exist?(Halcyon::Runner.config_path)
Halcyon.config = Halcyon::Runner.load_config
Expand All @@ -64,13 +75,13 @@ def initialize
Halcyon.logger = Halcyon::Logger.setup(Halcyon.config[:logging])

# Run initializers
Dir.glob(Halcyon.root/'config'/'initialize'/'{requires.rb,hooks.rb,routes.rb,*}').each do |initializer|
Dir.glob(Halcyon.paths[:init]/'{requires.rb,hooks.rb,routes.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'
Dir.glob(Halcyon.root/'app'/'{application.rb,*}').each do |controller|
Dir.glob(Halcyon.paths[:controller]/'{application.rb,*}').each do |controller|
self.logger.debug "Load: #{File.basename(controller).chomp('.rb').camel_case} Controller" if
require controller.chomp('.rb')
end
Expand Down
4 changes: 0 additions & 4 deletions spec/halcyon/application_spec.rb
Expand Up @@ -55,10 +55,6 @@
Halcyon.config[:allow_from].should == :all
end

it "should record the correct environment details" do
Halcyon.root.should == Dir.pwd
end

it "should handle exceptions gracefully" do
body = JSON.parse(Rack::MockRequest.new(@app).get("/specs/cause_exception").body)
body['status'].should == 500
Expand Down
2 changes: 1 addition & 1 deletion spec/halcyon/client_spec.rb
Expand Up @@ -5,7 +5,7 @@

fork do
dir = Halcyon.root/'support'/'generators'/'halcyon'/'templates'
command = "thin start -r runner.ru -p 89981 -c #{dir} > /dev/null 2>&1"
command = "thin start -R runner.ru -p 89981 -c #{dir} > /dev/null 2>&1"
STDOUT.close
STDERR.close
exec command
Expand Down
42 changes: 42 additions & 0 deletions spec/halcyon/halcyon_spec.rb
@@ -0,0 +1,42 @@
describe "Halcyon" do

before do
@log = ''
@logger = Logger.new(StringIO.new(@log))
@config = $config.dup
@config[:logger] = @logger
@config[:app] = 'Specs'
Halcyon.config = @config
@app = Halcyon::Runner.new
end

it "should provide the path of the application root directory" do
Halcyon.root.should == Dir.pwd
end

it "should provide quick access to the configuration hash" do
Halcyon.config.is_a?(Hash).should.be.true?
end

it "should provide universal access to a logger" do
# We assume Logger here because, you know, we're gods of the test
Halcyon.logger.is_a?(Logger).should.be.true?
# And this is just a side affect of making the logger universally accessible
{}.logger.is_a?(Logger).should.be.true?
end

it "should provide the (estimated) application name" do
# We set this above
Halcyon.app.should == "Specs"
end

it "should provide sane default paths for essential components" do
Halcyon.paths.is_a?(Hash).should.be.true?
Halcyon.paths[:controller].should == Halcyon.root/"app"
Halcyon.paths[:lib].should == Halcyon.root/"lib"
Halcyon.paths[:config].should == Halcyon.root/"config"
Halcyon.paths[:init].should == Halcyon.root/"config"/"{init,initialize}"
Halcyon.paths[:log].should == Halcyon.root/"log"
end

end
35 changes: 21 additions & 14 deletions spec/spec_helper.rb
Expand Up @@ -12,9 +12,12 @@
}
}

# Default Application
# Testing Application

# Default controller
class Application < Halcyon::Controller; end

# Weird edge-case controller
class Specs < Application

def greeter
Expand Down Expand Up @@ -43,6 +46,7 @@ def undispatchable_private_method

end

# Resources controller
class Resources < Application

def index
Expand All @@ -54,22 +58,25 @@ def show
end
end

# Models

class Model
attr_accessor :id
end

class Halcyon::Application
route do |r|
r.resources :resources
# Testing routes

r.match('/hello/:name').to(:controller => 'specs', :action => 'greeter')
r.match('/:action').to(:controller => 'specs')
r.match('/:controller/:action').to()
r.match('/').to(:controller => 'specs', :action => 'index', :arbitrary => 'random')
# r.default_routes
{:action => 'missing'}
end
startup do |config|
$started = true
end
Halcyon::Application.route do |r|
r.resources :resources

r.match('/hello/:name').to(:controller => 'specs', :action => 'greeter')
r.match('/:action').to(:controller => 'specs')
r.match('/:controller/:action').to()
r.match('/').to(:controller => 'specs', :action => 'index', :arbitrary => 'random')
# r.default_routes
{:action => 'missing'}
end

Halcyon::Application.startup do |config|
$started = true
end
Expand Up @@ -26,7 +26,7 @@
# Ideal for establishing connections to resources like databases.
# Establish configuration options in initializer files, though.
Halcyon::Application.startup do |config|
logger.info 'Define startup tasks in config/initialize/hooks.rb'
logger.info 'Define startup tasks in config/init/hooks.rb'
end

# = Shutdown
Expand All @@ -35,5 +35,5 @@
#
# Ideal for closing connections to resources.
Halcyon::Application.shutdown do |config|
logger.info 'Define shutdown tasks in config/initialize/hooks.rb'
logger.info 'Define shutdown tasks in config/init/hooks.rb'
end

0 comments on commit f2ed44c

Please sign in to comment.