app_config 2.3.1 → 2.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 089295fe6570693687189c8bf48b762741ed4a19
4
- data.tar.gz: bd621ae9f22c26f2486d6a4e6b905a8289e48089
3
+ metadata.gz: 61b54a9e60063869d1fb5dd8195685012c09d27a
4
+ data.tar.gz: abe0121e7d221f4510548c5e5d2517f2bf0a1fed
5
5
  SHA512:
6
- metadata.gz: 6d19ce9e2ff7a7d95061b09ff821d82c433c855ac6cb16d5374f8f1f530e77ae0d2d74f836723c95f3948bc346b81280f556fd19adaf39218557f683a9cc67d4
7
- data.tar.gz: 4f862e5befb0fe94604d4a69386b9f11059f55778b363b724acc5d840b342b3019dab0303d90ca91a460d1fcd22d18ad434b92435b01fa7bf853ac09c66083ec
6
+ metadata.gz: a430884e1eaeee82e0ec768a5fbdd745604c7b8589be4210a697fa1f6fb4237138ea88b74a93194484ce81da2b08c4611db3aaf7feb14e1bc7ce44264a73c292
7
+ data.tar.gz: 3b0b28c1a51c0f37d884f63d9f4591365a9305222048794b83e03ee4d6f379bd286c7b73db8e9f91a8ff951b5ca17f8738cf7b73ce756239e0dc98fb313af08f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- app_config (2.3.1)
4
+ app_config (2.4.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -27,6 +27,7 @@ GEM
27
27
  multi_json (~> 1.0)
28
28
  simplecov-html (~> 0.5.3)
29
29
  simplecov-html (0.5.3)
30
+ sqlite3 (1.3.7)
30
31
  yard (0.8.2)
31
32
 
32
33
  PLATFORMS
@@ -41,4 +42,5 @@ DEPENDENCIES
41
42
  redcarpet
42
43
  rspec
43
44
  simplecov
45
+ sqlite3
44
46
  yard
data/README.md CHANGED
@@ -49,6 +49,22 @@ AppConfig.api_name # => 'Supr Webz 2.0'
49
49
  AppConfig.api_key # => 'SUPERAWESOMESERVICE'
50
50
  ```
51
51
 
52
+ You can also use environments in the YAML file (like Rails `database.yml`):
53
+
54
+ ```yaml
55
+ ---
56
+ development:
57
+ api_url: 'http://localhost:3000/endpoint.json'
58
+
59
+ production:
60
+ api_url: 'http://api.example.com/endpoint.json'
61
+ ```
62
+
63
+ ```ruby
64
+ AppConfig.setup!(yaml: '/path/to/app_config.yml', env: :development)
65
+ AppConfig.api_url # => 'http://localhost:3000/endpoint.json'
66
+ ```
67
+
52
68
 
53
69
  ## Mongo
54
70
 
data/app_config.gemspec CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
21
21
  s.add_development_dependency 'redcarpet'
22
22
  s.add_development_dependency 'rspec'
23
23
  s.add_development_dependency 'simplecov'
24
+ s.add_development_dependency 'sqlite3'
24
25
  s.add_development_dependency 'yard'
25
26
 
26
27
  s.has_rdoc = true
data/lib/app_config.rb CHANGED
@@ -14,16 +14,19 @@ module AppConfig
14
14
  # Valid storage methods:
15
15
  # * `:mongo` - {AppConfig::Storage::Mongo AppConfig::Storage::Mongo}
16
16
  # * `:postgres` - {AppConfig::Storage::Mongo AppConfig::Storage::Postgres}
17
+ # * `:sqlite` - {AppConfig::Storage::Mongo AppConfig::Storage::SQLite}
17
18
  # * `:yaml` - {AppConfig::Storage::YAML AppConfig::Storage::YAML}
18
19
  def setup!(options = {}, &block)
19
20
  @@options = options
20
21
 
21
22
  if @@options[:yaml]
22
- @@storage = AppConfig::Storage::YAML.new(@@options.delete(:yaml))
23
+ @@storage = AppConfig::Storage::YAML.new(@@options.delete(:yaml), @@options)
23
24
  elsif @@options[:mongo]
24
25
  @@storage = AppConfig::Storage::Mongo.new(@@options.delete(:mongo))
25
26
  elsif @@options[:postgres]
26
27
  @@storage = AppConfig::Storage::Postgres.new(@@options.delete(:postgres))
28
+ elsif @@options[:sqlite]
29
+ @@storage = AppConfig::Storage::SQLite.new(@@options.delete(:sqlite))
27
30
  else
28
31
  @@storage = AppConfig::Storage::Base.new
29
32
  end
@@ -4,6 +4,7 @@ module AppConfig
4
4
  autoload :ConfigData, 'app_config/storage/config_data'
5
5
  autoload :Mongo, 'app_config/storage/mongo'
6
6
  autoload :Postgres, 'app_config/storage/postgres'
7
+ autoload :SQLite, 'app_config/storage/sqlite'
7
8
  autoload :YAML, 'app_config/storage/yaml'
8
9
  end
9
10
  end
@@ -0,0 +1,53 @@
1
+ module AppConfig
2
+ module Storage
3
+
4
+ require 'sqlite3'
5
+
6
+ # SQLite storage method.
7
+ class SQLite < Storage::Base
8
+
9
+ DEFAULTS = {
10
+ database: File.join(Dir.home, '.app_config.sqlite3'),
11
+ table: 'app_config',
12
+ }
13
+
14
+ def initialize(options)
15
+ # Allows passing `true` as an option to just use defaults.
16
+ if options.is_a?(Hash)
17
+ @options = DEFAULTS.merge(options)
18
+ else
19
+ @options = DEFAULTS
20
+ end
21
+
22
+ @database = ::SQLite3::Database.new(@options[:database])
23
+ @table = @options[:table]
24
+
25
+ fetch_data!
26
+ end
27
+
28
+ private
29
+
30
+ def fetch_data!
31
+ config = {}
32
+
33
+ # Get the column names in same order as schema.
34
+ columns = []
35
+ table_info = "PRAGMA table_info('#{@table}')"
36
+ @database.execute(table_info) do |row|
37
+ columns << row[1]
38
+ end
39
+
40
+ # Get the values in order of columns.
41
+ fetch_query = "SELECT #{columns.join(', ')} FROM #{@table} ORDER BY id DESC LIMIT 1"
42
+ @database.execute(fetch_query) do |row|
43
+ columns.each_with_index do |attr, i|
44
+ config[attr.to_sym] = row[i]
45
+ end
46
+ end
47
+
48
+ @data = Storage::ConfigData.new(config)
49
+ end
50
+
51
+ end
52
+ end
53
+ end
@@ -12,12 +12,17 @@ module AppConfig
12
12
  # `@data` will be the OpenStruct that is accessed with `AppConfig.some_var`.
13
13
  #
14
14
  # Defaults to `Dir.home/.app_config.yml`
15
- def initialize(path = DEFAULT_PATH)
15
+ def initialize(path = DEFAULT_PATH, options = {})
16
16
  # Allows passing `true` as an option.
17
17
  path = DEFAULT_PATH if path == true
18
18
 
19
19
  # Make sure to use the top-level YAML module here.
20
- @data = Storage::ConfigData.new(::YAML.load_file(path))
20
+ if options.has_key?(:env)
21
+ env = options[:env].to_s # Force a String here since YAML's keys are strings.
22
+ @data = Storage::ConfigData.new(::YAML.load_file(path)[env])
23
+ else
24
+ @data = Storage::ConfigData.new(::YAML.load_file(path))
25
+ end
21
26
  end
22
27
 
23
28
  end # YAML
@@ -1,3 +1,3 @@
1
1
  module AppConfig
2
- VERSION = '2.3.1'
2
+ VERSION = '2.4.1'
3
3
  end
@@ -0,0 +1,13 @@
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
+ end
@@ -34,4 +34,15 @@ describe AppConfig::Storage::YAML do
34
34
  AppConfig.setup!(yaml: true)
35
35
  AppConfig.api_key.should_not be_nil
36
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
+
37
48
  end
@@ -0,0 +1,15 @@
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 CHANGED
@@ -62,6 +62,43 @@ RSpec.configure do |config|
62
62
  end
63
63
  end
64
64
 
65
+ def config_for_sqlite(wipe_database = true)
66
+ Dir.mkdir(File.expand_path('tmp')) unless Dir.exists?(File.expand_path('tmp'))
67
+
68
+ database = File.expand_path(File.join('tmp', 'app_config_spec.sqlite3'))
69
+
70
+ if wipe_database
71
+ File.delete(database) if File.exists?(database)
72
+
73
+ db = ::SQLite3::Database.new(database)
74
+ table = AppConfig::Storage::SQLite::DEFAULTS[:table]
75
+
76
+ config = ::YAML.load_file(fixture('app_config.yml'))
77
+ attrs = config.map do |k, v|
78
+ if v.class == String
79
+ "#{k} varchar(255)"
80
+ else
81
+ "#{k} INTEGER DEFAULT #{v ? 1 : 0}"
82
+ end
83
+ end.join(', ')
84
+
85
+ create_query = "CREATE TABLE #{table} (id INTEGER PRIMARY KEY, #{attrs})"
86
+ insert_query = "INSERT INTO #{table} (#{config.keys.join(', ')}) VALUES (#{config.values.map { |v|
87
+ if v.is_a?(TrueClass) || v.is_a?(FalseClass)
88
+ # Convert to SQLite boolean INT
89
+ v ? '1' : '0'
90
+ else
91
+ "'#{v}'"
92
+ end
93
+ }.join(', ')})"
94
+
95
+ db.execute(create_query)
96
+ db.execute(insert_query)
97
+ end
98
+
99
+ config_for(sqlite: { database: database })
100
+ end
101
+
65
102
  private
66
103
 
67
104
  def load_mongo_test_config(options)
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.3.1
4
+ version: 2.4.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-25 00:00:00.000000000 Z
11
+ date: 2013-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bson_ext
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - '>='
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: yard
113
127
  requirement: !ruby/object:Gem::Requirement
@@ -146,15 +160,18 @@ files:
146
160
  - lib/app_config/storage/config_data.rb
147
161
  - lib/app_config/storage/mongo.rb
148
162
  - lib/app_config/storage/postgres.rb
163
+ - lib/app_config/storage/sqlite.rb
149
164
  - lib/app_config/storage/yaml.rb
150
165
  - lib/app_config/version.rb
151
166
  - spec/app_config/storage/config_data_spec.rb
152
167
  - spec/app_config/storage/mongo_spec.rb
153
168
  - spec/app_config/storage/postgres_spec.rb
169
+ - spec/app_config/storage/sqlite_spec.rb
154
170
  - spec/app_config/storage/yaml_spec.rb
155
171
  - spec/app_config/storage_spec.rb
156
172
  - spec/app_config_spec.rb
157
173
  - spec/fixtures/app_config.yml
174
+ - spec/fixtures/app_config_env.yml
158
175
  - spec/spec_helper.rb
159
176
  homepage: http://oshuma.github.io/app_config
160
177
  licenses:
@@ -186,9 +203,11 @@ test_files:
186
203
  - spec/app_config/storage/config_data_spec.rb
187
204
  - spec/app_config/storage/mongo_spec.rb
188
205
  - spec/app_config/storage/postgres_spec.rb
206
+ - spec/app_config/storage/sqlite_spec.rb
189
207
  - spec/app_config/storage/yaml_spec.rb
190
208
  - spec/app_config/storage_spec.rb
191
209
  - spec/app_config_spec.rb
192
210
  - spec/fixtures/app_config.yml
211
+ - spec/fixtures/app_config_env.yml
193
212
  - spec/spec_helper.rb
194
213
  has_rdoc: true