Skip to content

Commit

Permalink
added Config::Db to store db configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
mlomnicki committed Aug 7, 2009
1 parent 353c7ae commit 2db04dd
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
6 changes: 6 additions & 0 deletions dbconfig.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
host: localhost
user: username
password: secret
driver: pg
database: test

15 changes: 15 additions & 0 deletions lib/postpolicy/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ def self.load( config_hash )
accesses.each_pair do |action, acl|
AccessManager << Access.new( acls[acl], actions[action] )
end
Db.load( config_hash["database"] )
end

class Db
class << self
attr_reader :dbconfig, :dbi_params

def load( filename )
if filename
@dbconfig = YAML.load_file( filename ).stringify_keys!.freeze
@dbi_params = ["DBI:#{@dbconfig[:driver]}:#{@dbconfig[:database]}", @dbconfig[:user], @dbconfig[:password]]
end
end

end
end

protected
Expand Down
5 changes: 4 additions & 1 deletion postpolicy.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
acl:
foo:
sender: [email protected]
bar:
sender: file:///home/michal/test/filetest
recipient: sql://"SELECT * FROM users WHERE login = '%l'"

action:
pass: REJECT

access:
pass: foo

database: "dbconfig.yml"
25 changes: 25 additions & 0 deletions test/config_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'test_helper'

describe PostPolicy::Config::Db do

it "should load config from file and build dbi_params" do
filename = '/tmp/dbconfig'
config = { :host => 'localhost',
:database => 'postpolicy',
:user => 'foo',
:password => 'secret',
:driver => 'pg' }

File.open( filename, 'w' ) do |f|
f.puts config.to_yaml
end

PostPolicy::Config::Db.load( filename )
PostPolicy::Config::Db.dbconfig.should == config
dbi_params = ["DBI:#{config[:driver]}:#{config[:database]}", config[:user], config[:password]]
PostPolicy::Config::Db.dbi_params.should == dbi_params

FileUtils.rm( filename )
end

end
6 changes: 4 additions & 2 deletions test/datasource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@
describe PostPolicy::DataSource::File do

it "should read from file" do
File.open( '/tmp/file_datasource', 'w' ) do |f|
filename = '/tmp/file_datasource'
File.open( filename, 'w' ) do |f|
GOOD_VALUES.each { |value| f.puts value }
end
ds = PostPolicy::DataSource::File.new( '/tmp/file_datasource' )
ds = PostPolicy::DataSource::File.new( filename )
GOOD_VALUES.each do |value|
ds.exists?( value ).should == true
end
BAD_VALUES.each do |value|
ds.exists?( value ).should == false
end
FileUtils.rm( filename )
end

end
Expand Down

0 comments on commit 2db04dd

Please sign in to comment.