Skip to content
This repository has been archived by the owner on Sep 6, 2018. It is now read-only.

Commit

Permalink
configure connetion
Browse files Browse the repository at this point in the history
  • Loading branch information
javan committed May 14, 2011
1 parent 38e1545 commit 0ab9389
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
8 changes: 7 additions & 1 deletion lib/copy/server.rb
Expand Up @@ -31,12 +31,18 @@ def self.config(&block)
class_eval(&block)
end

before do
if settings.respond_to?(:storage) && !Copy::Storage.connected?
Copy::Storage.connect!(settings.storage)
end
end

get '/admin/?' do
"admin"
end

get '*' do
route = Copy::Router.new(params[:splat].to_s, settings.views)
route = Copy::Router.new(params[:splat].first, settings.views)
if route.success?
set_cache_control_header
content_type(route.format)
Expand Down
2 changes: 1 addition & 1 deletion lib/copy/storage.rb
Expand Up @@ -16,7 +16,7 @@ def self.connect!(connection_url)
end

def self.connected?
! @@storage.nil?
!defined?(@@storage).nil?
end

def self.get(name)
Expand Down
20 changes: 16 additions & 4 deletions test/server_test.rb
Expand Up @@ -5,7 +5,11 @@ class ServerTest < Test::Unit::TestCase
include Rack::Test::Methods

def app
Copy::Server.config do
Copy::Server
end

setup do
app.config do
set :views, File.dirname(File.expand_path(__FILE__)) + '/sample_app/views'
end
end
Expand Down Expand Up @@ -70,8 +74,16 @@ def app
assert_equal File.read(app.settings.views + '/data/people.xml.erb'), last_response.body
end

test "connects to storage when setting present" do
connection_url = 'redis://localhost:1234'
app.config { set :storage, connection_url }
Copy::Storage.expects(:connect!).with(connection_url).once.returns(true)
get '/'
assert last_response.ok?
end

test "copy helper displays content from storage" do
Copy::Storage.expects(:connected?).returns(true)
Copy::Storage.expects(:connected?).twice.returns(true)
Copy::Storage.expects(:get).with(:facts).returns("truth")

get 'with_copy_helper'
Expand All @@ -80,7 +92,7 @@ def app
end

test "copy helper shows default text when content is not in storage" do
Copy::Storage.expects(:connected?).returns(true)
Copy::Storage.expects(:connected?).twice.returns(true)
Copy::Storage.expects(:get).with(:facts).returns(nil)

get 'with_copy_helper'
Expand All @@ -89,7 +101,7 @@ def app
end

test "copy helper shows default text when not connected" do
Copy::Storage.expects(:connected?).returns(false)
Copy::Storage.expects(:connected?).twice.returns(false)

get 'with_copy_helper'
assert last_response.ok?
Expand Down

0 comments on commit 0ab9389

Please sign in to comment.