app_config 2.1.0 → 2.1.1
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/storage.rb +5 -4
- data/lib/app_config/storage/base.rb +2 -1
- data/lib/app_config/storage/config_data.rb +17 -0
- data/lib/app_config/storage/mongo.rb +1 -1
- data/lib/app_config/storage/postgres.rb +21 -9
- data/lib/app_config/storage/yaml.rb +1 -1
- data/lib/app_config/version.rb +1 -1
- data/spec/app_config/storage/config_data_spec.rb +10 -0
- data/spec/app_config/storage/postgres_spec.rb +25 -4
- data/spec/spec_helper.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 318df4378067f6ec5f0e70c42ab7af32ee235acf
|
4
|
+
data.tar.gz: 931193b4ba2fa09e79c0fd0772598cbde0deba45
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef84fdbb0dfa7c9deab6351d0a54eaa7f60c2c0758d49fd17c78ab5d6070762a17e6e93204a4c46ff1b79f1813623df549f5d594dd46d3c36aca1086a1794461
|
7
|
+
data.tar.gz: 7376e88e227b05ea545515109edff9426ab5d6f92702d1f799e04211bc66b5607f9be917c11587c8595653d29195799082fe7a0b63c86e135441e0bc8d64a58f
|
data/lib/app_config/storage.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
module AppConfig
|
2
2
|
module Storage
|
3
|
-
autoload :Base,
|
4
|
-
autoload :
|
5
|
-
autoload :
|
6
|
-
autoload :
|
3
|
+
autoload :Base, 'app_config/storage/base'
|
4
|
+
autoload :ConfigData, 'app_config/storage/config_data'
|
5
|
+
autoload :Mongo, 'app_config/storage/mongo'
|
6
|
+
autoload :Postgres, 'app_config/storage/postgres'
|
7
|
+
autoload :YAML, 'app_config/storage/yaml'
|
7
8
|
end
|
8
9
|
end
|
@@ -3,10 +3,11 @@ module AppConfig
|
|
3
3
|
class Base
|
4
4
|
|
5
5
|
def initialize
|
6
|
+
@data = Storage::ConfigData.new
|
6
7
|
end
|
7
8
|
|
8
9
|
def to_hash
|
9
|
-
defined?(@data) ? @data.
|
10
|
+
defined?(@data) ? @data.to_hash : Hash.new
|
10
11
|
end
|
11
12
|
|
12
13
|
# Wrap `method_missing` to proxy to `@data`.
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module AppConfig
|
2
|
+
module Storage
|
3
|
+
# OpenStruct wrapper to hold the underlying `Storage` data.
|
4
|
+
class ConfigData < OpenStruct
|
5
|
+
|
6
|
+
# Accepts a Hash and passes that along to `OpenStruct.new`.
|
7
|
+
def initialize(hash = {})
|
8
|
+
super(hash)
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_hash
|
12
|
+
marshal_dump
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -32,16 +32,23 @@ module AppConfig
|
|
32
32
|
fetch_data!
|
33
33
|
end
|
34
34
|
|
35
|
-
# Saves the data
|
35
|
+
# Saves the data to Postgres. Returns `true`/`false`.
|
36
36
|
def save!
|
37
37
|
# Build the `SET foo = 'bar', ...` string for the UPDATE query.
|
38
38
|
data_hash = @data.to_h
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
39
|
+
# Remove the primary key (id) from the SET attributes.
|
40
|
+
data_hash.delete(:id)
|
41
|
+
|
42
|
+
if @id # Updating existing values.
|
43
|
+
set_attrs = data_hash.map { |k, v| "#{k} = '#{v}'" }.join(', ')
|
44
|
+
save_query = "UPDATE #{@table} SET #{set_attrs} WHERE id = #{@id}"
|
45
|
+
else # Creating a new row.
|
46
|
+
columns = data_hash.keys.join(', ')
|
47
|
+
values = data_hash.map { |_, v| "'#{v}'" }.join(', ')
|
48
|
+
save_query = "INSERT INTO #{@table} (#{columns}) VALUES (#{values})"
|
49
|
+
end
|
43
50
|
|
44
|
-
result = @connection.exec(
|
51
|
+
result = @connection.exec(save_query)
|
45
52
|
result.result_status == PG::Constants::PGRES_COMMAND_OK
|
46
53
|
end
|
47
54
|
|
@@ -56,9 +63,14 @@ module AppConfig
|
|
56
63
|
fetch_query = "SELECT * FROM #{@table} ORDER BY id DESC LIMIT 1"
|
57
64
|
|
58
65
|
@connection.exec(fetch_query) do |result|
|
59
|
-
result.
|
60
|
-
@data =
|
61
|
-
|
66
|
+
if result.num_tuples == 0
|
67
|
+
@data = Storage::ConfigData.new
|
68
|
+
else
|
69
|
+
# TODO: Looping here is kinda pointless if we're just resetting `@data` on each pass.
|
70
|
+
result.each do |row|
|
71
|
+
@data = Storage::ConfigData.new(row)
|
72
|
+
@id = @data.id
|
73
|
+
end
|
62
74
|
end
|
63
75
|
end
|
64
76
|
end
|
data/lib/app_config/version.rb
CHANGED
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe AppConfig::Storage::Postgres do
|
4
4
|
|
5
5
|
before(:all) do
|
6
|
-
config_for_postgres
|
6
|
+
config_for_postgres(true)
|
7
7
|
end
|
8
8
|
|
9
9
|
it 'should have some values' do
|
@@ -25,7 +25,8 @@ describe AppConfig::Storage::Postgres do
|
|
25
25
|
it "uses the defaults when 'true' is passed" do
|
26
26
|
AppConfig.reset!
|
27
27
|
|
28
|
-
#
|
28
|
+
# HACK: Use a test database as the 'default'.
|
29
|
+
old_dbname = AppConfig::Storage::Postgres::DEFAULTS[:dbname]
|
29
30
|
AppConfig::Storage::Postgres::DEFAULTS[:dbname] = 'app_config_test'
|
30
31
|
|
31
32
|
begin
|
@@ -35,7 +36,27 @@ describe AppConfig::Storage::Postgres do
|
|
35
36
|
end
|
36
37
|
|
37
38
|
AppConfig.class_variable_get(:@@storage)
|
38
|
-
|
39
|
-
|
39
|
+
.instance_variable_get(:@options)
|
40
|
+
.should == AppConfig::Storage::Postgres::DEFAULTS
|
41
|
+
|
42
|
+
# HACK: Reset dbname default to original value.
|
43
|
+
AppConfig::Storage::Postgres::DEFAULTS[:dbname] = old_dbname
|
40
44
|
end
|
45
|
+
|
46
|
+
it "should create a new row if @id is not set" do
|
47
|
+
# HACK: Save the old id so we can reset it.
|
48
|
+
original_id = AppConfig.class_variable_get(:@@storage)
|
49
|
+
.instance_variable_get(:@id)
|
50
|
+
|
51
|
+
AppConfig.class_variable_get(:@@storage)
|
52
|
+
.instance_variable_set(:@id, nil)
|
53
|
+
|
54
|
+
AppConfig.api_key = 'foobar'
|
55
|
+
AppConfig.save!.should be_true
|
56
|
+
|
57
|
+
# HACK: Reset the original id.
|
58
|
+
AppConfig.class_variable_get(:@@storage)
|
59
|
+
.instance_variable_set(:@id, original_id)
|
60
|
+
end
|
61
|
+
|
41
62
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -38,7 +38,7 @@ RSpec.configure do |config|
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
-
def config_for_postgres(
|
41
|
+
def config_for_postgres(load_test_data = false, opts = {})
|
42
42
|
postgres = AppConfig::Storage::Postgres::DEFAULTS.merge({
|
43
43
|
dbname: 'app_config_test'
|
44
44
|
})
|
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.1.
|
4
|
+
version: 2.1.1
|
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-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bson_ext
|
@@ -143,10 +143,12 @@ files:
|
|
143
143
|
- lib/app_config/error.rb
|
144
144
|
- lib/app_config/storage.rb
|
145
145
|
- lib/app_config/storage/base.rb
|
146
|
+
- lib/app_config/storage/config_data.rb
|
146
147
|
- lib/app_config/storage/mongo.rb
|
147
148
|
- lib/app_config/storage/postgres.rb
|
148
149
|
- lib/app_config/storage/yaml.rb
|
149
150
|
- lib/app_config/version.rb
|
151
|
+
- spec/app_config/storage/config_data_spec.rb
|
150
152
|
- spec/app_config/storage/mongo_spec.rb
|
151
153
|
- spec/app_config/storage/postgres_spec.rb
|
152
154
|
- spec/app_config/storage/yaml_spec.rb
|
@@ -180,6 +182,7 @@ signing_key:
|
|
180
182
|
specification_version: 4
|
181
183
|
summary: Quick and easy application configuration.
|
182
184
|
test_files:
|
185
|
+
- spec/app_config/storage/config_data_spec.rb
|
183
186
|
- spec/app_config/storage/mongo_spec.rb
|
184
187
|
- spec/app_config/storage/postgres_spec.rb
|
185
188
|
- spec/app_config/storage/yaml_spec.rb
|