mongo-configure 0.0.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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ - jruby-18mode
6
+ - jruby-19mode
7
+ - rbx-18mode
8
+ - rbx-19mode
9
+ - ruby-head
10
+ - jruby-head
11
+ - 1.8.7
12
+ - ree
13
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongo-configure.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Jon Rowe
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ [![Build Status](https://secure.travis-ci.org/JonRowe/mongo-configure.png)](http://travis-ci.org/JonRowe/mongo-configure)
2
+ # Mongo::Configure
3
+
4
+ A simple gem for configuring mongo databases.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'mongo-configure'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install mongo-configure
19
+
20
+ ## Usage
21
+
22
+ When you have a Mongo URI:
23
+
24
+ Mongo::Configure.from_uri "mongodb://remote_server.com:27017/production"
25
+
26
+ When you only care about the database name:
27
+
28
+ Mongo::Configure.from_database "test_db"
29
+
30
+ To reaccess a previously configured configuration:
31
+
32
+ Mongo::Configure.current
33
+
34
+ To access a configured database connection:
35
+
36
+ configuration.load
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,37 @@
1
+ require 'morphine'
2
+ require "mongo-configure/version"
3
+ require 'mongo-configure/uri'
4
+ require 'mongo-configure/config'
5
+
6
+ module Mongo
7
+ module Configure
8
+
9
+ class Config
10
+ include Morphine
11
+
12
+ register(:connection) { Connection }
13
+
14
+ def initialize(uri)
15
+ @uri = uri
16
+ end
17
+ attr_reader :uri
18
+ def load
19
+ connection.from_uri(@uri.to_s).db @uri.database
20
+ end
21
+ end
22
+
23
+ class << self
24
+
25
+ def from_uri(uri)
26
+ @config = Config.new URI.parse uri
27
+ end
28
+ def from_database(name)
29
+ @config = Config.new URI.new.tap { |uri| uri.database = name }
30
+ end
31
+ def current
32
+ @config ||= new
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,24 @@
1
+ module Mongo
2
+ module Configure
3
+ class URI < Struct.new(:scheme,:host,:port,:database,:user,:password)
4
+ def initialize(scheme='mongodb',host='localhost',port='27017',database='',user=nil,password=nil)
5
+ super
6
+ end
7
+
8
+ def self.parse(string)
9
+ uri = ::URI.parse string
10
+ new uri.scheme, uri.host, uri.port.to_s, uri.path.gsub(/^\//,''), uri.user, uri.password
11
+ end
12
+ def auth
13
+ if user || password
14
+ [user,password].join(':')+'@'
15
+ else
16
+ ''
17
+ end
18
+ end
19
+ def to_s
20
+ "#{scheme}://#{auth}#{host}:#{port}/#{database}"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ module Mongo
2
+ module Configure
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/mongo-configure/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Jon Rowe"]
6
+ gem.email = ["hello@jonrowe.co.uk"]
7
+ gem.description = %q{A simple gem for configuring mongo databases.}
8
+ gem.summary = %q{A simple gem for configuring mongo databases.}
9
+ gem.homepage = "https://github.com/JonRowe/mongo-configure"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "mongo-configure"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Mongo::Configure::VERSION
17
+
18
+ gem.add_dependency 'morphine'
19
+ gem.add_dependency 'mongo'
20
+ gem.add_development_dependency 'rspec'
21
+ end
@@ -0,0 +1,36 @@
1
+ require 'mongo-configure/uri'
2
+
3
+ describe 'parsing Mongo URIs' do
4
+ describe 'falling back to defaults' do
5
+ subject { Mongo::Configure::URI.new }
6
+ its(:scheme) { should == 'mongodb' }
7
+ its(:host) { should == 'localhost' }
8
+ its(:port) { should == '27017' }
9
+ its(:database) { should == '' }
10
+ its(:user) { should == nil }
11
+ its(:password) { should == nil }
12
+ its(:to_s) { should == 'mongodb://localhost:27017/' }
13
+ end
14
+ describe 'setting elements' do
15
+ subject { Mongo::Configure::URI.new "secretsquirrel",'server.name','666','data','user','s3kr1t' }
16
+ its(:scheme) { should == 'secretsquirrel' }
17
+ its(:host) { should == 'server.name' }
18
+ its(:port) { should == '666' }
19
+ its(:database) { should == 'data' }
20
+ its(:user) { should == 'user' }
21
+ its(:password) { should == 's3kr1t' }
22
+ its(:auth) { should == 'user:s3kr1t@' }
23
+ its(:to_s) { should == 'secretsquirrel://user:s3kr1t@server.name:666/data' }
24
+ end
25
+ describe 'parsing a supplied uri' do
26
+ subject { Mongo::Configure::URI.parse "secretsquirrel://user:s3kr1t@server.name:666/data" }
27
+ its(:scheme) { should == 'secretsquirrel' }
28
+ its(:host) { should == 'server.name' }
29
+ its(:port) { should == '666' }
30
+ its(:database) { should == 'data' }
31
+ its(:user) { should == 'user' }
32
+ its(:password) { should == 's3kr1t' }
33
+ its(:auth) { should == 'user:s3kr1t@' }
34
+ its(:to_s) { should == 'secretsquirrel://user:s3kr1t@server.name:666/data' }
35
+ end
36
+ end
@@ -0,0 +1,66 @@
1
+ require 'mongo-configure'
2
+
3
+ describe 'configuring databases' do
4
+ describe 'from a uri' do
5
+ subject do
6
+ Mongo::Configure.from_uri('secretsquirrel://server.name:666/data').uri
7
+ end
8
+ its(:scheme) { should == 'secretsquirrel' }
9
+ its(:host) { should == 'server.name' }
10
+ its(:port) { should == '666' }
11
+ its(:database) { should == 'data' }
12
+ its(:auth) { should == '' }
13
+ its(:to_s) { should == 'secretsquirrel://server.name:666/data' }
14
+
15
+ context "with authentication" do
16
+ subject do
17
+ Mongo::Configure.from_uri('secretsquirrel://squirrel:s3krit@server.name:666/data').uri
18
+ end
19
+ its(:user) { should == 'squirrel' }
20
+ its(:password) { should == 's3krit' }
21
+ its(:auth) { should == 'squirrel:s3krit@' }
22
+ its(:to_s) { should == 'secretsquirrel://squirrel:s3krit@server.name:666/data' }
23
+ end
24
+ end
25
+
26
+ describe "configuration via database name" do
27
+ subject do
28
+ Mongo::Configure.from_database("test").uri
29
+ end
30
+ its(:scheme) { should == 'mongodb' }
31
+ its(:host) { should == 'localhost' }
32
+ its(:port) { should == '27017' }
33
+ its(:database) { should == 'test' }
34
+ its(:to_s) { should == 'mongodb://localhost:27017/test' }
35
+ end
36
+
37
+ describe 'accessing a configuration' do
38
+ let!(:configuration) { Mongo::Configure.from_database("test") }
39
+ subject { Mongo::Configure.current }
40
+ it { should == configuration }
41
+ end
42
+
43
+ describe 'loading a connection from the configuration' do
44
+ let(:configuration) { Mongo::Configure.current }
45
+ let(:fake_connector) { double from_uri: fake_connection }
46
+ let(:fake_connection) { double db: database }
47
+ let(:database) { double }
48
+
49
+ before do
50
+ Mongo::Configure.from_uri 'secretsquirrel://server.name:666/data'
51
+ configuration.connection = fake_connector
52
+ end
53
+
54
+ subject { configuration.load }
55
+
56
+ it "loads the database from uri" do
57
+ fake_connector.should_receive(:from_uri).with 'secretsquirrel://server.name:666/data'
58
+ subject
59
+ end
60
+ it "gets the database named" do
61
+ fake_connection.should_receive(:db).with "data"
62
+ subject
63
+ end
64
+ it { should == database }
65
+ end
66
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongo-configure
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jon Rowe
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: morphine
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: mongo
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: A simple gem for configuring mongo databases.
63
+ email:
64
+ - hello@jonrowe.co.uk
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .travis.yml
71
+ - Gemfile
72
+ - LICENSE
73
+ - README.md
74
+ - Rakefile
75
+ - lib/mongo-configure.rb
76
+ - lib/mongo-configure/uri.rb
77
+ - lib/mongo-configure/version.rb
78
+ - mongo-configure.gemspec
79
+ - spec/mongo-configure/uri_spec.rb
80
+ - spec/mongo-configure_spec.rb
81
+ homepage: https://github.com/JonRowe/mongo-configure
82
+ licenses: []
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.24
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: A simple gem for configuring mongo databases.
105
+ test_files:
106
+ - spec/mongo-configure/uri_spec.rb
107
+ - spec/mongo-configure_spec.rb