app_config 2.6.0 → 2.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/app_config/version.rb +1 -1
- metadata +45 -41
- data/.gitignore +0 -8
- data/.rspec +0 -1
- data/.travis.yml +0 -29
- data/Gemfile +0 -4
- data/Gemfile.lock +0 -51
- data/README.md +0 -247
- data/Rakefile +0 -33
- data/app_config.gemspec +0 -35
- data/doc/.gitignore +0 -0
- data/spec/app_config/storage/base_spec.rb +0 -10
- data/spec/app_config/storage/config_data_spec.rb +0 -10
- data/spec/app_config/storage/mongo_spec.rb +0 -47
- data/spec/app_config/storage/mysql_spec.rb +0 -75
- data/spec/app_config/storage/postgres_spec.rb +0 -81
- data/spec/app_config/storage/sqlite_spec.rb +0 -57
- data/spec/app_config/storage/yaml_spec.rb +0 -59
- data/spec/app_config/storage_spec.rb +0 -4
- data/spec/app_config_spec.rb +0 -66
- data/spec/fixtures/app_config.yml +0 -6
- data/spec/fixtures/app_config_env.yml +0 -15
- data/spec/spec_helper.rb +0 -231
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3e8bb34562d1c51420e4a75e84488b40831e88d8
|
4
|
+
data.tar.gz: 83b5123812d2c423af6fc6a1fee36b2becb83f2f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e1a25ad0387eeba5c75a63ad83d619ebee2d2651b157a3eb12a805d32fd2678893e201cbdc582b1dfdf14259c0f92578a18511496b71c3d7bb1762088bf09dc5
|
7
|
+
data.tar.gz: df48f7919ef1b0ed21adba0de4c7e05b34419f862bcd15e9a180b3943a679924a803281cc97d9ec3d838ac995d2e44e3f0e3978fffff0d60f936d153786e19a9
|
data/lib/app_config/version.rb
CHANGED
metadata
CHANGED
@@ -1,71 +1,85 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: app_config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.7.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:
|
11
|
+
date: 2018-05-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bson
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.12.5
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.12.5
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bson_ext
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
16
30
|
requirements:
|
17
|
-
- - "
|
31
|
+
- - "~>"
|
18
32
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
33
|
+
version: 1.12.5
|
20
34
|
type: :development
|
21
35
|
prerelease: false
|
22
36
|
version_requirements: !ruby/object:Gem::Requirement
|
23
37
|
requirements:
|
24
|
-
- - "
|
38
|
+
- - "~>"
|
25
39
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
40
|
+
version: 1.12.5
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: mongo
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
30
44
|
requirements:
|
31
|
-
- - "
|
45
|
+
- - "~>"
|
32
46
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
47
|
+
version: 1.12.5
|
34
48
|
type: :development
|
35
49
|
prerelease: false
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
37
51
|
requirements:
|
38
|
-
- - "
|
52
|
+
- - "~>"
|
39
53
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
54
|
+
version: 1.12.5
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: mysql2
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
44
58
|
requirements:
|
45
|
-
- - "
|
59
|
+
- - "~>"
|
46
60
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
61
|
+
version: 0.5.1
|
48
62
|
type: :development
|
49
63
|
prerelease: false
|
50
64
|
version_requirements: !ruby/object:Gem::Requirement
|
51
65
|
requirements:
|
52
|
-
- - "
|
66
|
+
- - "~>"
|
53
67
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
68
|
+
version: 0.5.1
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: pg
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
58
72
|
requirements:
|
59
|
-
- - "
|
73
|
+
- - "~>"
|
60
74
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
75
|
+
version: '1.0'
|
62
76
|
type: :development
|
63
77
|
prerelease: false
|
64
78
|
version_requirements: !ruby/object:Gem::Requirement
|
65
79
|
requirements:
|
66
|
-
- - "
|
80
|
+
- - "~>"
|
67
81
|
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
82
|
+
version: '1.0'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: rake
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -158,15 +172,6 @@ executables: []
|
|
158
172
|
extensions: []
|
159
173
|
extra_rdoc_files: []
|
160
174
|
files:
|
161
|
-
- ".gitignore"
|
162
|
-
- ".rspec"
|
163
|
-
- ".travis.yml"
|
164
|
-
- Gemfile
|
165
|
-
- Gemfile.lock
|
166
|
-
- README.md
|
167
|
-
- Rakefile
|
168
|
-
- app_config.gemspec
|
169
|
-
- doc/.gitignore
|
170
175
|
- lib/app_config.rb
|
171
176
|
- lib/app_config/error.rb
|
172
177
|
- lib/app_config/storage.rb
|
@@ -178,18 +183,6 @@ files:
|
|
178
183
|
- lib/app_config/storage/sqlite.rb
|
179
184
|
- lib/app_config/storage/yaml.rb
|
180
185
|
- lib/app_config/version.rb
|
181
|
-
- spec/app_config/storage/base_spec.rb
|
182
|
-
- spec/app_config/storage/config_data_spec.rb
|
183
|
-
- spec/app_config/storage/mongo_spec.rb
|
184
|
-
- spec/app_config/storage/mysql_spec.rb
|
185
|
-
- spec/app_config/storage/postgres_spec.rb
|
186
|
-
- spec/app_config/storage/sqlite_spec.rb
|
187
|
-
- spec/app_config/storage/yaml_spec.rb
|
188
|
-
- spec/app_config/storage_spec.rb
|
189
|
-
- spec/app_config_spec.rb
|
190
|
-
- spec/fixtures/app_config.yml
|
191
|
-
- spec/fixtures/app_config_env.yml
|
192
|
-
- spec/spec_helper.rb
|
193
186
|
homepage: http://oshuma.github.io/app_config
|
194
187
|
licenses:
|
195
188
|
- MIT
|
@@ -212,8 +205,19 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
212
205
|
version: '0'
|
213
206
|
requirements: []
|
214
207
|
rubyforge_project:
|
215
|
-
rubygems_version: 2.5.1
|
208
|
+
rubygems_version: 2.5.2.1
|
216
209
|
signing_key:
|
217
210
|
specification_version: 4
|
218
211
|
summary: Quick and easy application configuration.
|
219
|
-
test_files:
|
212
|
+
test_files:
|
213
|
+
- lib/app_config/error.rb
|
214
|
+
- lib/app_config/version.rb
|
215
|
+
- lib/app_config/storage/base.rb
|
216
|
+
- lib/app_config/storage/postgres.rb
|
217
|
+
- lib/app_config/storage/mongo.rb
|
218
|
+
- lib/app_config/storage/yaml.rb
|
219
|
+
- lib/app_config/storage/sqlite.rb
|
220
|
+
- lib/app_config/storage/mysql.rb
|
221
|
+
- lib/app_config/storage/config_data.rb
|
222
|
+
- lib/app_config/storage.rb
|
223
|
+
- lib/app_config.rb
|
data/.gitignore
DELETED
data/.rspec
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
--color
|
data/.travis.yml
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
language: ruby
|
2
|
-
rvm:
|
3
|
-
- 1.9.2
|
4
|
-
- 1.9.3
|
5
|
-
- 2.0.0
|
6
|
-
- 2.1
|
7
|
-
- 2.2
|
8
|
-
|
9
|
-
before_script:
|
10
|
-
# Setup test Postgres database.
|
11
|
-
- psql -c 'create database app_config_test;' -U postgres
|
12
|
-
- psql -d app_config_test -U postgres -c 'CREATE TABLE app_config (id bigserial primary key, admin_email character varying(255), api_name character varying(255), api_key character varying(255), true_option boolean, false_option boolean);'
|
13
|
-
- psql -d app_config_test -U postgres -c "INSERT INTO app_config (admin_email, api_name, api_key, true_option, false_option) VALUES ('admin@example.com', 'Supr Webz 2.0', 'SUPERAWESOMESERVICE', true, false);"
|
14
|
-
# Setup test MySQL database.
|
15
|
-
- mysql -u root -e 'create database app_config_test;'
|
16
|
-
- mysql -u root -D app_config_test -e 'CREATE TABLE app_config (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, admin_email VARCHAR(255), api_name VARCHAR(255), api_key VARCHAR(255), true_option BOOLEAN, false_option BOOLEAN);'
|
17
|
-
- mysql -u root -D app_config_test -e "INSERT INTO app_config (admin_email, api_name, api_key, true_option, false_option) VALUES ('admin@example.com', 'Supr Webz 2.0', 'SUPERAWESOMESERVICE', true, false);"
|
18
|
-
|
19
|
-
env:
|
20
|
-
# Ignore warnings when running specs.
|
21
|
-
- RUBYOPT="-W0"
|
22
|
-
|
23
|
-
branches:
|
24
|
-
only:
|
25
|
-
- master
|
26
|
-
|
27
|
-
services:
|
28
|
-
- mongodb
|
29
|
-
- postgres
|
data/Gemfile
DELETED
data/Gemfile.lock
DELETED
@@ -1,51 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
app_config (2.6.0)
|
5
|
-
|
6
|
-
GEM
|
7
|
-
remote: https://rubygems.org/
|
8
|
-
specs:
|
9
|
-
bson (1.3.1)
|
10
|
-
bson_ext (1.3.1)
|
11
|
-
diff-lcs (1.1.3)
|
12
|
-
mongo (1.3.1)
|
13
|
-
bson (>= 1.3.1)
|
14
|
-
multi_json (1.3.6)
|
15
|
-
mysql2 (0.3.15)
|
16
|
-
pg (0.15.1)
|
17
|
-
rake (0.9.2.2)
|
18
|
-
redcarpet (2.3.0)
|
19
|
-
rspec (2.10.0)
|
20
|
-
rspec-core (~> 2.10.0)
|
21
|
-
rspec-expectations (~> 2.10.0)
|
22
|
-
rspec-mocks (~> 2.10.0)
|
23
|
-
rspec-core (2.10.1)
|
24
|
-
rspec-expectations (2.10.0)
|
25
|
-
diff-lcs (~> 1.1.3)
|
26
|
-
rspec-mocks (2.10.1)
|
27
|
-
simplecov (0.6.4)
|
28
|
-
multi_json (~> 1.0)
|
29
|
-
simplecov-html (~> 0.5.3)
|
30
|
-
simplecov-html (0.5.3)
|
31
|
-
sqlite3 (1.3.10)
|
32
|
-
yard (0.9.12)
|
33
|
-
|
34
|
-
PLATFORMS
|
35
|
-
ruby
|
36
|
-
|
37
|
-
DEPENDENCIES
|
38
|
-
app_config!
|
39
|
-
bson_ext
|
40
|
-
mongo
|
41
|
-
mysql2
|
42
|
-
pg
|
43
|
-
rake
|
44
|
-
redcarpet
|
45
|
-
rspec
|
46
|
-
simplecov
|
47
|
-
sqlite3
|
48
|
-
yard (~> 0.9.12)
|
49
|
-
|
50
|
-
BUNDLED WITH
|
51
|
-
1.16.1
|
data/README.md
DELETED
@@ -1,247 +0,0 @@
|
|
1
|
-
# AppConfig [![Build Status](https://travis-ci.org/Oshuma/app_config.png?branch=master)](https://travis-ci.org/Oshuma/app_config) [![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=3N885MZB7QCY6&lc=US&item_name=Dale%20Campbell&item_number=app_config¤cy_code=USD&bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHosted)
|
2
|
-
|
3
|
-
An easy to use, customizable library to easily store and retrieve application
|
4
|
-
configuration; basically anything in 'key/value' pairs.
|
5
|
-
|
6
|
-
AppConfig requires at least Ruby 1.9.
|
7
|
-
|
8
|
-
|
9
|
-
## Usage
|
10
|
-
|
11
|
-
Usage is simple. Just pass either a hash of options, or a block, to `AppConfig.setup!`.
|
12
|
-
|
13
|
-
In it's simplest form, you can use it like so:
|
14
|
-
|
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
|
21
|
-
|
22
|
-
AppConfig.admin_email # => 'admin@example.com'
|
23
|
-
```
|
24
|
-
|
25
|
-
AppConfig also supports many different 'storage methods', such as YAML and MongoDB,
|
26
|
-
allowing you to tailor AppConfig to many different use cases. For example,
|
27
|
-
storing your configuration in the same database as your development/production environment.
|
28
|
-
|
29
|
-
|
30
|
-
## YAML
|
31
|
-
|
32
|
-
Given this YAML file:
|
33
|
-
|
34
|
-
```yaml
|
35
|
-
---
|
36
|
-
admin_email: 'admin@example.com'
|
37
|
-
api_name: 'Supr Webz 2.0'
|
38
|
-
api_key: 'SUPERAWESOMESERVICE'
|
39
|
-
```
|
40
|
-
|
41
|
-
Use it like so:
|
42
|
-
|
43
|
-
```ruby
|
44
|
-
AppConfig.setup!(yaml: '/path/to/app_config.yml')
|
45
|
-
|
46
|
-
# Later on...
|
47
|
-
AppConfig.admin_email # => 'admin@example.com'
|
48
|
-
AppConfig.api_name # => 'Supr Webz 2.0'
|
49
|
-
AppConfig.api_key # => 'SUPERAWESOMESERVICE'
|
50
|
-
```
|
51
|
-
|
52
|
-
|
53
|
-
## Mongo
|
54
|
-
|
55
|
-
You can pass a `:mongo` options hash to `AppConfig.setup!` which should contain
|
56
|
-
configuration values for a Mongo database. Check the `AppConfig::Storage::Mongo::DEFAULTS`
|
57
|
-
constant for the default Mongo connection options.
|
58
|
-
|
59
|
-
The '[mongo](https://rubygems.org/gems/mongo)' gem is required in order to use Mongo storage.
|
60
|
-
|
61
|
-
```ruby
|
62
|
-
# These are the defaults.
|
63
|
-
mongo_opts = {
|
64
|
-
host: 'localhost',
|
65
|
-
database: 'app_config',
|
66
|
-
collection: 'app_config'
|
67
|
-
}
|
68
|
-
|
69
|
-
AppConfig.setup!(mongo: mongo_opts)
|
70
|
-
|
71
|
-
AppConfig.admin_email # => 'admin@example.com'
|
72
|
-
|
73
|
-
# Override an existing value and save to the database:
|
74
|
-
AppConfig.admin_email = 'other_admin@example.com'
|
75
|
-
AppConfig.save!
|
76
|
-
```
|
77
|
-
|
78
|
-
The values are read/saved (by default) to the `app_config` database and
|
79
|
-
`app_config` collection. These defaults can be overridden, however, which
|
80
|
-
might lend well to versioned configurations; collection names such as
|
81
|
-
`app_config_v1`, `app_config_v2`, etc.
|
82
|
-
|
83
|
-
```ruby
|
84
|
-
AppConfig.setup!(mongo: { collection: 'app_config_v2' })
|
85
|
-
```
|
86
|
-
|
87
|
-
|
88
|
-
## PostgreSQL
|
89
|
-
|
90
|
-
Using PostgreSQL is similar to a Mongo setup.
|
91
|
-
The only current requirement is that the table have a primary key named `id`.
|
92
|
-
All other columns are used as configuration keys.
|
93
|
-
|
94
|
-
The '[pg](https://rubygems.org/gems/pg)' gem is required in order to use Postgres storage.
|
95
|
-
|
96
|
-
**Note:** The database and schema must exist prior to calling `AppConfig.setup!`.
|
97
|
-
|
98
|
-
Given this schema:
|
99
|
-
|
100
|
-
```sql
|
101
|
-
CREATE TABLE app_config (
|
102
|
-
id bigserial NOT NULL PRIMARY KEY,
|
103
|
-
admin_email character varying(255) DEFAULT 'admin@example.com'::character varying,
|
104
|
-
api_key character varying(255) DEFAULT 'SOME_API_KEY'::character varying
|
105
|
-
);
|
106
|
-
```
|
107
|
-
|
108
|
-
Setup AppConfig:
|
109
|
-
|
110
|
-
```ruby
|
111
|
-
# These are the defaults.
|
112
|
-
postgres_opts = {
|
113
|
-
host: 'localhost',
|
114
|
-
port: 5432,
|
115
|
-
dbname: 'app_config',
|
116
|
-
table: 'app_config',
|
117
|
-
|
118
|
-
# If these are nil (or omitted), the PostgreSQL defaults will be used.
|
119
|
-
user: nil,
|
120
|
-
password: nil,
|
121
|
-
}
|
122
|
-
|
123
|
-
AppConfig.setup!(postgres: postgres_opts)
|
124
|
-
|
125
|
-
AppConfig.admin_email # => 'admin@example.com'
|
126
|
-
|
127
|
-
# Override an existing value and save to the database:
|
128
|
-
AppConfig.admin_email = 'another_admin@example.com'
|
129
|
-
AppConfig.save!
|
130
|
-
```
|
131
|
-
|
132
|
-
|
133
|
-
## MySQL
|
134
|
-
|
135
|
-
Using MySQL is similar to Postgres, including the required primary key named `id`.
|
136
|
-
All other columns are used as configuration keys.
|
137
|
-
|
138
|
-
The '[mysql2](https://rubygems.org/gems/mysql2)' gem is required in order to use MySQL storage.
|
139
|
-
|
140
|
-
**Note:** The database and schema must exist prior to calling `AppConfig.setup!`.
|
141
|
-
|
142
|
-
Given this schema:
|
143
|
-
|
144
|
-
```sql
|
145
|
-
CREATE TABLE app_config (
|
146
|
-
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
147
|
-
admin_email VARCHAR(255) DEFAULT "admin@example.com",
|
148
|
-
api_key VARCHAR(255) DEFAULT "SOME_API_KEY",
|
149
|
-
true_option BOOLEAN DEFAULT true,
|
150
|
-
false_option BOOLEAN DEFAULT false
|
151
|
-
);
|
152
|
-
```
|
153
|
-
|
154
|
-
Setup AppConfig:
|
155
|
-
|
156
|
-
```ruby
|
157
|
-
# These are the defaults:
|
158
|
-
mysql_opts = {
|
159
|
-
host: 'localhost',
|
160
|
-
port: 3306,
|
161
|
-
database: 'app_config',
|
162
|
-
table: 'app_config',
|
163
|
-
username: nil,
|
164
|
-
password: nil,
|
165
|
-
}
|
166
|
-
|
167
|
-
AppConfig.setup!(mysql: mysql_opts)
|
168
|
-
|
169
|
-
AppConfig.admin_email # => 'admin@example.com'
|
170
|
-
|
171
|
-
# Update an existing value and save changes:
|
172
|
-
AppConfig.admin_email = 'another_admin@example.com'
|
173
|
-
AppConfig.save!
|
174
|
-
```
|
175
|
-
|
176
|
-
|
177
|
-
## SQLite
|
178
|
-
|
179
|
-
SQLite storage works the same as the other SQL storage methods, including the mandatory
|
180
|
-
primary key `id` column.
|
181
|
-
|
182
|
-
The '[sqlite3](https://rubygems.org/gems/sqlite3)' gem is required in order to use SQLite storage.
|
183
|
-
|
184
|
-
**Note:** The database schema must exist prior to calling `AppConfig.setup!`.
|
185
|
-
|
186
|
-
```ruby
|
187
|
-
# These are the defaults:
|
188
|
-
sqlite_opts = {
|
189
|
-
database: File.join(Dir.home, '.app_config.sqlite3'),
|
190
|
-
table: 'app_config',
|
191
|
-
}
|
192
|
-
|
193
|
-
AppConfig.setup!(sqlite: sqlite_opts)
|
194
|
-
```
|
195
|
-
|
196
|
-
|
197
|
-
## Using Storage Defaults
|
198
|
-
|
199
|
-
All storage options accept `true` as a value, which uses the default options for that storage.
|
200
|
-
|
201
|
-
For example, to use the [Mongo](lib/app_config/storage/mongo.rb#L9) defaults:
|
202
|
-
|
203
|
-
```ruby
|
204
|
-
AppConfig.setup!(mongo: true)
|
205
|
-
```
|
206
|
-
|
207
|
-
### Storage Defaults
|
208
|
-
|
209
|
-
* [Mongo](lib/app_config/storage/mongo.rb#L9)
|
210
|
-
* [MySQL](lib/app_config/storage/mysql.rb#L8)
|
211
|
-
* [Postgres](lib/app_config/storage/postgres.rb#L8)
|
212
|
-
* [SQLite](lib/app_config/storage/sqlite.rb#L9)
|
213
|
-
* [YAML](lib/app_config/storage/yaml.rb#L9)
|
214
|
-
|
215
|
-
|
216
|
-
### Environment Mode
|
217
|
-
|
218
|
-
The YAML storage method provides an `:env` option where you can organize the config like Rails `database.yml`:
|
219
|
-
|
220
|
-
```yaml
|
221
|
-
# Rails.root/config/app_config.yml
|
222
|
-
development:
|
223
|
-
title: 'Development Mode'
|
224
|
-
|
225
|
-
production:
|
226
|
-
title: 'Production Mode'
|
227
|
-
```
|
228
|
-
|
229
|
-
Pass a string or symbol to the `:env` option.
|
230
|
-
|
231
|
-
```ruby
|
232
|
-
# Rails.root/config/initializers/app_config.rb
|
233
|
-
AppConfig.setup!({
|
234
|
-
yaml: "#{Rails.root}/config/app_config.yml",
|
235
|
-
env: Rails.env
|
236
|
-
})
|
237
|
-
|
238
|
-
# Uses the given environment section of the config.
|
239
|
-
AppConfig.title # => 'Production Mode'
|
240
|
-
```
|
241
|
-
|
242
|
-
|
243
|
-
## Deprecation Note
|
244
|
-
|
245
|
-
Version `2.x` is **not** backwards compatible with the `1.x` branch.
|
246
|
-
|
247
|
-
See the [wiki](https://github.com/Oshuma/app_config/wiki) for current usage instructions.
|
data/Rakefile
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'bundler'
|
2
|
-
require 'rdoc/task'
|
3
|
-
require 'rspec/core/rake_task'
|
4
|
-
|
5
|
-
Bundler::GemHelper.install_tasks
|
6
|
-
|
7
|
-
task :default => :spec
|
8
|
-
|
9
|
-
desc 'Start an irb session with AppConfig loaded'
|
10
|
-
task :console do
|
11
|
-
sh "irb -I ./lib -r 'app_config'"
|
12
|
-
end
|
13
|
-
|
14
|
-
desc 'Run the specs'
|
15
|
-
RSpec::Core::RakeTask.new(:spec)
|
16
|
-
|
17
|
-
task :doc => [ 'doc:clean', 'doc:api' ]
|
18
|
-
|
19
|
-
namespace :doc do
|
20
|
-
require 'yard'
|
21
|
-
YARD::Rake::YardocTask.new(:api) do |t|
|
22
|
-
t.files = ['lib/**/*.rb']
|
23
|
-
t.options = [
|
24
|
-
'--output-dir', 'doc/api',
|
25
|
-
'--markup', 'markdown'
|
26
|
-
]
|
27
|
-
end
|
28
|
-
|
29
|
-
desc 'Remove YARD Documentation'
|
30
|
-
task :clean do
|
31
|
-
system("rm -rf #{File.dirname(__FILE__)}/doc/api")
|
32
|
-
end
|
33
|
-
end
|
data/app_config.gemspec
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
$:.push File.expand_path("../lib", __FILE__)
|
2
|
-
require 'app_config'
|
3
|
-
|
4
|
-
Gem::Specification.new do |s|
|
5
|
-
s.name = "app_config"
|
6
|
-
s.version = AppConfig::VERSION
|
7
|
-
|
8
|
-
s.authors = ['Dale Campbell']
|
9
|
-
s.email = ['oshuma@gmail.com']
|
10
|
-
s.homepage = 'http://oshuma.github.io/app_config'
|
11
|
-
|
12
|
-
s.license = 'MIT'
|
13
|
-
|
14
|
-
s.summary = %q{Quick and easy application configuration.}
|
15
|
-
s.description = %q{An easy to use, framework agnostic, customizable library to easily store and retrieve application configuration.}
|
16
|
-
|
17
|
-
s.add_development_dependency 'bson_ext'
|
18
|
-
s.add_development_dependency 'mongo'
|
19
|
-
s.add_development_dependency 'mysql2'
|
20
|
-
s.add_development_dependency 'pg'
|
21
|
-
s.add_development_dependency 'rake'
|
22
|
-
s.add_development_dependency 'redcarpet'
|
23
|
-
s.add_development_dependency 'rspec'
|
24
|
-
s.add_development_dependency 'simplecov'
|
25
|
-
s.add_development_dependency 'sqlite3'
|
26
|
-
s.add_development_dependency 'yard', '~> 0.9.12'
|
27
|
-
|
28
|
-
s.has_rdoc = true
|
29
|
-
s.rdoc_options = ["--inline-source", "--charset=UTF-8"]
|
30
|
-
|
31
|
-
s.require_paths = ["lib"]
|
32
|
-
s.files = `git ls-files`.split("\n")
|
33
|
-
|
34
|
-
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
35
|
-
end
|
data/doc/.gitignore
DELETED
File without changes
|
@@ -1,47 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe AppConfig::Storage::Mongo do
|
4
|
-
|
5
|
-
before(:all) do
|
6
|
-
AppConfig.reset!
|
7
|
-
config_for_mongo
|
8
|
-
end
|
9
|
-
|
10
|
-
it 'should have some values' do
|
11
|
-
AppConfig.api_key.should_not be_nil
|
12
|
-
end
|
13
|
-
|
14
|
-
it 'should update the values' do
|
15
|
-
AppConfig.api_key = 'SOME_NEW_API_KEY'
|
16
|
-
AppConfig.api_key.should == 'SOME_NEW_API_KEY'
|
17
|
-
|
18
|
-
AppConfig.save!.should be_true
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'should have a @_id variable for the Mongo ID' do
|
22
|
-
AppConfig.class_variable_get(:@@storage)
|
23
|
-
.instance_variable_get(:@_id).should_not be_nil
|
24
|
-
end
|
25
|
-
|
26
|
-
it "uses the defaults when 'true' is passed" do
|
27
|
-
AppConfig.reset!
|
28
|
-
AppConfig.setup!(mongo: true)
|
29
|
-
|
30
|
-
AppConfig.class_variable_get(:@@storage)
|
31
|
-
.instance_variable_get(:@options)
|
32
|
-
.should == AppConfig::Storage::Mongo::DEFAULTS
|
33
|
-
end
|
34
|
-
|
35
|
-
it 'should reload the data' do
|
36
|
-
AppConfig.reset!
|
37
|
-
config_for_mongo(true, false)
|
38
|
-
|
39
|
-
# Set a variable, but do not call AppConfig.save!
|
40
|
-
AppConfig.true_option = false
|
41
|
-
|
42
|
-
AppConfig.reload!
|
43
|
-
|
44
|
-
AppConfig.true_option.should == true
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
@@ -1,75 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe AppConfig::Storage::MySQL do
|
4
|
-
|
5
|
-
before(:all) do
|
6
|
-
config_for_mysql(true)
|
7
|
-
end
|
8
|
-
|
9
|
-
it 'should have some values' do
|
10
|
-
AppConfig.api_key.should_not be_nil
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'should reload the data' do
|
14
|
-
# Set a value, but don't call AppConfig.save!
|
15
|
-
AppConfig.true_option = false
|
16
|
-
|
17
|
-
AppConfig.reload!
|
18
|
-
|
19
|
-
AppConfig.true_option.should == true
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'should update the values' do
|
23
|
-
new_api_key = 'NEW_API_KEY'
|
24
|
-
new_admin_email = 'new_admin@example.com'
|
25
|
-
|
26
|
-
AppConfig.api_key = new_api_key
|
27
|
-
AppConfig.admin_email = new_admin_email
|
28
|
-
|
29
|
-
AppConfig.save!.should be_true
|
30
|
-
|
31
|
-
# Reload AppConfig
|
32
|
-
config_for_mysql
|
33
|
-
|
34
|
-
AppConfig.api_key.should == new_api_key
|
35
|
-
AppConfig.admin_email.should == new_admin_email
|
36
|
-
end
|
37
|
-
|
38
|
-
it "uses the defaults when 'true' is passed" do
|
39
|
-
AppConfig.reset!
|
40
|
-
|
41
|
-
# HACK: Use a test database as the 'default'.
|
42
|
-
old_dbname = AppConfig::Storage::MySQL::DEFAULTS[:database]
|
43
|
-
AppConfig::Storage::MySQL::DEFAULTS[:database] = 'app_config_test'
|
44
|
-
|
45
|
-
begin
|
46
|
-
AppConfig.setup!(mysql: true)
|
47
|
-
rescue Mysql2::Error => e
|
48
|
-
config_for_mysql
|
49
|
-
end
|
50
|
-
|
51
|
-
AppConfig.class_variable_get(:@@storage)
|
52
|
-
.instance_variable_get(:@options)
|
53
|
-
.should == AppConfig::Storage::MySQL::DEFAULTS
|
54
|
-
|
55
|
-
# HACK: Reset dbname default to original value.
|
56
|
-
AppConfig::Storage::MySQL::DEFAULTS[:database] = old_dbname
|
57
|
-
end
|
58
|
-
|
59
|
-
it "should create a new row if @id is not set" do
|
60
|
-
# HACK: Save the old id so we can reset it.
|
61
|
-
original_id = AppConfig.class_variable_get(:@@storage)
|
62
|
-
.instance_variable_get(:@id)
|
63
|
-
|
64
|
-
AppConfig.class_variable_get(:@@storage)
|
65
|
-
.instance_variable_set(:@id, nil)
|
66
|
-
|
67
|
-
AppConfig.api_key = 'foobar'
|
68
|
-
AppConfig.save!.should be_true
|
69
|
-
|
70
|
-
# HACK: Reset the original id.
|
71
|
-
AppConfig.class_variable_get(:@@storage)
|
72
|
-
.instance_variable_set(:@id, original_id)
|
73
|
-
end
|
74
|
-
|
75
|
-
end
|
@@ -1,81 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe AppConfig::Storage::Postgres do
|
4
|
-
|
5
|
-
before(:all) do
|
6
|
-
config_for_postgres(true)
|
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
|
-
new_admin_email = 'foo@example.com'
|
16
|
-
|
17
|
-
AppConfig.api_key = new_api_key
|
18
|
-
AppConfig.admin_email = new_admin_email
|
19
|
-
|
20
|
-
AppConfig.save!.should be_true
|
21
|
-
|
22
|
-
# Reload AppConfig
|
23
|
-
config_for_postgres
|
24
|
-
|
25
|
-
AppConfig.api_key.should == new_api_key
|
26
|
-
AppConfig.admin_email.should == new_admin_email
|
27
|
-
end
|
28
|
-
|
29
|
-
it "uses the defaults when 'true' is passed" do
|
30
|
-
AppConfig.reset!
|
31
|
-
|
32
|
-
# HACK: Use a test database as the 'default'.
|
33
|
-
old_dbname = AppConfig::Storage::Postgres::DEFAULTS[:dbname]
|
34
|
-
AppConfig::Storage::Postgres::DEFAULTS[:dbname] = 'app_config_test'
|
35
|
-
|
36
|
-
begin
|
37
|
-
AppConfig.setup!(postgres: true)
|
38
|
-
rescue PG::Error => e
|
39
|
-
config_for_postgres
|
40
|
-
end
|
41
|
-
|
42
|
-
AppConfig.class_variable_get(:@@storage)
|
43
|
-
.instance_variable_get(:@options)
|
44
|
-
.should == AppConfig::Storage::Postgres::DEFAULTS
|
45
|
-
|
46
|
-
# HACK: Reset dbname default to original value.
|
47
|
-
AppConfig::Storage::Postgres::DEFAULTS[:dbname] = old_dbname
|
48
|
-
end
|
49
|
-
|
50
|
-
it "should create a new row if @id is not set" do
|
51
|
-
# HACK: Save the old id so we can reset it.
|
52
|
-
original_id = AppConfig.class_variable_get(:@@storage)
|
53
|
-
.instance_variable_get(:@id)
|
54
|
-
|
55
|
-
AppConfig.class_variable_get(:@@storage)
|
56
|
-
.instance_variable_set(:@id, nil)
|
57
|
-
|
58
|
-
AppConfig.api_key = 'foobar'
|
59
|
-
AppConfig.save!.should be_true
|
60
|
-
|
61
|
-
# HACK: Reset the original id.
|
62
|
-
AppConfig.class_variable_get(:@@storage)
|
63
|
-
.instance_variable_set(:@id, original_id)
|
64
|
-
end
|
65
|
-
|
66
|
-
it "turns Postgres booleans into Ruby objects" do
|
67
|
-
# set in spec/fixtures/app_config.yml
|
68
|
-
AppConfig.true_option.class.should == TrueClass
|
69
|
-
AppConfig.false_option.class.should == FalseClass
|
70
|
-
end
|
71
|
-
|
72
|
-
it 'should reload the data' do
|
73
|
-
# Set a variable, but do not call AppConfig.save!
|
74
|
-
AppConfig.true_option = false
|
75
|
-
|
76
|
-
AppConfig.reload!
|
77
|
-
|
78
|
-
AppConfig.true_option.should == true
|
79
|
-
end
|
80
|
-
|
81
|
-
end
|
@@ -1,57 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe AppConfig::Storage::SQLite do
|
4
|
-
|
5
|
-
before(:all) do
|
6
|
-
config_for_sqlite
|
7
|
-
end
|
8
|
-
|
9
|
-
it 'should have some values' do
|
10
|
-
AppConfig.api_key.should_not be_nil
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'should reload the data' do
|
14
|
-
# Save the old value, so we can make sure it hasn't changed.
|
15
|
-
admin_email = AppConfig.admin_email
|
16
|
-
|
17
|
-
# Set a value, but don't call AppConfig.save!
|
18
|
-
AppConfig.admin_email = 'CHANGED'
|
19
|
-
|
20
|
-
AppConfig.reload!
|
21
|
-
|
22
|
-
AppConfig.admin_email.should == admin_email
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'should update the values' do
|
26
|
-
new_api_key = 'NEW_API_KEY'
|
27
|
-
new_admin_email = 'new_admin@example.com'
|
28
|
-
|
29
|
-
AppConfig.api_key = new_api_key
|
30
|
-
AppConfig.admin_email = new_admin_email
|
31
|
-
|
32
|
-
AppConfig.save!.should be_true
|
33
|
-
|
34
|
-
# Reload AppConfig (passing `false` to avoid wiping test db).
|
35
|
-
config_for_sqlite(false)
|
36
|
-
|
37
|
-
AppConfig.api_key.should == new_api_key
|
38
|
-
AppConfig.admin_email.should == new_admin_email
|
39
|
-
end
|
40
|
-
|
41
|
-
it 'should create a new row if @id is not set' do
|
42
|
-
# HACK: Save the old id so we can reset it.
|
43
|
-
original_id = AppConfig.class_variable_get(:@@storage)
|
44
|
-
.instance_variable_get(:@id)
|
45
|
-
|
46
|
-
AppConfig.class_variable_get(:@@storage)
|
47
|
-
.instance_variable_set(:@id, nil)
|
48
|
-
|
49
|
-
AppConfig.api_key = 'foobar'
|
50
|
-
AppConfig.save!.should be_true
|
51
|
-
|
52
|
-
# HACK: Reset the original id.
|
53
|
-
AppConfig.class_variable_get(:@@storage)
|
54
|
-
.instance_variable_set(:@id, original_id)
|
55
|
-
end
|
56
|
-
|
57
|
-
end
|
@@ -1,59 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe AppConfig::Storage::YAML do
|
4
|
-
|
5
|
-
it 'should have some values' do
|
6
|
-
config_for_yaml
|
7
|
-
AppConfig.api_key.should_not be_nil
|
8
|
-
end
|
9
|
-
|
10
|
-
it 'should raise file not found' do
|
11
|
-
lambda do
|
12
|
-
config_for_yaml(yaml: 'not/a/real/file.yml')
|
13
|
-
end.should raise_error(Errno::ENOENT)
|
14
|
-
end
|
15
|
-
|
16
|
-
it 'saves the new value in memory' do
|
17
|
-
config_for_yaml
|
18
|
-
AppConfig.new_key = 'new value'
|
19
|
-
AppConfig.new_key.should == 'new value'
|
20
|
-
end
|
21
|
-
|
22
|
-
it "requires the use of 'Dir.home'" do
|
23
|
-
unless Dir.respond_to?(:home)
|
24
|
-
fail "Requires 'Dir.home' which is available in Ruby 1.9"
|
25
|
-
end
|
26
|
-
end
|
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
|
37
|
-
|
38
|
-
it 'accepts an :env option' do
|
39
|
-
AppConfig.setup!(yaml: fixture('app_config_env.yml'), env: :production)
|
40
|
-
AppConfig.production.should be_true
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'accepts a String as :env option' do
|
44
|
-
AppConfig.setup!(yaml: fixture('app_config_env.yml'), env: 'production')
|
45
|
-
AppConfig.production.should be_true
|
46
|
-
end
|
47
|
-
|
48
|
-
it 'should reload the data' do
|
49
|
-
config_for_yaml
|
50
|
-
original_api_key = AppConfig.api_key
|
51
|
-
|
52
|
-
# Set to some random value:
|
53
|
-
AppConfig.api_key = "foobar-#{rand(100)}"
|
54
|
-
|
55
|
-
AppConfig.reload!
|
56
|
-
AppConfig.api_key.should == original_api_key
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
data/spec/app_config_spec.rb
DELETED
@@ -1,66 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe AppConfig do
|
4
|
-
|
5
|
-
it 'has a version' do
|
6
|
-
AppConfig::VERSION.should_not be_nil
|
7
|
-
end
|
8
|
-
|
9
|
-
it 'responds to .setup!()' do
|
10
|
-
AppConfig.should respond_to(:setup!)
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'responds to .setup?()' do
|
14
|
-
AppConfig.should respond_to(:setup?)
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'responds to .reset!()' do
|
18
|
-
AppConfig.should respond_to(:reset!)
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'should have to_hash' do
|
22
|
-
config_for_yaml
|
23
|
-
AppConfig.to_hash.class.should == Hash
|
24
|
-
end
|
25
|
-
|
26
|
-
it 'should reset @@storage' do
|
27
|
-
# configure first
|
28
|
-
config_for_yaml
|
29
|
-
# then reset
|
30
|
-
AppConfig.reset!
|
31
|
-
AppConfig.send(:storage).should be_nil
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'to_hash() returns an empty hash if storage not set' do
|
35
|
-
AppConfig.reset!
|
36
|
-
AppConfig.to_hash.should == {}
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'should not be setup' do
|
40
|
-
AppConfig.reset!
|
41
|
-
AppConfig.should_not be_setup
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'should be setup' do
|
45
|
-
config_for_yaml
|
46
|
-
AppConfig.should be_setup
|
47
|
-
end
|
48
|
-
|
49
|
-
it 'returns a Hash on setup' do
|
50
|
-
AppConfig.reset!
|
51
|
-
config = AppConfig.setup! do |c|
|
52
|
-
c.name = 'Dale'
|
53
|
-
c.nick = 'Oshuma'
|
54
|
-
end
|
55
|
-
config.should be_instance_of(Hash)
|
56
|
-
end
|
57
|
-
|
58
|
-
it 'raises NotSetup if .storage is accessed and .setup! has not been called' do
|
59
|
-
AppConfig.remove_class_variable(:@@storage)
|
60
|
-
|
61
|
-
lambda do
|
62
|
-
AppConfig.save!
|
63
|
-
end.should raise_error(AppConfig::Error::NotSetup)
|
64
|
-
end
|
65
|
-
|
66
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
---
|
2
|
-
defaults: &defaults
|
3
|
-
admin_email: 'admin@example.com'
|
4
|
-
api_name: 'Supr Webz 2.0'
|
5
|
-
api_key: 'SUPERAWESOMESERVICE'
|
6
|
-
true_option: true
|
7
|
-
false_option: false
|
8
|
-
|
9
|
-
development:
|
10
|
-
<<: *defaults
|
11
|
-
production: false
|
12
|
-
|
13
|
-
production:
|
14
|
-
<<: *defaults
|
15
|
-
production: true
|
data/spec/spec_helper.rb
DELETED
@@ -1,231 +0,0 @@
|
|
1
|
-
require 'simplecov'
|
2
|
-
SimpleCov.start do
|
3
|
-
add_filter '/spec'
|
4
|
-
end
|
5
|
-
|
6
|
-
require 'rspec'
|
7
|
-
require 'app_config'
|
8
|
-
|
9
|
-
RSpec.configure do |config|
|
10
|
-
include AppConfig
|
11
|
-
|
12
|
-
# Returns the full path to the +name+ fixture file.
|
13
|
-
def fixture(name)
|
14
|
-
File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', name))
|
15
|
-
end
|
16
|
-
|
17
|
-
# AppConfig.setup! wrapper. Accepts a hash of +options+.
|
18
|
-
def config_for(options)
|
19
|
-
AppConfig.reset!
|
20
|
-
AppConfig.setup!(options)
|
21
|
-
end
|
22
|
-
|
23
|
-
# Setup YAML options and pass to config_for().
|
24
|
-
def config_for_yaml(opts = {})
|
25
|
-
path = opts[:yaml] || fixture('app_config.yml')
|
26
|
-
config_for({ yaml: path }.merge(opts))
|
27
|
-
end
|
28
|
-
|
29
|
-
def config_for_mongo(load_test_data = true, drop_collection = true, opts = {})
|
30
|
-
mongo = AppConfig::Storage::Mongo::DEFAULTS.merge({
|
31
|
-
database: 'app_config_test',
|
32
|
-
})
|
33
|
-
|
34
|
-
begin
|
35
|
-
load_mongo_test_config(mongo) if load_test_data
|
36
|
-
config_for({mongo: mongo}.merge(opts))
|
37
|
-
|
38
|
-
if drop_collection
|
39
|
-
collection = AppConfig.class_variable_get(:@@storage)
|
40
|
-
.send(:collection)
|
41
|
-
.drop
|
42
|
-
end
|
43
|
-
rescue Mongo::ConnectionFailure
|
44
|
-
pending "***** Mongo specs require a running MongoDB server *****"
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def config_for_mysql(load_test_data = false, opts = {})
|
49
|
-
mysql = AppConfig::Storage::MySQL::DEFAULTS.merge({
|
50
|
-
database: 'app_config_test'
|
51
|
-
})
|
52
|
-
|
53
|
-
begin
|
54
|
-
load_mysql_test_config(mysql) if load_test_data
|
55
|
-
config_for({mysql: mysql}.merge(opts))
|
56
|
-
rescue Mysql2::Error => e
|
57
|
-
if e.to_s =~ /Can't connect/
|
58
|
-
pending "***** MySQL specs require a running MySQL server *****"
|
59
|
-
else
|
60
|
-
raise e
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
def config_for_postgres(load_test_data = false, opts = {})
|
66
|
-
postgres = AppConfig::Storage::Postgres::DEFAULTS.merge({
|
67
|
-
dbname: 'app_config_test'
|
68
|
-
})
|
69
|
-
|
70
|
-
begin
|
71
|
-
load_postgres_test_config(postgres) if load_test_data
|
72
|
-
config_for({postgres: postgres}.merge(opts))
|
73
|
-
rescue PG::Error => e
|
74
|
-
if e.to_s =~ /could not connect to server/
|
75
|
-
pending "***** Postgres specs require a running PostgreSQL server *****"
|
76
|
-
else
|
77
|
-
# Re-raise the exception, since we only care about connectivity here.
|
78
|
-
raise e
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
def config_for_sqlite(wipe_database = true)
|
84
|
-
Dir.mkdir(File.expand_path('tmp')) unless Dir.exists?(File.expand_path('tmp'))
|
85
|
-
|
86
|
-
database = File.expand_path(File.join('tmp', 'app_config_spec.sqlite3'))
|
87
|
-
|
88
|
-
if wipe_database
|
89
|
-
File.delete(database) if File.exists?(database)
|
90
|
-
|
91
|
-
db = ::SQLite3::Database.new(database)
|
92
|
-
table = AppConfig::Storage::SQLite::DEFAULTS[:table]
|
93
|
-
|
94
|
-
config = ::YAML.load_file(fixture('app_config.yml'))
|
95
|
-
attrs = config.map do |k, v|
|
96
|
-
if v.class == String
|
97
|
-
"#{k} varchar(255)"
|
98
|
-
else
|
99
|
-
"#{k} INTEGER DEFAULT #{v ? 1 : 0}"
|
100
|
-
end
|
101
|
-
end.join(', ')
|
102
|
-
|
103
|
-
create_query = "CREATE TABLE #{table} (id INTEGER PRIMARY KEY, #{attrs})"
|
104
|
-
insert_query = "INSERT INTO #{table} (#{config.keys.join(', ')}) VALUES (#{config.values.map { |v|
|
105
|
-
if v.is_a?(TrueClass) || v.is_a?(FalseClass)
|
106
|
-
# Convert to SQLite boolean INT
|
107
|
-
v ? '1' : '0'
|
108
|
-
else
|
109
|
-
"'#{v}'"
|
110
|
-
end
|
111
|
-
}.join(', ')})"
|
112
|
-
|
113
|
-
db.execute(create_query)
|
114
|
-
db.execute(insert_query)
|
115
|
-
end
|
116
|
-
|
117
|
-
config_for(sqlite: { database: database })
|
118
|
-
end
|
119
|
-
|
120
|
-
private
|
121
|
-
|
122
|
-
def load_mongo_test_config(options)
|
123
|
-
connection = ::Mongo::Connection.new(options[:host], options[:port].to_i)
|
124
|
-
database = connection.db(options[:database])
|
125
|
-
collection = database.collection(options[:collection])
|
126
|
-
test_data = YAML.load_file(fixture('app_config.yml'))
|
127
|
-
|
128
|
-
data = collection.find_one
|
129
|
-
if data
|
130
|
-
collection.update({'_id' => data['_id']}, test_data)
|
131
|
-
else
|
132
|
-
collection.save(test_data)
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
def load_mysql_test_config(options)
|
137
|
-
original_options = options.dup
|
138
|
-
|
139
|
-
options.delete(:username) if options[:username].nil?
|
140
|
-
options.delete(:password) if options[:password].nil?
|
141
|
-
|
142
|
-
table = options.delete(:table)
|
143
|
-
|
144
|
-
begin
|
145
|
-
config = ::YAML.load_file(fixture('app_config.yml'))
|
146
|
-
attrs = config.map do |k, v|
|
147
|
-
if v.is_a?(String)
|
148
|
-
"#{k} VARCHAR(255)"
|
149
|
-
elsif v.is_a?(TrueClass) || v.is_a?(FalseClass)
|
150
|
-
"#{k} BOOLEAN"
|
151
|
-
end
|
152
|
-
end.join(', ')
|
153
|
-
|
154
|
-
values = config.values.map do |v|
|
155
|
-
if v.is_a?(TrueClass) || v.is_a?(FalseClass)
|
156
|
-
# Boolean types shouldn't be quoted.
|
157
|
-
"#{v}"
|
158
|
-
else
|
159
|
-
# But VARCHAR types should be.
|
160
|
-
"'#{v}'"
|
161
|
-
end
|
162
|
-
end.join(', ')
|
163
|
-
|
164
|
-
create_query = "CREATE TABLE #{table} (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, #{attrs});"
|
165
|
-
insert_query = "INSERT INTO #{table} (#{config.keys.join(', ')}) VALUES (#{values});"
|
166
|
-
|
167
|
-
client = Mysql2::Client.new(options)
|
168
|
-
client.query("USE #{options[:database]};")
|
169
|
-
|
170
|
-
begin
|
171
|
-
client.query(create_query)
|
172
|
-
rescue Mysql2::Error => e
|
173
|
-
case e.to_s
|
174
|
-
when /Table '#{table}' already exists/
|
175
|
-
# no-op
|
176
|
-
else
|
177
|
-
raise e
|
178
|
-
end
|
179
|
-
end
|
180
|
-
|
181
|
-
client.query(insert_query)
|
182
|
-
rescue Mysql2::Error => e
|
183
|
-
case e.to_s
|
184
|
-
when /Unknown database/
|
185
|
-
Mysql2::Client.new.query("CREATE DATABASE #{options[:database]};")
|
186
|
-
load_mysql_test_config(original_options)
|
187
|
-
else
|
188
|
-
raise e
|
189
|
-
end
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
def load_postgres_test_config(options)
|
194
|
-
original_options = options.dup
|
195
|
-
|
196
|
-
options.delete(:user) if options[:user].nil?
|
197
|
-
options.delete(:password) if options[:password].nil?
|
198
|
-
|
199
|
-
table = options.delete(:table)
|
200
|
-
|
201
|
-
begin
|
202
|
-
connection = ::PG.connect(options)
|
203
|
-
|
204
|
-
config = ::YAML.load_file(fixture('app_config.yml'))
|
205
|
-
attrs = config.map do |k, v|
|
206
|
-
if v.class == String
|
207
|
-
"#{k} character varying(255)"
|
208
|
-
else
|
209
|
-
"#{k} boolean DEFAULT #{v}"
|
210
|
-
end
|
211
|
-
end.join(', ')
|
212
|
-
|
213
|
-
create_query = "CREATE TABLE #{table} (id bigserial primary key, #{attrs})"
|
214
|
-
insert_query = "INSERT INTO #{table} (#{config.keys.join(', ')}) VALUES (#{config.values.map { |v| "'#{v}'" }.join(', ')})"
|
215
|
-
|
216
|
-
connection.exec(create_query)
|
217
|
-
connection.exec(insert_query)
|
218
|
-
rescue PG::Error => e
|
219
|
-
case e.to_s
|
220
|
-
when /database "#{options[:dbname]}" does not exist/
|
221
|
-
%x[createdb -U `whoami` -O `whoami` #{options[:dbname]}]
|
222
|
-
load_postgres_test_config(original_options)
|
223
|
-
when /relation "#{table}" already exists/
|
224
|
-
# no-op
|
225
|
-
else
|
226
|
-
raise e
|
227
|
-
end
|
228
|
-
end
|
229
|
-
end
|
230
|
-
|
231
|
-
end
|