app_config 2.1.3 → 2.3.0

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: 35fd2897702d6f84752be6616c399de404e77e2d
4
- data.tar.gz: 65d8a105d4b606ea1edc931b839360ea04fd6216
3
+ metadata.gz: 34fdaa8f69598900e31d96ed27f715ad9fd0f77c
4
+ data.tar.gz: 5c4d38a1fd7e58262467b8bd1e8f3f34d04ca6ee
5
5
  SHA512:
6
- metadata.gz: fb77e2f64fd7379620af5cc23fa8d72a7c9d7cf401a86f40514d124243879aeee9446224673cd4bc6b270e3af2b9d368ae9b628ed5f398cc4d71d0fd722acc2e
7
- data.tar.gz: e24fa2a2197fbfbb8c413a1ec3e46139d812797368876a8d4ab42aee5cc0c69b8f6eaf37b13b65aa5ca59888b428f1d4f658684572569520adc765271e1a7c34
6
+ metadata.gz: 0404e1ef35e4cb6d26c75e546c6a3cdbf42a31a4477be011195b42e68be6fc88f985b1dfec77fae1bf8808992a082d4a710b2d7db6744d118a7dd5a32c6d8213
7
+ data.tar.gz: 17a69ff742b2ca5233a8ecf87b1c734a9d96da3280550f18fc743b009495af85e4a8ad07cc87a0ebce3273015b444d1a68800a868afb1bbb5352eb3db03f5119
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- app_config (2.1.3)
4
+ app_config (2.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/app_config.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
10
10
  s.homepage = 'http://oshuma.github.io/app_config'
11
11
 
12
12
  s.summary = %q{Quick and easy application configuration.}
13
- s.description = %q{An easy to use, customizable library to easily store and retrieve application configuration.}
13
+ s.description = %q{An easy to use, framework agnostic, customizable library to easily store and retrieve application configuration.}
14
14
 
15
15
  s.add_development_dependency 'bson_ext'
16
16
  s.add_development_dependency 'mongo'
data/lib/app_config.rb CHANGED
@@ -13,6 +13,7 @@ module AppConfig
13
13
  #
14
14
  # Valid storage methods:
15
15
  # * `:mongo` - {AppConfig::Storage::Mongo AppConfig::Storage::Mongo}
16
+ # * `:postgres` - {AppConfig::Storage::Mongo AppConfig::Storage::Postgres}
16
17
  # * `:yaml` - {AppConfig::Storage::YAML AppConfig::Storage::YAML}
17
18
  def setup!(options = {}, &block)
18
19
  @@options = options
@@ -37,6 +38,15 @@ module AppConfig
37
38
  defined?(@@storage) && !@@storage.nil?
38
39
  end
39
40
 
41
+ # Reload the data from storage.
42
+ def reload!
43
+ if storage.respond_to?(:reload!)
44
+ storage.reload!
45
+ else
46
+ raise AppConfig::Error::MustOverride.new("#{storage.class}#reload!")
47
+ end
48
+ end
49
+
40
50
  # Clears the `@@storage`.
41
51
  def reset!
42
52
  @@storage = nil
@@ -55,7 +65,11 @@ module AppConfig
55
65
 
56
66
  # Returns the `@@storage` contents, which is what is exposed as the configuration.
57
67
  def storage
58
- @@storage
68
+ begin
69
+ @@storage
70
+ rescue NameError # @@storage does not exist
71
+ raise AppConfig::Error::NotSetup unless setup?
72
+ end
59
73
  end
60
74
 
61
75
  end # self
@@ -27,6 +27,10 @@ module AppConfig
27
27
  fetch_data!
28
28
  end
29
29
 
30
+ def reload!
31
+ fetch_data!
32
+ end
33
+
30
34
  # Saves the data back to Mongo. Returns `true`/`false`.
31
35
  def save!
32
36
  if @_id
@@ -32,6 +32,10 @@ module AppConfig
32
32
  fetch_data!
33
33
  end
34
34
 
35
+ def reload!
36
+ fetch_data!
37
+ end
38
+
35
39
  # Saves the data to Postgres. Returns `true`/`false`.
36
40
  def save!
37
41
  # Build the `SET foo = 'bar', ...` string for the UPDATE query.
@@ -43,17 +47,39 @@ module AppConfig
43
47
  set_attrs = data_hash.map { |k, v| "#{k} = '#{v}'" }.join(', ')
44
48
  save_query = "UPDATE #{@table} SET #{set_attrs} WHERE id = #{@id}"
45
49
  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})"
50
+ if data_hash.empty?
51
+ save_query = "INSERT INTO #{@table} DEFAULT VALUES"
52
+ else
53
+ columns = data_hash.keys.join(', ')
54
+ values = data_hash.map { |_, v| "'#{v}'" }.join(', ')
55
+ save_query = "INSERT INTO #{@table} (#{columns}) VALUES (#{values})"
56
+ end
49
57
  end
50
58
 
51
59
  result = @connection.exec(save_query)
52
- result.result_status == PG::Constants::PGRES_COMMAND_OK
60
+
61
+ if result.result_status == PG::Constants::PGRES_COMMAND_OK
62
+ # Initial write (no rows exist), so we have to set @id.
63
+ if result.cmd_status.split[0] == 'INSERT'
64
+ @connection.exec("SELECT id FROM #{@table}") do |result|
65
+ result.each { |row| @id = row['id'] }
66
+ end
67
+
68
+ fetch_data!
69
+ end
70
+
71
+ true
72
+ else
73
+ false
74
+ end
53
75
  end
54
76
 
55
77
  private
56
78
 
79
+ def connected?
80
+ @connection && @connection.status == PG::Constants::CONNECTION_OK
81
+ end
82
+
57
83
  def fetch_data!
