app_config 1.0.2 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/Gemfile.lock +2 -2
- data/README.md +1 -0
- data/lib/app_config.rb +12 -4
- data/lib/app_config/storage.rb +3 -1
- data/lib/app_config/storage/base.rb +1 -1
- data/lib/app_config/storage/mongo.rb +3 -0
- data/lib/app_config/storage/yaml.rb +2 -2
- data/lib/core_ext/hashish.rb +1 -1
- data/spec/app_config/storage/mongo_spec.rb +1 -1
- data/spec/app_config/storage/yaml_spec.rb +6 -0
- data/spec/app_config_spec.rb +12 -7
- metadata +2 -3
- data/lib/app_config/storage/memory.rb +0 -23
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
data/lib/app_config.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'core_ext/hashish'
|
2
2
|
|
3
3
|
module AppConfig
|
4
|
-
VERSION = '1.0
|
4
|
+
VERSION = '1.1.0'
|
5
5
|
|
6
6
|
autoload :Error, 'app_config/error'
|
7
7
|
autoload :Storage, 'app_config/storage'
|
@@ -12,7 +12,6 @@ module AppConfig
|
|
12
12
|
# See each storage method's documentation for their specific options.
|
13
13
|
#
|
14
14
|
# Valid storage methods:
|
15
|
-
# * `:memory` - {AppConfig::Storage::Memory AppConfig::Storage::Memory}
|
16
15
|
# * `:mongo` - {AppConfig::Storage::Mongo AppConfig::Storage::Mongo}
|
17
16
|
# * `:yaml` - {AppConfig::Storage::YAML AppConfig::Storage::YAML}
|
18
17
|
def setup(options = {}, &block)
|
@@ -23,7 +22,7 @@ module AppConfig
|
|
23
22
|
elsif @@options[:mongo]
|
24
23
|
@@storage = AppConfig::Storage::Mongo.new(@@options.delete(:mongo))
|
25
24
|
else
|
26
|
-
@@storage =
|
25
|
+
@@storage = Hash.new(&Storage::DEEP_HASH)
|
27
26
|
end
|
28
27
|
|
29
28
|
yield @@storage if block_given?
|
@@ -63,11 +62,20 @@ module AppConfig
|
|
63
62
|
end
|
64
63
|
|
65
64
|
def to_hash
|
66
|
-
setup?
|
65
|
+
unless setup?
|
66
|
+
@@storage = default_storage
|
67
|
+
end
|
68
|
+
|
69
|
+
storage.to_hash
|
67
70
|
end
|
68
71
|
|
69
72
|
private
|
70
73
|
|
74
|
+
# Returns a nested Hash as a sane default.
|
75
|
+
def default_storage
|
76
|
+
Hash.new(&Storage::DEEP_HASH)
|
77
|
+
end
|
78
|
+
|
71
79
|
def environment
|
72
80
|
(@@options[:environment] || @@options[:env]) || nil
|
73
81
|
end
|
data/lib/app_config/storage.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
module AppConfig
|
2
2
|
module Storage
|
3
|
+
# Used when creating a new, infinitely nested Hash.
|
4
|
+
DEEP_HASH = lambda { |h, k| h[k] = Hash.new(&h.default_proc) }
|
5
|
+
|
3
6
|
autoload :Base, 'app_config/storage/base'
|
4
|
-
autoload :Memory, 'app_config/storage/memory'
|
5
7
|
autoload :Mongo, 'app_config/storage/mongo'
|
6
8
|
autoload :YAML, 'app_config/storage/yaml'
|
7
9
|
end
|
@@ -6,12 +6,12 @@ module AppConfig
|
|
6
6
|
# YAML storage method.
|
7
7
|
class YAML < Storage::Base
|
8
8
|
|
9
|
-
DEFAULT_PATH = File.
|
9
|
+
DEFAULT_PATH = File.join(Dir.home, '.app_config.yml')
|
10
10
|
|
11
11
|
# Loads `@data` with the YAML file located at `path`.
|
12
12
|
# `@data` will be the Hashish that is accessed with `AppConfig[:key]`.
|
13
13
|
#
|
14
|
-
# Defaults to
|
14
|
+
# Defaults to `Dir.home/.app_config.yml`
|
15
15
|
def initialize(path = DEFAULT_PATH)
|
16
16
|
# Make sure to use the top-level YAML module here.
|
17
17
|
@data = Hashish.new(::YAML.load_file(path))
|
data/lib/core_ext/hashish.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Stolen from Rails
|
1
|
+
# Stolen from Rails `ActiveSupport::HashWithIndifferentAccess` and renamed to Hashish.
|
2
2
|
#
|
3
3
|
# This class has dubious semantics and we only have it so that
|
4
4
|
# people can write `params[:key]` instead of `params['key']`
|
data/spec/app_config_spec.rb
CHANGED
@@ -20,7 +20,7 @@ describe AppConfig do
|
|
20
20
|
|
21
21
|
it 'should have to_hash' do
|
22
22
|
config_for_yaml
|
23
|
-
AppConfig.to_hash.class.should ==
|
23
|
+
AppConfig.to_hash.class.should == Hash
|
24
24
|
end
|
25
25
|
|
26
26
|
it 'should reset @@storage' do
|
@@ -28,7 +28,7 @@ describe AppConfig do
|
|
28
28
|
config_for_yaml(:api_key => 'API_KEY')
|
29
29
|
# then reset
|
30
30
|
AppConfig.reset!
|
31
|
-
AppConfig[:api_key].should
|
31
|
+
AppConfig[:api_key].should be_empty
|
32
32
|
end
|
33
33
|
|
34
34
|
it 'to_hash() returns an empty hash if storage not set' do
|
@@ -55,21 +55,26 @@ describe AppConfig do
|
|
55
55
|
end
|
56
56
|
|
57
57
|
it 'should create nested keys' do
|
58
|
-
pending 'Implement nested keys'
|
59
58
|
AppConfig.reset!
|
60
59
|
AppConfig.setup
|
61
60
|
|
62
|
-
AppConfig[:name][:first] = 'Dale'
|
63
|
-
AppConfig[:name][:first].should == 'Dale'
|
61
|
+
AppConfig[:person][:name][:first] = 'Dale'
|
62
|
+
AppConfig[:person][:name][:first].should == 'Dale'
|
64
63
|
end
|
65
64
|
|
66
|
-
it 'returns a
|
65
|
+
it 'returns a Hash on setup' do
|
67
66
|
AppConfig.reset!
|
68
67
|
config = AppConfig.setup do |c|
|
69
68
|
c[:name] = 'Dale'
|
70
69
|
c[:nick] = 'Oshuma'
|
71
70
|
end
|
72
|
-
config.should be_instance_of(
|
71
|
+
config.should be_instance_of(Hash)
|
72
|
+
end
|
73
|
+
|
74
|
+
it '.default_storage() returns a nested Hash' do
|
75
|
+
hash = AppConfig.send(:default_storage)
|
76
|
+
hash[:name][:first] = 'Dale'
|
77
|
+
hash[:name][:first].should == 'Dale'
|
73
78
|
end
|
74
79
|
|
75
80
|
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.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-05-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bson_ext
|
@@ -143,7 +143,6 @@ 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/memory.rb
|
147
146
|
- lib/app_config/storage/mongo.rb
|
148
147
|
- lib/app_config/storage/yaml.rb
|
149
148
|
- lib/core_ext/hashish.rb
|
@@ -1,23 +0,0 @@
|
|
1
|
-
module AppConfig
|
2
|
-
module Storage
|
3
|
-
class Memory < Storage::Base
|
4
|
-
|
5
|
-
def initialize(options)
|
6
|
-
@data = Hashish.new(options)
|
7
|
-
end
|
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
|
-
|
21
|
-
end # Memory
|
22
|
-
end # Storage
|
23
|
-
end # AppConfig
|