app_config 0.5.3 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,12 +9,6 @@ An easy to use, customizable library to easily store and retrieve application
9
9
  Usage is simple. Just pass either a hash of options, or a block, to
10
10
  AppConfig.setup. See AppConfig::Base for a list of valid storage methods.
11
11
 
12
- As of version 0.4.1, if the <tt>:storage_method</tt> is not set, AppConfig
13
- will act pretty much just like a normal Hash:
14
-
15
- AppConfig.setup(:email => 'admin@example.com')
16
- AppConfig[:email] # => 'admin@example.com'
17
-
18
12
 
19
13
  == AppConfig::Storage::YAML
20
14
 
@@ -43,6 +37,37 @@ Use it like so:
43
37
  AppConfig[:api_key] # => 'SUPERAWESOMESERVICE'
44
38
 
45
39
 
40
+ == AppConfig::Storage::Mongo
41
+
42
+ As of version 0.6.0, you can now use <tt>:mongo</tt> as the storage method.
43
+ The values are read/saved (by default) to the 'app_config' database and
44
+ 'app_config' collection. These defaults can be overridden, however, which
45
+ might lend well to versioned configurations; collection names such as
46
+ 'app_config_v1', 'app_config_v2', etc. Check the <tt>DEFAULTS</tt> constant
47
+ in AppConfig::Storage::Mongo for the default Mongo connection options.
48
+
49
+ AppConfig.setup do |config|
50
+ config[:storage_method] = :mongo
51
+ config[:collection] = 'app_config_v2' # Or any valid Mongo collection name.
52
+
53
+ # Override an existing value:
54
+ config[:admin_email] = 'other_admin@example.com'
55
+ end
56
+
57
+ If you wanted to read/update the configuration from the <tt>mongo</tt> client,
58
+ it would look something like this:
59
+
60
+ $ mongo
61
+ MongoDB shell version: 1.6.4
62
+ connecting to: test
63
+ > use app_config
64
+ switched to db app_config
65
+ > db.app_config.find()
66
+ { "_id" : ObjectId("4cddc317da98dd42f8000001"), "admin_email" : "admin@example.com" }
67
+ > db.app_config_v2.find()
68
+ { "_id" : ObjectId("4cddc317da98dd42f8000001"), "admin_email" : "other_admin@example.com" }
69
+
70
+
46
71
  == AppConfig::Storage::Sqlite
47
72
 
48
73
  AppConfig.setup do |config|
@@ -57,6 +82,15 @@ Use it like so:
57
82
  AppConfig[:column] # => 'value'
58
83
 
59
84
 
85
+ == Basically just a Hash...
86
+
87
+ As of version 0.4.1, if the <tt>:storage_method</tt> is not set, AppConfig
88
+ will act pretty much just like a normal Hash:
89
+
90
+ AppConfig.setup(:email => 'admin@example.com')
91
+ AppConfig[:email] # => 'admin@example.com'
92
+
93
+
60
94
  == Environment Mode
61
95
 
62
96
  As of version 0.4.0, there's an 'environment mode' where you can organize
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift File.dirname(__FILE__)
4
4
  require 'core_ext/hashish'
5
5
 
6
6
  module AppConfig
7
- VERSION = '0.5.3'
7
+ VERSION = '0.6.0'
8
8
 
9
9
  autoload :Base, 'app_config/base'
10
10
  autoload :Error, 'app_config/error'
@@ -48,7 +48,7 @@ module AppConfig
48
48
  private
49
49
 
50
50
  def validate!
51
- raise AppConfig::Error::NotSetup unless defined?(@@storage) && @@storage
51
+ raise AppConfig::Error::NotSetup unless setup?
52
52
  end
53
53
 
54
54
  end # self
@@ -9,6 +9,7 @@ module AppConfig
9
9
  #
10
10
  # Valid storage methods:
11
11
  # * :memory (AppConfig::Storage::Memory)
12
+ # * :mongo (AppConfig::Storage::Mongo)
12
13
  # * :sqlite (AppConfig::Storage::Sqlite)
13
14
  # * :yaml (AppConfig::Storage::YAML)
14
15
  class Base
@@ -76,11 +77,14 @@ module AppConfig
76
77
  end
77
78
 
78
79
  # This decides how to load the data, based on the +storage_method+.
79
- # TODO: Maybe purge AppConfig options (ie, those not related to the user-end).
80
+ # TODO: Purge AppConfig options (ie, those not related to the user-end).
81
+ # TODO: This should return an instance of the storage method, instead of calling Storage::Base.load().
80
82
  def initialize_storage
81
83
  case @options[:storage_method]
82
84
  when :memory
83
85
  AppConfig::Storage::Memory.load(@options)
86
+ when :mongo
87
+ AppConfig::Storage::Mongo.new(@options)
84
88
  when :sqlite
85
89
  AppConfig::Storage::Sqlite.load(@options)
86
90
  when :yaml
@@ -1,6 +1,12 @@
1
1
  module AppConfig
2
2
  module Error
3
3
 
4
+ class MustOverride < Exception
5
+ def initialize(method)
6
+ super("Must override method: #{method}")
7
+ end
8
+ end
9
+
4
10
  class NotSetup < Exception