58
84
  raise 'Not connected to PostgreSQL' unless connected?
59
85
 
@@ -66,8 +92,11 @@ module AppConfig
66
92
  if result.num_tuples == 0
67
93
  @data = Storage::ConfigData.new
68
94
  else
69
- # TODO: Looping here is kinda pointless if we're just resetting `@data` on each pass.
70
95
  result.each do |row|
96
+ # Convert Postgres booleans into Ruby objects.
97
+ row.each { |k, v| row[k] = true if v == 't' }
98
+ row.each { |k, v| row[k] = false if v == 'f' }
99
+
71
100
  @data = Storage::ConfigData.new(row)
72
101
  @id = @data.id
73
102
  end
@@ -75,10 +104,6 @@ module AppConfig
75
104
  end
76
105
  end
77
106
 
78
- def connected?
79
- @connection && @connection.status == PG::Constants::CONNECTION_OK
80
- end
81
-
82
107
  def setup_connection!
83
108
  @connection = PG.connect(@options)
84
109
  end
@@ -1,3 +1,3 @@
1
1
  module AppConfig
2
- VERSION = '2.1.3'
2
+ VERSION = '2.3.0'
3
3
  end
@@ -31,4 +31,17 @@ describe AppConfig::Storage::Mongo do
31
31
  .instance_variable_get(:@options)
32
32
  .should == AppConfig::Storage::Mongo::DEFAULTS
33
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
+
34
47
  end
@@ -12,7 +12,10 @@ describe AppConfig::Storage::Postgres do
12
12
 
13
13
  it 'should update the values' do
14
14
  new_api_key = 'SOME_NEW_API_KEY'
15
+ new_admin_email = 'foo@example.com'
16
+
15
17
  AppConfig.api_key = new_api_key
18
+ AppConfig.admin_email = new_admin_email
16
19
 
17
20
  AppConfig.save!.should be_true
18
21
 
@@ -20,6 +23,7 @@ describe AppConfig::Storage::Postgres do
20
23
  config_for_postgres
21
24
 
22
25
  AppConfig.api_key.should == new_api_key
26
+ AppConfig.admin_email.should == new_admin_email
23
27
  end
24
28
 
25
29
  it "uses the defaults when 'true' is passed" do
@@ -59,4 +63,19 @@ describe AppConfig::Storage::Postgres do
59
63
  .instance_variable_set(:@id, original_id)
60
64
  end
61
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
+
62
81
  end
@@ -55,4 +55,12 @@ describe AppConfig do
55
55
  config.should be_instance_of(Hash)
56
56
  end
57
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
+
58
66
  end
@@ -2,3 +2,5 @@
2
2
  admin_email: 'admin@example.com'
3
3
  api_name: 'Supr Webz 2.0'
4
4
  api_key: 'SUPERAWESOMESERVICE'
5
+ true_option: true
6
+ false_option: false
data/spec/spec_helper.rb CHANGED
@@ -26,7 +26,7 @@ RSpec.configure do |config|
26
26
  config_for({ yaml: path }.merge(opts))
27
27
  end
28
28
 
29
- def config_for_mongo(opts = {}, load_test_data = true)
29
+ def config_for_mongo(load_test_data = true, drop_collection = true, opts = {})
30
30
  mongo = AppConfig::Storage::Mongo::DEFAULTS.merge({
31
31
  database: 'app_config_test',
32
32
  })
@@ -34,9 +34,11 @@ RSpec.configure do |config|
34
34
  load_mongo_test_config(mongo) if load_test_data
35
35
  config_for({mongo: mongo}.merge(opts))
36
36
 
37
- collection = AppConfig.class_variable_get(:@@storage)
38
- .send(:collection)
39
- .drop
37
+ if drop_collection
38
+ collection = AppConfig.class_variable_get(:@@storage)
39
+ .send(:collection)
40
+ .drop
41
+ end
40
42
  rescue Mongo::ConnectionFailure
41
43
  pending "***** Mongo specs require a running MongoDB server *****"
42
44
  end
@@ -66,7 +68,7 @@ RSpec.configure do |config|
66
68
  connection = ::Mongo::Connection.new(options[:host], options[:port].to_i)
67
69
  database = connection.db(options[:database])
68
70
  collection = database.collection(options[:collection])
69
- test_data = YAML.load_file(fixture('app_config.yml'))
71
+ test_data = YAML.load_file(fixture('app_config.yml'))
70
72
 
71
73
  data = collection.find_one
72
74
  if data
@@ -88,7 +90,13 @@ RSpec.configure do |config|
88
90
  connection = ::PG.connect(options)
89
91
 
90
92
  config = ::YAML.load_file(fixture('app_config.yml'))
91
- attrs = config.keys.map { |k| "#{k} character varying(255)" }.join(', ')
93
+ attrs = config.map do |k, v|
94
+ if v.class == String
95
+ "#{k} character varying(255)"
96
+ else
97
+ "#{k} boolean DEFAULT #{v}"
98
+ end
99
+ end.join(', ')
92
100
 
93
101
  create_query = "CREATE TABLE #{table} (id bigserial primary key, #{attrs})"
94
102
  insert_query = "INSERT INTO #{table} (#{config.keys.join(', ')}) VALUES (#{config.values.map { |v| "'#{v}'" }.join(', ')})"
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.3
4
+ version: 2.3.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-05 00:00:00.000000000 Z
11
+ date: 2013-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bson_ext
@@ -122,8 +122,8 @@ dependencies:
122
122
  - - '>='
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
- description: An easy to use, customizable library to easily store and retrieve application
126
- configuration.
125
+ description: An easy to use, framework agnostic, customizable library to easily store
126
+ and retrieve application configuration.
127
127
  email:
128
128
  - oshuma@gmail.com
129
129
  executables: []