app_config 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.md +5 -0
- data/lib/app_config.rb +39 -7
- data/lib/app_config/storage/base.rb +1 -15
- data/lib/app_config/storage/memory.rb +12 -1
- data/lib/app_config/storage/mongo.rb +10 -6
- data/lib/app_config/storage/yaml.rb +16 -0
- data/spec/app_config/storage/mongo_spec.rb +10 -7
- data/spec/app_config_spec.rb +9 -0
- metadata +1 -2
- data/lib/app_config/base.rb +0 -70
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -22,6 +22,11 @@ In it's simplest form, you can use it like so:
|
|
22
22
|
You may also specify the storage method along with options specific to that storage method.
|
23
23
|
Check the [wiki](https://github.com/Oshuma/app_config/wiki) for more usage examples.
|
24
24
|
|
25
|
+
## Deprecation Note
|
26
|
+
|
27
|
+
Version `1.0` is \***not**\* backwards compatible with `0.7.1`! See the [wiki](https://github.com/Oshuma/app_config/wiki)
|
28
|
+
for upgrade instructions.
|
29
|
+
|
25
30
|
|
26
31
|
## AppConfig::Storage::YAML
|
27
32
|
|
data/lib/app_config.rb
CHANGED
@@ -1,18 +1,34 @@
|
|
1
1
|
require 'core_ext/hashish'
|
2
2
|
|
3
3
|
module AppConfig
|
4
|
-
VERSION = '1.0.
|
4
|
+
VERSION = '1.0.1'
|
5
5
|
|
6
|
-
autoload :Base, 'app_config/base'
|
7
6
|
autoload :Error, 'app_config/error'
|
8
7
|
autoload :Storage, 'app_config/storage'
|
9
8
|
|
10
9
|
class << self
|
11
10
|
|
12
11
|
# Accepts an `options` hash or a block.
|
13
|
-
# See
|
12
|
+
# See each storage method's documentation for their specific options.
|
13
|
+
#
|
14
|
+
# Valid storage methods:
|
15
|
+
# * `:memory` - {AppConfig::Storage::Memory AppConfig::Storage::Memory}
|
16
|
+
# * `:mongo` - {AppConfig::Storage::Mongo AppConfig::Storage::Mongo}
|
17
|
+
# * `:yaml` - {AppConfig::Storage::YAML AppConfig::Storage::YAML}
|
14
18
|
def setup(options = {}, &block)
|
15
|
-
@@
|
19
|
+
@@options = options
|
20
|
+
|
21
|
+
if @@options[:yaml]
|
22
|
+
@@storage = AppConfig::Storage::YAML.new(@@options.delete(:yaml))
|
23
|
+
elsif @@options[:mongo]
|
24
|
+
@@storage = AppConfig::Storage::Mongo.new(@@options.delete(:mongo))
|
25
|
+
else
|
26
|
+
@@storage = AppConfig::Storage::Memory.new(@@options)
|
27
|
+
end
|
28
|
+
|
29
|
+
yield @@storage if block_given?
|
30
|
+
|
31
|
+
to_hash
|
16
32
|
end
|
17
33
|
|
18
34
|
# Returns `true` if {AppConfig.setup AppConfig.setup} has been called.
|
@@ -33,17 +49,33 @@ module AppConfig
|
|
33
49
|
# Access the configured `key`'s value.
|
34
50
|
def [](key)
|
35
51
|
setup unless setup?
|
36
|
-
|
52
|
+
storage[key]
|
37
53
|
end
|
38
54
|
|
39
55
|
# Set a new `value` for `key` (persistence depends on the type of Storage).
|
40
56
|
def []=(key, value)
|
41
57
|
setup unless setup?
|
42
|
-
|
58
|
+
storage[key] = value
|
59
|
+
end
|
60
|
+
|
61
|
+
def empty?
|
62
|
+
storage.empty?
|
43
63
|
end
|
44
64
|
|
45
65
|
def to_hash
|
46
|
-
setup? ?
|
66
|
+
setup? ? storage.to_hash : Hashish.new
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
def environment
|
72
|
+
(@@options[:environment] || @@options[:env]) || nil
|
73
|
+
end
|
74
|
+
alias_method :env, :environment
|
75
|
+
|
76
|
+
# Returns the `@@storage` contents, which is what is exposed as the configuration.
|
77
|
+
def storage
|
78
|
+
environment ? @@storage[environment] : @@storage
|
47
79
|
end
|
48
80
|
|
49
81
|
end # self
|
@@ -2,28 +2,14 @@ module AppConfig
|
|
2
2
|
module Storage
|
3
3
|
class Base
|
4
4
|
|
5
|
-
attr_reader :data
|
6
|
-
|
7
5
|
def initialize(options)
|
8
6
|
@options = options
|
9
7
|
end
|
10
8
|
|
11
|
-
def [](key)
|
12
|
-
@data[key]
|
13
|
-
end
|
14
|
-
|
15
|
-
def []=(key, value)
|
16
|
-
@data[key] = value
|
17
|
-
end
|
18
|
-
|
19
|
-
def empty?
|
20
|
-
@data.empty?
|
21
|
-
end
|
22
|
-
|
23
9
|
def to_hash
|
24
10
|
@data.to_hash
|
25
11
|
end
|
26
12
|
|
27
|
-
end #
|
13
|
+
end # Base
|
28
14
|
end # Storage
|
29
15
|
end # AppConfig
|
@@ -3,10 +3,21 @@ module AppConfig
|
|
3
3
|
class Memory < Storage::Base
|
4
4
|
|
5
5
|
def initialize(options)
|
6
|
-
super(options)
|
7
6
|
@data = Hashish.new(options)
|
8
7
|
end
|
9
8
|
|
9
|
+
def [](key)
|
10
|
+
@data[key]
|
11
|
+
end
|
12
|
+
|
13
|
+
def []=(key, value)
|
14
|
+
@data[key] = value
|
15
|
+
end
|
16
|
+
|
17
|
+
def empty?
|
18
|
+
@data.empty?
|
19
|
+
end
|
20
|
+
|
10
21
|
end # Memory
|
11
22
|
end # Storage
|
12
23
|
end # AppConfig
|
@@ -16,11 +16,10 @@ module AppConfig
|
|
16
16
|
}
|
17
17
|
|
18
18
|
def initialize(options)
|
19
|
-
super(DEFAULTS.merge(options))
|
20
19
|
@connected = false
|
21
20
|
@options = DEFAULTS.merge(options)
|
22
21
|
setup_connection
|
23
|
-
|
22
|
+
fetch_data!
|
24
23
|
end
|
25
24
|
|
26
25
|
def [](key)
|
@@ -32,11 +31,15 @@ module AppConfig
|
|
32
31
|
save!
|
33
32
|
end
|
34
33
|
|
34
|
+
def empty?
|
35
|
+
@data.empty?
|
36
|
+
end
|
37
|
+
|
35
38
|
private
|
36
39
|
|
37
40
|
def save!
|
38
|
-
if @
|
39
|
-
collection.update({'_id' => @
|
41
|
+
if @_id
|
42
|
+
collection.update({ '_id' => @_id}, @data)
|
40
43
|
else
|
41
44
|
collection.save(@data)
|
42
45
|
end
|
@@ -55,9 +58,10 @@ module AppConfig
|
|
55
58
|
@connection && @connection.connected?
|
56
59
|
end
|
57
60
|
|
58
|
-
def fetch_data
|
61
|
+
def fetch_data!
|
59
62
|
raise 'Not connected to MongoDB' unless connected?
|
60
|
-
Hashish.new(collection.find_one)
|
63
|
+
@data = Hashish.new(collection.find_one)
|
64
|
+
@_id = @data.delete('_id')
|
61
65
|
end
|
62
66
|
|
63
67
|
def database
|
@@ -17,6 +17,22 @@ module AppConfig
|
|
17
17
|
@data = Hashish.new(::YAML.load_file(path))
|
18
18
|
end
|
19
19
|
|
20
|
+
def [](key)
|
21
|
+
@data[key]
|
22
|
+
end
|
23
|
+
|
24
|
+
def []=(key, value)
|
25
|
+
@data[key] = value
|
26
|
+
end
|
27
|
+
|
28
|
+
def empty?
|
29
|
+
@data.empty?
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_hash
|
33
|
+
@data.to_hash
|
34
|
+
end
|
35
|
+
|
20
36
|
end # YAML
|
21
37
|
end # Storage
|
22
38
|
end # AppConfig
|
@@ -13,14 +13,17 @@ describe AppConfig::Storage::Mongo do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
it 'should update the values' do
|
16
|
-
|
17
|
-
AppConfig.class_variable_get(:@@storage).
|
18
|
-
instance_variable_get(:@storage).should_receive(:save!)
|
16
|
+
AppConfig.class_variable_get(:@@storage).should_receive(:save!)
|
19
17
|
AppConfig[:api_key] = 'SOME_NEW_API_KEY'
|
20
|
-
|
21
|
-
# now reload the config options and check the value
|
22
|
-
AppConfig.reset!
|
23
|
-
config_for_mongo({}, false) # use default options and do not load test data
|
24
18
|
AppConfig[:api_key].should == 'SOME_NEW_API_KEY'
|
25
19
|
end
|
20
|
+
|
21
|
+
it 'should not have the Mongo _id in storage' do
|
22
|
+
AppConfig['_id'].should be_nil
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should have a @_id variable for the Mongo ID' do
|
26
|
+
AppConfig.class_variable_get(:@@storage).
|
27
|
+
instance_variable_get(:@_id).should_not be_nil
|
28
|
+
end
|
26
29
|
end
|
data/spec/app_config_spec.rb
CHANGED
@@ -54,4 +54,13 @@ describe AppConfig do
|
|
54
54
|
AppConfig.should be_setup
|
55
55
|
end
|
56
56
|
|
57
|
+
it 'should create nested keys' do
|
58
|
+
pending 'Implement nested keys'
|
59
|
+
AppConfig.reset!
|
60
|
+
AppConfig.setup
|
61
|
+
|
62
|
+
AppConfig[:name][:first] = 'Dale'
|
63
|
+
AppConfig[:name][:first].should == 'Dale'
|
64
|
+
end
|
65
|
+
|
57
66
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: app_config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -140,7 +140,6 @@ files:
|
|
140
140
|
- app_config.gemspec
|
141
141
|
- doc/.gitignore
|
142
142
|
- lib/app_config.rb
|
143
|
-
- lib/app_config/base.rb
|
144
143
|
- lib/app_config/error.rb
|
145
144
|
- lib/app_config/storage.rb
|
146
145
|
- lib/app_config/storage/base.rb
|
data/lib/app_config/base.rb
DELETED
@@ -1,70 +0,0 @@
|
|
1
|
-
module AppConfig
|
2
|
-
|
3
|
-
# The Base storage class.
|
4
|
-
# Acts as a wrapper for the different storage methods.
|
5
|
-
#
|
6
|
-
# See each storage method's documentation for their specific options.
|
7
|
-
#
|
8
|
-
# Valid storage methods:
|
9
|
-
# * `:memory` - {AppConfig::Storage::Memory AppConfig::Storage::Memory}
|
10
|
-
# * `:mongo` - {AppConfig::Storage::Mongo AppConfig::Storage::Mongo}
|
11
|
-
# * `:yaml` - {AppConfig::Storage::YAML AppConfig::Storage::YAML}
|
12
|
-
class Base
|
13
|
-
|
14
|
-
# Accepts either a hash of `options` or a block (which overrides any options passed in the hash).
|
15
|
-
def initialize(options = {}, &block)
|
16
|
-
@options = options
|
17
|
-
|
18
|
-
if @options[:yaml]
|
19
|
-
@storage = AppConfig::Storage::YAML.new(@options.delete(:yaml))
|
20
|
-
elsif @options[:mongo]
|
21
|
-
@storage = AppConfig::Storage::Mongo.new(@options.delete(:mongo))
|
22
|
-
else
|
23
|
-
@storage = AppConfig::Storage::Memory.new(@options)
|
24
|
-
end
|
25
|
-
|
26
|
-
yield @storage if block_given?
|
27
|
-
end
|
28
|
-
|
29
|
-
# Access the `key`'s value in storage.
|
30
|
-
def [](key)
|
31
|
-
if storage.respond_to?(:[])
|
32
|
-
storage[key]
|
33
|
-
else
|
34
|
-
raise AppConfig::Error::MustOverride.new('#[]')
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def []=(key, value)
|
39
|
-
if storage.respond_to?(:[]=)
|
40
|
-
storage[key] = value
|
41
|
-
else
|
42
|
-
raise AppConfig::Error::MustOverride.new('#[]=')
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def empty?
|
47
|
-
if storage.respond_to?(:empty?)
|
48
|
-
storage.empty?
|
49
|
-
else
|
50
|
-
raise AppConfig::Error::MustOverride.new('#empty?')
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def environment
|
55
|
-
(@options[:environment] || @options[:env]) || nil
|
56
|
-
end
|
57
|
-
alias_method :env, :environment
|
58
|
-
|
59
|
-
# Returns the `@storage` contents, which is what is exposed
|
60
|
-
# as the configuration.
|
61
|
-
def storage
|
62
|
-
environment ? @storage[environment] : @storage
|
63
|
-
end
|
64
|
-
|
65
|
-
def to_hash
|
66
|
-
storage.to_hash
|
67
|
-
end
|
68
|
-
|
69
|
-
end # Base
|
70
|
-
end # AppConfig
|