5
11
  def to_s; "Must call 'AppConfig.setup' to setup storage!"; end
6
12
  end
@@ -9,6 +9,7 @@ module AppConfig
9
9
  module Storage
10
10
 
11
11
  # Mongo storage method.
12
+ # FIXME: Come up with a way of removing stale config entries.
12
13
  class Mongo < Storage::Base
13
14
 
14
15
  DEFAULTS = {
@@ -39,7 +40,11 @@ module AppConfig
39
40
  private
40
41
 
41
42
  def save!
42
- collection.save(@data)
43
+ if @data.has_key?('_id')
44
+ collection.update({'_id' => @data['_id']}, @data)
45
+ else
46
+ collection.save(@data)
47
+ end
43
48
  end
44
49
 
45
50
  def setup_connection
@@ -3,23 +3,16 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
3
3
  describe AppConfig::Storage::Mongo do
4
4
 
5
5
  before(:each) do
6
- pending "TODO: Spec this out"
6
+ AppConfig.reset!
7
+ config_for_mongo
7
8
  end
8
9
 
9
10
  it 'should have some values' do
10
- config_for_mongo
11
11
  AppConfig[:api_key].should_not be_nil
12
12
  end
13
13
 
14
14
  it 'should update the values' do
15
- config_for_mongo
16
- original_key = AppConfig[:api_key]
17
15
  AppConfig[:api_key] = 'SOME_NEW_API_KEY'
18
-
19
- # Reload the data.
20
- AppConfig.reset!
21
- config_for_mongo
22
-
23
16
  AppConfig[:api_key].should == 'SOME_NEW_API_KEY'
24
17
  end
25
18
  end
@@ -0,0 +1,5 @@
1
+ # Test values loaded into the Mongo db.
2
+ ---
3
+ admin_email: 'admin@example.com'
4
+ api_name: 'Supr Webz 2.0'
5
+ api_key: 'SUPERAWESOMESERVICE'
@@ -28,16 +28,32 @@ RSpec.configure do |config|
28
28
  end
29
29
 
30
30
  def config_for_mongo(opts = {})
31
- mongo = {
31
+ mongo = AppConfig::Storage::Mongo::DEFAULTS.merge({
32
32
  :storage_method => :mongo,
33
33
  :host => 'localhost',
34
34
  :database => 'app_config_spec',
35
- :environment => 'test',
36
- }
35
+ })
37
36
  begin
38
37
  config_for(mongo.merge(opts))
38
+ load_mongo_test_config(mongo)
39
39
  rescue Mongo::ConnectionFailure
40
40
  pending "***** Mongo specs require a running MongoDB server *****"
41
41
  end
42
42
  end
43
+
44
+ private
45
+
46
+ def load_mongo_test_config(options)
47
+ connection = ::Mongo::Connection.new(options[:host], options[:port].to_i)
48
+ database = connection.db(options[:database])
49
+ collection = database.collection(options[:collection])
50
+ test_data = YAML.load_file(fixture('app_config_mongo.yml'))
51
+
52
+ data = collection.find_one
53
+ if data
54
+ collection.update({'_id' => data['_id']}, test_data)
55
+ else
56
+ collection.save(test_data)
57
+ end
58
+ end
43
59
  end
@@ -7,7 +7,7 @@ end
7
7
 
8
8
  namespace :doc do
9
9
  task :setup_rdoc do
10
- @file_list = FileList[ "#{File.join(APP_ROOT, 'README')}",
10
+ @file_list = FileList[ "#{File.join(APP_ROOT, 'README.rdoc')}",
11
11
  "#{APP_ROOT}/lib/**/*.rb" ]
12
12
  # Substitute APP_ROOT with a dot. Makes for a better index in the generated docs.
13
13
  @files = @file_list.collect {|f| f.gsub(/#{APP_ROOT}/, '.')}
@@ -15,6 +15,7 @@ namespace :doc do
15
15
  --all
16
16
  --inline-source
17
17
  --line-numbers
18
+ --main README.rdoc
18
19
  --op #{File.join(APP_ROOT, 'doc', 'api')}
19
20
  --title 'AppConfig API Documentation'
20
21
  ]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: app_config
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 5
9
- - 3
10
- version: 0.5.3
8
+ - 6
9
+ - 0
10
+ version: 0.6.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dale Campbell
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-07 00:00:00 -04:00
18
+ date: 2010-11-12 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -60,7 +60,7 @@ extensions: []
60
60
  extra_rdoc_files: []
61
61
 
62
62
  files:
63
- - README
63
+ - README.rdoc
64
64
  - Rakefile
65
65
  - lib/app_config/error.rb
66
66
  - lib/app_config/storage.rb
@@ -73,6 +73,7 @@ files:
73
73
  - lib/app_config.rb
74
74
  - lib/core_ext/hashish.rb
75
75
  - spec/watchr.rb
76
+ - spec/fixtures/app_config_mongo.yml
76
77
  - spec/fixtures/app_config.yml
77
78
  - spec/fixtures/env_app_config.yml
78
79
  - spec/app_config_spec.rb