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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 85532501e786eca705ac6224a529a1f1ea596b1f
4
- data.tar.gz: cf1e1d838361c7bca2135db8274a52c2f9cef843
3
+ metadata.gz: 318df4378067f6ec5f0e70c42ab7af32ee235acf
4
+ data.tar.gz: 931193b4ba2fa09e79c0fd0772598cbde0deba45
5
5
  SHA512:
6
- metadata.gz: 7ef79b6d4670050d2d583599d54889c2e219e71e7a8833b39f22a062a842fa919eaa06a8d74f4642548f3db3b8797cd016d66658a6ce31c0970b501ea506c9a2
7
- data.tar.gz: e39497d9599c666bc615ff3ab3b669c977ce5b141cf6dff6506d54cbe50e7a3b21170a7776ace29ed5bd2439a340c10cecbdd6e3bcc2f6955dec5c6aa000fc5b
6
+ metadata.gz: ef84fdbb0dfa7c9deab6351d0a54eaa7f60c2c0758d49fd17c78ab5d6070762a17e6e93204a4c46ff1b79f1813623df549f5d594dd46d3c36aca1086a1794461
7
+ data.tar.gz: 7376e88e227b05ea545515109edff9426ab5d6f92702d1f799e04211bc66b5607f9be917c11587c8595653d29195799082fe7a0b63c86e135441e0bc8d64a58f
@@ -1,8 +1,9 @@
1
1
  module AppConfig
2
2
  module Storage
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'
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.marshal_dump : Hash.new
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
@@ -56,7 +56,7 @@ module AppConfig
56
56
  def fetch_data!
57
57
  raise 'Not connected to MongoDB' unless connected?
58
58
 
59
- @data = OpenStruct.new(collection.find_one)
59
+ @data = Storage::ConfigData.new(collection.find_one)
60
60
  @_id = @data._id
61
61
  end
62
62
 
@@ -32,16 +32,23 @@ module AppConfig
32
32
  fetch_data!
33
33
  end
34
34
 
35
- # Saves the data back to Postgres. Returns `true`/`false`.
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
- 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}"
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(update_query)
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.each do |row|
60
- @data = OpenStruct.new(row)
61
- @id = @data.id
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
@@ -17,7 +17,7 @@ module AppConfig
17
17
  path = DEFAULT_PATH if path == true
18
18
 
19
19
  # Make sure to use the top-level YAML module here.
20
- @data = OpenStruct.new(::YAML.load_file(path))
20
+ @data = Storage::ConfigData.new(::YAML.load_file(path))
21
21
  end
22
22
 
23
23
  end # YAML
@@ -1,3 +1,3 @@
1
1
  module AppConfig
2
- VERSION = '2.1.0'
2
+ VERSION = '2.1.1'
3
3
  end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe AppConfig::Storage::ConfigData do
4
+
5
+ it 'has a #to_hash method' do
6
+ @data = Storage::ConfigData.new({foo: 'bar'})
7
+ @data.to_hash.should == {foo: 'bar' }
8
+ end
9
+
10
+ end
@@ -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
- # Hack to use a test database as the 'default'.
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
- .instance_variable_get(:@options)
39
- .should == AppConfig::Storage::Postgres::DEFAULTS
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(opts = {}, load_test_data = true)
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.0
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-04 00:00:00.000000000 Z
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