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 CHANGED
@@ -1,4 +1,4 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
3
  # Dependencies in .gemspec
4
4
  gemspec
data/Gemfile.lock CHANGED
@@ -1,10 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- app_config (1.0.1)
4
+ app_config (1.0.3)
5
5
 
6
6
  GEM
7
- remote: http://rubygems.org/
7
+ remote: https://rubygems.org/
8
8
  specs:
9
9
  bson (1.3.1)
10
10
  bson_ext (1.3.1)
data/README.md CHANGED
@@ -3,6 +3,7 @@
3
3
  An easy to use, customizable library to easily store and retrieve application
4
4
  (or library) configuration, API keys or basically anything in 'key/value' pairs.
5
5
 
6
+ AppConfig requires at least Ruby 1.9.
6
7
 
7
8
  ## Usage
8
9
 
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.2'
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 = AppConfig::Storage::Memory.new(@@options)
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? ? storage.to_hash : Hashish.new
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
@@ -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
@@ -7,7 +7,7 @@ module AppConfig
7
7
  end
8
8
 
9
9
  def to_hash
10
- Hashish.new(@data.to_hash)
10
+ defined?(@data) ? @data.to_hash : Hash.new(&Storage::DEEP_HASH)
11
11
  end
12
12
 
13
13
  end # Base
@@ -60,7 +60,10 @@ module AppConfig
60
60
 
61
61
  def fetch_data!
62
62
  raise 'Not connected to MongoDB' unless connected?
63
+
63
64
  @data = Hashish.new(collection.find_one)
65
+ @data.default_proc = Storage::DEEP_HASH
66
+
64
67
  @_id = @data.delete('_id')
65
68
  end
66
69
 
@@ -6,12 +6,12 @@ module AppConfig
6
6
  # YAML storage method.
7
7
  class YAML < Storage::Base
8
8
 
9
- DEFAULT_PATH = File.expand_path(File.join(ENV['HOME'], '.app_config.yml'))
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 `$HOME/.app_config.yml`
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))
@@ -1,4 +1,4 @@
1
- # Stolen from Rails Active Support and renamed to Hashish.
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']`
@@ -19,7 +19,7 @@ describe AppConfig::Storage::Mongo do
19
19
  end
20
20
 
21
21
  it 'should not have the Mongo _id in storage' do
22
- AppConfig['_id'].should be_nil
22
+ AppConfig['_id'].should be_empty
23
23
  end
24
24
 
25
25
  it 'should have a @_id variable for the Mongo ID' do
@@ -19,4 +19,10 @@ describe AppConfig::Storage::YAML do
19
19
  AppConfig[:new_key].should == 'new value'
20
20
  end
21
21
 
22
+ it "requires the use of 'Dir.home'" do
23
+ unless Dir.respond_to?(:home)
24
+ fail "Requires 'Dir.home' which is available in Ruby 1.9"
25
+ end
26
+ end
27
+
22
28
  end
@@ -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 == Hashish
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 be_nil
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 Hashish on setup' do
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(Hashish)
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.2
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: 2012-08-27 00:00:00.000000000 Z
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