app_config 2.0.1 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +4 -2
- data/README.md +108 -38
- data/app_config.gemspec +2 -1
- data/lib/app_config.rb +2 -0
- data/lib/app_config/storage.rb +4 -6
- data/lib/app_config/storage/mongo.rb +18 -13
- data/lib/app_config/storage/postgres.rb +76 -0
- data/lib/app_config/storage/yaml.rb +3 -0
- data/lib/app_config/version.rb +1 -1
- data/spec/app_config/storage/mongo_spec.rb +12 -4
- data/spec/app_config/storage/postgres_spec.rb +41 -0
- data/spec/app_config/storage/yaml_spec.rb +10 -1
- data/spec/app_config_spec.rb +1 -10
- data/spec/spec_helper.rb +55 -5
- metadata +23 -10
- data/spec/fixtures/app_config_mongo.yml +0 -5
- data/spec/fixtures/env_app_config.yml +0 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 85532501e786eca705ac6224a529a1f1ea596b1f
|
4
|
+
data.tar.gz: cf1e1d838361c7bca2135db8274a52c2f9cef843
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7ef79b6d4670050d2d583599d54889c2e219e71e7a8833b39f22a062a842fa919eaa06a8d74f4642548f3db3b8797cd016d66658a6ce31c0970b501ea506c9a2
|
7
|
+
data.tar.gz: e39497d9599c666bc615ff3ab3b669c977ce5b141cf6dff6506d54cbe50e7a3b21170a7776ace29ed5bd2439a340c10cecbdd6e3bcc2f6955dec5c6aa000fc5b
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
app_config (2.0
|
4
|
+
app_config (2.1.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -12,6 +12,7 @@ GEM
|
|
12
12
|
mongo (1.3.1)
|
13
13
|
bson (>= 1.3.1)
|
14
14
|
multi_json (1.3.6)
|
15
|
+
pg (0.15.1)
|
15
16
|
rake (0.9.2.2)
|
16
17
|
redcarpet (2.3.0)
|
17
18
|
rspec (2.10.0)
|
@@ -35,8 +36,9 @@ DEPENDENCIES
|
|
35
36
|
app_config!
|
36
37
|
bson_ext
|
37
38
|
mongo
|
39
|
+
pg
|
38
40
|
rake
|
39
41
|
redcarpet
|
40
|
-
rspec
|
42
|
+
rspec
|
41
43
|
simplecov
|
42
44
|
yard
|
data/README.md
CHANGED
@@ -6,74 +6,144 @@ An easy to use, customizable library to easily store and retrieve application
|
|
6
6
|
AppConfig requires at least Ruby 1.9.
|
7
7
|
|
8
8
|
|
9
|
-
## Deprecation Note
|
10
|
-
|
11
|
-
Version `2.0` is **not** backwards compatible with the `1.x` branch.
|
12
|
-
|
13
|
-
See the [wiki](https://github.com/Oshuma/app_config/wiki) for upgrade instructions.
|
14
|
-
|
15
|
-
|
16
9
|
## Usage
|
17
10
|
|
18
|
-
Usage is simple. Just pass either a hash of options, or a block, to
|
11
|
+
Usage is simple. Just pass either a hash of options, or a block, to `AppConfig.setup!`.
|
19
12
|
|
20
13
|
In it's simplest form, you can use it like so:
|
21
14
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
15
|
+
```ruby
|
16
|
+
AppConfig.setup!(admin_email: 'admin@example.com')
|
17
|
+
# ..or..
|
18
|
+
AppConfig.setup! do |config|
|
19
|
+
config.admin_email = 'admin@example.com'
|
20
|
+
end
|
27
21
|
|
28
|
-
|
22
|
+
AppConfig.admin_email # => 'admin@example.com'
|
23
|
+
```
|
29
24
|
|
30
25
|
You may also specify the storage method along with options specific to that storage method.
|
31
26
|
Check the [wiki](https://github.com/Oshuma/app_config/wiki) for more usage examples.
|
32
27
|
|
33
28
|
|
34
|
-
##
|
29
|
+
## YAML
|
35
30
|
|
36
31
|
Given this YAML file:
|
37
32
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
33
|
+
```yaml
|
34
|
+
---
|
35
|
+
admin_email: 'admin@example.com'
|
36
|
+
api_name: 'Supr Webz 2.0'
|
37
|
+
api_key: 'SUPERAWESOMESERVICE'
|
38
|
+
```
|
42
39
|
|
43
40
|
Use it like so:
|
44
41
|
|
45
|
-
|
42
|
+
```ruby
|
43
|
+
AppConfig.setup!(yaml: '/path/to/app_config.yml')
|
46
44
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
45
|
+
# Later on...
|
46
|
+
AppConfig.admin_email # => 'admin@example.com'
|
47
|
+
AppConfig.api_name # => 'Supr Webz 2.0'
|
48
|
+
AppConfig.api_key # => 'SUPERAWESOMESERVICE'
|
49
|
+
```
|
51
50
|
|
52
51
|
|
53
|
-
##
|
52
|
+
## Mongo
|
54
53
|
|
55
|
-
You can pass a `:mongo` options hash to
|
56
|
-
configuration values for a Mongo database. Check the
|
54
|
+
You can pass a `:mongo` options hash to `AppConfig.setup!` which should contain
|
55
|
+
configuration values for a Mongo database. Check the `AppConfig::Storage::Mongo::DEFAULTS`
|
57
56
|
constant for the default Mongo connection options.
|
58
57
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
58
|
+
```ruby
|
59
|
+
# These are the defaults.
|
60
|
+
mongo_opts = {
|
61
|
+
host: 'localhost',
|
62
|
+
database: 'app_config',
|
63
|
+
collection: 'app_config'
|
64
|
+
}
|
64
65
|
|
65
|
-
|
66
|
+
AppConfig.setup!(mongo: mongo_opts)
|
66
67
|
|
67
|
-
|
68
|
+
AppConfig.admin_email # => 'admin@example.com'
|
68
69
|
|
69
|
-
|
70
|
-
|
71
|
-
|
70
|
+
# Override an existing value and save to the database:
|
71
|
+
AppConfig.admin_email = 'other_admin@example.com'
|
72
|
+
AppConfig.save!
|
73
|
+
```
|
72
74
|
|
73
75
|
The values are read/saved (by default) to the `app_config` database and
|
74
76
|
`app_config` collection. These defaults can be overridden, however, which
|
75
77
|
might lend well to versioned configurations; collection names such as
|
76
78
|
`app_config_v1`, `app_config_v2`, etc.
|
77
79
|
|
78
|
-
|
80
|
+
```ruby
|
81
|
+
AppConfig.setup!(mongo: { collection: 'app_config_v2' })
|
82
|
+
```
|
83
|
+
|
84
|
+
|
85
|
+
## PostgreSQL
|
86
|
+
|
87
|
+
Using PostgreSQL is similar to a Mongo setup.
|
88
|
+
The only current requirement is that the table have a primary key named `id`.
|
89
|
+
All other columns are used as configuration keys.
|
90
|
+
|
91
|
+
**Note:** The database and schema must exist prior to calling `AppConfig.setup!`.
|
92
|
+
|
93
|
+
Given this schema:
|
94
|
+
|
95
|
+
```sql
|
96
|
+
CREATE TABLE app_config (
|
97
|
+
id bigserial NOT NULL PRIMARY KEY,
|
98
|
+
admin_email character varying(255) DEFAULT 'admin@example.com'::character varying,
|
99
|
+
api_key character varying(255) DEFAULT 'SOME_API_KEY'::character varying
|
100
|
+
);
|
101
|
+
```
|
102
|
+
|
103
|
+
Setup AppConfig:
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
# These are the defaults.
|
107
|
+
postgres_opts = {
|
108
|
+
host: 'localhost',
|
109
|
+
port: 5432,
|
110
|
+
dbname: 'app_config',
|
111
|
+
table: 'app_config',
|
112
|
+
|
113
|
+
# If these are nil (or omitted), the PostgreSQL defaults will be used.
|
114
|
+
user: nil,
|
115
|
+
password: nil,
|
116
|
+
}
|
117
|
+
|
118
|
+
AppConfig.setup!(postgres: postgres_opts)
|
119
|
+
|
120
|
+
AppConfig.admin_email # => 'admin@example.com'
|
121
|
+
|
122
|
+
# Override an existing value and save to the database:
|
123
|
+
AppConfig.admin_email = 'another_admin@example.com'
|
124
|
+
AppConfig.save!
|
125
|
+
```
|
126
|
+
|
127
|
+
|
128
|
+
## Using Storage Defaults
|
129
|
+
|
130
|
+
All storage options accept `true` as a value, which uses the default options for that storage.
|
131
|
+
|
132
|
+
For example, to use the [Mongo](https://github.com/Oshuma/app_config/blob/master/lib/app_config/storage/mongo.rb#L9) defaults:
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
AppConfig.setup!(mongo: true)
|
136
|
+
```
|
137
|
+
|
138
|
+
### Storage Defaults
|
139
|
+
|
140
|
+
* [Mongo](https://github.com/Oshuma/app_config/blob/master/lib/app_config/storage/mongo.rb#L9)
|
141
|
+
* [Postgres](https://github.com/Oshuma/app_config/blob/master/lib/app_config/storage/postgres.rb#L8)
|
142
|
+
* [YAML](https://github.com/Oshuma/app_config/blob/master/lib/app_config/storage/yaml.rb#L9)
|
143
|
+
|
144
|
+
|
145
|
+
## Deprecation Note
|
146
|
+
|
147
|
+
Version `2.x` is **not** backwards compatible with the `1.x` branch.
|
79
148
|
|
149
|
+
See the [wiki](https://github.com/Oshuma/app_config/wiki) for current usage instructions.
|
data/app_config.gemspec
CHANGED
@@ -14,9 +14,10 @@ Gem::Specification.new do |s|
|
|
14
14
|
|
15
15
|
s.add_development_dependency 'bson_ext'
|
16
16
|
s.add_development_dependency 'mongo'
|
17
|
+
s.add_development_dependency 'pg'
|
17
18
|
s.add_development_dependency 'rake'
|
18
19
|
s.add_development_dependency 'redcarpet'
|
19
|
-
s.add_development_dependency 'rspec'
|
20
|
+
s.add_development_dependency 'rspec'
|
20
21
|
s.add_development_dependency 'simplecov'
|
21
22
|
s.add_development_dependency 'yard'
|
22
23
|
|
data/lib/app_config.rb
CHANGED
@@ -21,6 +21,8 @@ module AppConfig
|
|
21
21
|
@@storage = AppConfig::Storage::YAML.new(@@options.delete(:yaml))
|
22
22
|
elsif @@options[:mongo]
|
23
23
|
@@storage = AppConfig::Storage::Mongo.new(@@options.delete(:mongo))
|
24
|
+
elsif @@options[:postgres]
|
25
|
+
@@storage = AppConfig::Storage::Postgres.new(@@options.delete(:postgres))
|
24
26
|
else
|
25
27
|
@@storage = AppConfig::Storage::Base.new
|
26
28
|
end
|
data/lib/app_config/storage.rb
CHANGED
@@ -1,10 +1,8 @@
|
|
1
1
|
module AppConfig
|
2
2
|
module Storage
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
autoload :
|
7
|
-
autoload :Mongo, 'app_config/storage/mongo'
|
8
|
-
autoload :YAML, 'app_config/storage/yaml'
|
3
|
+
autoload :Base, 'app_config/storage/base'
|
4
|
+
autoload :Mongo, 'app_config/storage/mongo'
|
5
|
+
autoload :Postgres, 'app_config/storage/postgres'
|
6
|
+
autoload :YAML, 'app_config/storage/yaml'
|
9
7
|
end
|
10
8
|
end
|
@@ -7,18 +7,23 @@ module AppConfig
|
|
7
7
|
class Mongo < Storage::Base
|
8
8
|
|
9
9
|
DEFAULTS = {
|
10
|
-
:
|
11
|
-
:
|
12
|
-
:
|
13
|
-
:
|
14
|
-
:
|
15
|
-
:
|
10
|
+
host: 'localhost',
|
11
|
+
port: 27017,
|
12
|
+
database: 'app_config',
|
13
|
+
collection: 'app_config',
|
14
|
+
username: nil,
|
15
|
+
password: nil,
|
16
16
|
}
|
17
17
|
|
18
18
|
def initialize(options)
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
# Allows passing `true` as an option.
|
20
|
+
if options.is_a?(Hash)
|
21
|
+
@options = DEFAULTS.merge(options)
|
22
|
+
else
|
23
|
+
@options = DEFAULTS
|
24
|
+
end
|
25
|
+
|
26
|
+
setup_connection!
|
22
27
|
fetch_data!
|
23
28
|
end
|
24
29
|
|
@@ -35,13 +40,13 @@ module AppConfig
|
|
35
40
|
|
36
41
|
private
|
37
42
|
|
38
|
-
def setup_connection
|
43
|
+
def setup_connection!
|
39
44
|
@connection = ::Mongo::Connection.new(@options[:host], @options[:port].to_i)
|
40
|
-
authenticate_connection if @options[:
|
45
|
+
authenticate_connection! if @options[:username] && @options[:password]
|
41
46
|
end
|
42
47
|
|
43
|
-
def authenticate_connection
|
44
|
-
database.authenticate(@options[:
|
48
|
+
def authenticate_connection!
|
49
|
+
database.authenticate(@options[:username], @options[:password])
|
45
50
|
end
|
46
51
|
|
47
52
|
def connected?
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module AppConfig
|
2
|
+
module Storage
|
3
|
+
|
4
|
+
require 'pg'
|
5
|
+
|
6
|
+
class Postgres < Storage::Base
|
7
|
+
|
8
|
+
DEFAULTS = {
|
9
|
+
host: 'localhost',
|
10
|
+
port: 5432,
|
11
|
+
dbname: 'app_config',
|
12
|
+
table: 'app_config',
|
13
|
+
user: nil,
|
14
|
+
password: nil,
|
15
|
+
}
|
16
|
+
|
17
|
+
def initialize(options)
|
18
|
+
# Allows passing `true` as an option.
|
19
|
+
if options.is_a?(Hash)
|
20
|
+
@options = DEFAULTS.merge(options)
|
21
|
+
else
|
22
|
+
@options = DEFAULTS
|
23
|
+
end
|
24
|
+
|
25
|
+
# HACK: Remove the `user` and `password` keys if they're nil, since `@options` is passed directly to `PG.connect`.
|
26
|
+
@options.delete(:user) if @options[:user].nil?
|
27
|
+
@options.delete(:password) if @options[:password].nil?
|
28
|
+
|
29
|
+
@table = @options.delete(:table)
|
30
|
+
|
31
|
+
setup_connection!
|
32
|
+
fetch_data!
|
33
|
+
end
|
34
|
+
|
35
|
+
# Saves the data back to Postgres. Returns `true`/`false`.
|
36
|
+
def save!
|
37
|
+
# Build the `SET foo = 'bar', ...` string for the UPDATE query.
|
38
|
+
data_hash = @data.to_h
|
39
|
+
data_hash.delete(:id) # Remove the primary key (id) from the SET attributes.
|
40
|
+
set_attrs = data_hash.map { |k, v| "#{k} = '#{v}'" }.join(', ')
|
41
|
+
|
42
|
+
update_query = "UPDATE #{@table} SET #{set_attrs} WHERE id = #{@id}"
|
43
|
+
|
44
|
+
result = @connection.exec(update_query)
|
45
|
+
result.result_status == PG::Constants::PGRES_COMMAND_OK
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def fetch_data!
|
51
|
+
raise 'Not connected to PostgreSQL' unless connected?
|
52
|
+
|
53
|
+
# TODO: This might not be the best solution here.
|
54
|
+
# It currently uses the newest row, based on a primary key of `id`.
|
55
|
+
# Maybe provide a way to configure what row gets returned.
|
56
|
+
fetch_query = "SELECT * FROM #{@table} ORDER BY id DESC LIMIT 1"
|
57
|
+
|
58
|
+
@connection.exec(fetch_query) do |result|
|
59
|
+
result.each do |row|
|
60
|
+
@data = OpenStruct.new(row)
|
61
|
+
@id = @data.id
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def connected?
|
67
|
+
@connection && @connection.status == PG::Constants::CONNECTION_OK
|
68
|
+
end
|
69
|
+
|
70
|
+
def setup_connection!
|
71
|
+
@connection = PG.connect(@options)
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -13,6 +13,9 @@ module AppConfig
|
|
13
13
|
#
|
14
14
|
# Defaults to `Dir.home/.app_config.yml`
|
15
15
|
def initialize(path = DEFAULT_PATH)
|
16
|
+
# Allows passing `true` as an option.
|
17
|
+
path = DEFAULT_PATH if path == true
|
18
|
+
|
16
19
|
# Make sure to use the top-level YAML module here.
|
17
20
|
@data = OpenStruct.new(::YAML.load_file(path))
|
18
21
|
end
|
data/lib/app_config/version.rb
CHANGED
@@ -16,12 +16,20 @@ describe AppConfig::Storage::Mongo do
|
|
16
16
|
AppConfig.api_key = 'SOME_NEW_API_KEY'
|
17
17
|
AppConfig.api_key.should == 'SOME_NEW_API_KEY'
|
18
18
|
|
19
|
-
AppConfig.
|
20
|
-
AppConfig.save!
|
19
|
+
AppConfig.save!.should be_true
|
21
20
|
end
|
22
21
|
|
23
22
|
it 'should have a @_id variable for the Mongo ID' do
|
24
|
-
AppConfig.class_variable_get(:@@storage)
|
25
|
-
instance_variable_get(:@_id).should_not be_nil
|
23
|
+
AppConfig.class_variable_get(:@@storage)
|
24
|
+
.instance_variable_get(:@_id).should_not be_nil
|
25
|
+
end
|
26
|
+
|
27
|
+
it "uses the defaults when 'true' is passed" do
|
28
|
+
AppConfig.reset!
|
29
|
+
AppConfig.setup!(mongo: true)
|
30
|
+
|
31
|
+
AppConfig.class_variable_get(:@@storage)
|
32
|
+
.instance_variable_get(:@options)
|
33
|
+
.should == AppConfig::Storage::Mongo::DEFAULTS
|
26
34
|
end
|
27
35
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe AppConfig::Storage::Postgres do
|
4
|
+
|
5
|
+
before(:all) do
|
6
|
+
config_for_postgres
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should have some values' do
|
10
|
+
AppConfig.api_key.should_not be_nil
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should update the values' do
|
14
|
+
new_api_key = 'SOME_NEW_API_KEY'
|
15
|
+
AppConfig.api_key = new_api_key
|
16
|
+
|
17
|
+
AppConfig.save!.should be_true
|
18
|
+
|
19
|
+
# Reload AppConfig
|
20
|
+
config_for_postgres
|
21
|
+
|
22
|
+
AppConfig.api_key.should == new_api_key
|
23
|
+
end
|
24
|
+
|
25
|
+
it "uses the defaults when 'true' is passed" do
|
26
|
+
AppConfig.reset!
|
27
|
+
|
28
|
+
# Hack to use a test database as the 'default'.
|
29
|
+
AppConfig::Storage::Postgres::DEFAULTS[:dbname] = 'app_config_test'
|
30
|
+
|
31
|
+
begin
|
32
|
+
AppConfig.setup!(postgres: true)
|
33
|
+
rescue PG::Error => e
|
34
|
+
config_for_postgres
|
35
|
+
end
|
36
|
+
|
37
|
+
AppConfig.class_variable_get(:@@storage)
|
38
|
+
.instance_variable_get(:@options)
|
39
|
+
.should == AppConfig::Storage::Postgres::DEFAULTS
|
40
|
+
end
|
41
|
+
end
|
@@ -9,7 +9,7 @@ describe AppConfig::Storage::YAML do
|
|
9
9
|
|
10
10
|
it 'should raise file not found' do
|
11
11
|
lambda do
|
12
|
-
config_for_yaml(:
|
12
|
+
config_for_yaml(yaml: 'not/a/real/file.yml')
|
13
13
|
end.should raise_error(Errno::ENOENT)
|
14
14
|
end
|
15
15
|
|
@@ -25,4 +25,13 @@ describe AppConfig::Storage::YAML do
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
+
it "uses the defaults when 'true' is passed" do
|
29
|
+
AppConfig.reset!
|
30
|
+
|
31
|
+
# Hack to use spec config as the 'default'
|
32
|
+
AppConfig::Storage::YAML::DEFAULT_PATH = fixture('app_config.yml')
|
33
|
+
|
34
|
+
AppConfig.setup!(yaml: true)
|
35
|
+
AppConfig.api_key.should_not be_nil
|
36
|
+
end
|
28
37
|
end
|
data/spec/app_config_spec.rb
CHANGED
@@ -25,7 +25,7 @@ describe AppConfig do
|
|
25
25
|
|
26
26
|
it 'should reset @@storage' do
|
27
27
|
# configure first
|
28
|
-
config_for_yaml
|
28
|
+
config_for_yaml
|
29
29
|
# then reset
|
30
30
|
AppConfig.reset!
|
31
31
|
AppConfig.send(:storage).should be_nil
|
@@ -46,15 +46,6 @@ describe AppConfig do
|
|
46
46
|
AppConfig.should be_setup
|
47
47
|
end
|
48
48
|
|
49
|
-
it 'should create nested keys' do
|
50
|
-
pending 'Re-implement this later.'
|
51
|
-
AppConfig.reset!
|
52
|
-
AppConfig.setup!
|
53
|
-
|
54
|
-
AppConfig.person.name.first = 'Dale'
|
55
|
-
AppConfig.person.name.first.should == 'Dale'
|
56
|
-
end
|
57
|
-
|
58
49
|
it 'returns a Hash on setup' do
|
59
50
|
AppConfig.reset!
|
60
51
|
config = AppConfig.setup! do |c|
|
data/spec/spec_helper.rb
CHANGED
@@ -23,29 +23,46 @@ RSpec.configure do |config|
|
|
23
23
|
# Setup YAML options and pass to config_for().
|
24
24
|
def config_for_yaml(opts = {})
|
25
25
|
path = opts[:yaml] || fixture('app_config.yml')
|
26
|
-
config_for({ :
|
26
|
+
config_for({ yaml: path }.merge(opts))
|
27
27
|
end
|
28
28
|
|
29
29
|
def config_for_mongo(opts = {}, load_test_data = true)
|
30
30
|
mongo = AppConfig::Storage::Mongo::DEFAULTS.merge({
|
31
|
-
:
|
32
|
-
:database => 'app_config_test',
|
31
|
+
database: 'app_config_test',
|
33
32
|
})
|
34
33
|
begin
|
35
34
|
load_mongo_test_config(mongo) if load_test_data
|
36
|
-
config_for({:
|
35
|
+
config_for({mongo: mongo}.merge(opts))
|
37
36
|
rescue Mongo::ConnectionFailure
|
38
37
|
pending "***** Mongo specs require a running MongoDB server *****"
|
39
38
|
end
|
40
39
|
end
|
41
40
|
|
41
|
+
def config_for_postgres(opts = {}, load_test_data = true)
|
42
|
+
postgres = AppConfig::Storage::Postgres::DEFAULTS.merge({
|
43
|
+
dbname: 'app_config_test'
|
44
|
+
})
|
45
|
+
|
46
|
+
begin
|
47
|
+
load_postgres_test_config(postgres) if load_test_data
|
48
|
+
config_for({postgres: postgres}.merge(opts))
|
49
|
+
rescue PG::Error => e
|
50
|
+
if e.to_s =~ /could not connect to server/
|
51
|
+
pending "***** Postgres specs require a running PostgreSQL server *****"
|
52
|
+
else
|
53
|
+
# Re-raise the exception, since we only care about connectivity here.
|
54
|
+
raise e
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
42
59
|
private
|
43
60
|
|
44
61
|
def load_mongo_test_config(options)
|
45
62
|
connection = ::Mongo::Connection.new(options[:host], options[:port].to_i)
|
46
63
|
database = connection.db(options[:database])
|
47
64
|
collection = database.collection(options[:collection])
|
48
|
-
test_data = YAML.load_file(fixture('
|
65
|
+
test_data = YAML.load_file(fixture('app_config.yml'))
|
49
66
|
|
50
67
|
data = collection.find_one
|
51
68
|
if data
|
@@ -54,4 +71,37 @@ RSpec.configure do |config|
|
|
54
71
|
collection.save(test_data)
|
55
72
|
end
|
56
73
|
end
|
74
|
+
|
75
|
+
def load_postgres_test_config(options)
|
76
|
+
original_options = options.dup
|
77
|
+
|
78
|
+
options.delete(:user) if options[:user].nil?
|
79
|
+
options.delete(:password) if options[:password].nil?
|
80
|
+
|
81
|
+
table = options.delete(:table)
|
82
|
+
|
83
|
+
begin
|
84
|
+
connection = ::PG.connect(options)
|
85
|
+
|
86
|
+
config = ::YAML.load_file(fixture('app_config.yml'))
|
87
|
+
attrs = config.keys.map { |k| "#{k} character varying(255)" }.join(', ')
|
88
|
+
|
89
|
+
create_query = "CREATE TABLE #{table} (id bigserial primary key, #{attrs})"
|
90
|
+
insert_query = "INSERT INTO #{table} (#{config.keys.join(', ')}) VALUES (#{config.values.map { |v| "'#{v}'" }.join(', ')})"
|
91
|
+
|
92
|
+
connection.exec(create_query)
|
93
|
+
connection.exec(insert_query)
|
94
|
+
rescue PG::Error => e
|
95
|
+
case e.to_s
|
96
|
+
when /database "#{options[:dbname]}" does not exist/
|
97
|
+
%x[createdb -U `whoami` -O `whoami` #{options[:dbname]}]
|
98
|
+
load_postgres_test_config(original_options)
|
99
|
+
when /relation "#{table}" already exists/
|
100
|
+
# no-op
|
101
|
+
else
|
102
|
+
raise e
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
57
107
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: app_config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dale Campbell
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-07-
|
11
|
+
date: 2013-07-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bson_ext
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pg
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: rake
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -70,16 +84,16 @@ dependencies:
|
|
70
84
|
name: rspec
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
72
86
|
requirements:
|
73
|
-
- -
|
87
|
+
- - '>='
|
74
88
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
89
|
+
version: '0'
|
76
90
|
type: :development
|
77
91
|
prerelease: false
|
78
92
|
version_requirements: !ruby/object:Gem::Requirement
|
79
93
|
requirements:
|
80
|
-
- -
|
94
|
+
- - '>='
|
81
95
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
96
|
+
version: '0'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: simplecov
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -130,15 +144,15 @@ files:
|
|
130
144
|
- lib/app_config/storage.rb
|
131
145
|
- lib/app_config/storage/base.rb
|
132
146
|
- lib/app_config/storage/mongo.rb
|
147
|
+
- lib/app_config/storage/postgres.rb
|
133
148
|
- lib/app_config/storage/yaml.rb
|
134
149
|
- lib/app_config/version.rb
|
135
150
|
- spec/app_config/storage/mongo_spec.rb
|
151
|
+
- spec/app_config/storage/postgres_spec.rb
|
136
152
|
- spec/app_config/storage/yaml_spec.rb
|
137
153
|
- spec/app_config/storage_spec.rb
|
138
154
|
- spec/app_config_spec.rb
|
139
155
|
- spec/fixtures/app_config.yml
|
140
|
-
- spec/fixtures/app_config_mongo.yml
|
141
|
-
- spec/fixtures/env_app_config.yml
|
142
156
|
- spec/spec_helper.rb
|
143
157
|
homepage: http://oshuma.github.io/app_config
|
144
158
|
licenses: []
|
@@ -167,11 +181,10 @@ specification_version: 4
|
|
167
181
|
summary: Quick and easy application configuration.
|
168
182
|
test_files:
|
169
183
|
- spec/app_config/storage/mongo_spec.rb
|
184
|
+
- spec/app_config/storage/postgres_spec.rb
|
170
185
|
- spec/app_config/storage/yaml_spec.rb
|
171
186
|
- spec/app_config/storage_spec.rb
|
172
187
|
- spec/app_config_spec.rb
|
173
188
|
- spec/fixtures/app_config.yml
|
174
|
-
- spec/fixtures/app_config_mongo.yml
|
175
|
-
- spec/fixtures/env_app_config.yml
|
176
189
|
- spec/spec_helper.rb
|
177
190
|
has_rdoc: true
|
@@ -1,17 +0,0 @@
|
|
1
|
-
defaults: &defaults
|
2
|
-
title: 'AppConfig Env Mode'
|
3
|
-
admin_email: 'admin@example.com'
|
4
|
-
api_name: 'Supr Webz 2.0'
|
5
|
-
api_key: 'SUPERAWESOMESERVICE'
|
6
|
-
|
7
|
-
development:
|
8
|
-
<<: *defaults
|
9
|
-
admin_email: 'dev@example.com'
|
10
|
-
|
11
|
-
production:
|
12
|
-
<<: *defaults
|
13
|
-
title: 'AppConfig Env Production Mode!'
|
14
|
-
|
15
|
-
test:
|
16
|
-
<<: *defaults
|
17
|
-
admin_email: 'test@example.com'
|