mongo-sugar 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/LICENSE +20 -0
- data/README.md +51 -0
- data/lib/mongo-sugar.rb +8 -0
- data/lib/mongo-sugar/collection.rb +50 -0
- data/lib/mongo-sugar/connection.rb +33 -0
- data/lib/mongo-sugar/connection/config.rb +29 -0
- data/lib/mongo-sugar/connection/manager.rb +50 -0
- data/lib/mongo-sugar/version.rb +6 -0
- data/spec/fixtures/config.yml +4 -0
- data/spec/functional/collection.rb +53 -0
- data/spec/spec_helper.rb +11 -0
- metadata +92 -0
data/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright © 2012 Endel Dreyer
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
Mongo Sugar [](http://travis-ci.org/endel/mongo-sugar)
|
|
2
|
+
===
|
|
3
|
+
|
|
4
|
+
Provide a syntatic sugar for MongoDB ruby driver, similar to an ORM.
|
|
5
|
+
|
|
6
|
+
But it isn't another ORM. If you need relationship features, you should use
|
|
7
|
+
[MongoID](http://mongoid.org/), [MongoMapper](http://mongomapper.com/), or, why
|
|
8
|
+
not - a relational database.
|
|
9
|
+
|
|
10
|
+
Usage
|
|
11
|
+
===
|
|
12
|
+
|
|
13
|
+
Add mongo-sugar to your gemfile:
|
|
14
|
+
|
|
15
|
+
gem 'mongo-sugar'
|
|
16
|
+
|
|
17
|
+
Require it on your code:
|
|
18
|
+
|
|
19
|
+
require 'mongo-sugar'
|
|
20
|
+
|
|
21
|
+
Define classes as collections. By default, collection names are the class name
|
|
22
|
+
underscored. On the following example, the collection name is `my_collection`.
|
|
23
|
+
|
|
24
|
+
class MyCollection < Mongo::Sugar::Collection
|
|
25
|
+
#
|
|
26
|
+
# Define your custom methods, like this:
|
|
27
|
+
#
|
|
28
|
+
def do_upsert(hash)
|
|
29
|
+
@collection.update({
|
|
30
|
+
key: hash[:key]
|
|
31
|
+
}, {
|
|
32
|
+
:$addToSet => {
|
|
33
|
+
:value => hash[:value]
|
|
34
|
+
}
|
|
35
|
+
}, :upsert => true)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
You can customize the collection names using `store_in`, which is similar to mongoid
|
|
40
|
+
syntax.
|
|
41
|
+
|
|
42
|
+
class MyCollection < Mongo::Sugar::Collection
|
|
43
|
+
store_in :collection => 'custom_collection'
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
For more details, read the [documentation](http://endel.github.com/mongo-sugar) here.
|
|
47
|
+
|
|
48
|
+
License
|
|
49
|
+
===
|
|
50
|
+
|
|
51
|
+
This library is released under MIT License. Please see LICENSE file.
|
data/lib/mongo-sugar.rb
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
require 'singleton'
|
|
2
|
+
require 'delegate'
|
|
3
|
+
|
|
4
|
+
module Mongo
|
|
5
|
+
module Sugar
|
|
6
|
+
class Collection < Delegator
|
|
7
|
+
include Singleton
|
|
8
|
+
attr_reader :database
|
|
9
|
+
|
|
10
|
+
#
|
|
11
|
+
# Class methods
|
|
12
|
+
#
|
|
13
|
+
|
|
14
|
+
class << self
|
|
15
|
+
attr_reader :store_in_options
|
|
16
|
+
alias :collection :instance
|
|
17
|
+
|
|
18
|
+
def database
|
|
19
|
+
self.instance.database
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def store_in(options)
|
|
23
|
+
@store_in_options = options
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
#
|
|
28
|
+
# Instance methods
|
|
29
|
+
#
|
|
30
|
+
|
|
31
|
+
def initialize(store_in={})
|
|
32
|
+
__setobj__(self.class.store_in_options || {})
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def __setobj__(store_in_options)
|
|
36
|
+
conn = store_in_options.delete(:connection) || 'default'
|
|
37
|
+
db = store_in_options.delete(:database)
|
|
38
|
+
coll = store_in_options.delete(:collection) || self.class.name.delete(':').gsub(/(.)([A-Z])/,'\1_\2').downcase
|
|
39
|
+
|
|
40
|
+
@database = Mongo::Sugar::Connection::Manager.database(conn, db)
|
|
41
|
+
@collection = @database[coll]
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def __getobj__
|
|
45
|
+
@collection
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
require 'mongo'
|
|
2
|
+
|
|
3
|
+
module Mongo
|
|
4
|
+
module Sugar
|
|
5
|
+
module Connection
|
|
6
|
+
autoload :Config, 'mongo-sugar/connection/config'
|
|
7
|
+
autoload :Manager, 'mongo-sugar/connection/manager'
|
|
8
|
+
|
|
9
|
+
class Connection
|
|
10
|
+
attr_reader :conn, :default_db
|
|
11
|
+
|
|
12
|
+
def initialize(uri)
|
|
13
|
+
raise "Connection#initialize: you must create a config/mongodb.yml file, or set ENV['MONGODB_URI']." if uri.nil?
|
|
14
|
+
@conn = Mongo::Connection.from_uri(uri)
|
|
15
|
+
|
|
16
|
+
# If URI doens't contain database part, get default connection's database name
|
|
17
|
+
default_db = File.basename(uri)
|
|
18
|
+
@default_db = default_db.index(':') ? @conn.db.name : default_db
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Get database reference from connection
|
|
22
|
+
#
|
|
23
|
+
# @param [String] database name
|
|
24
|
+
#
|
|
25
|
+
# @return [Mongo::DB] datbase reference
|
|
26
|
+
def database(name=nil)
|
|
27
|
+
@conn.db(name || @default_db)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require 'yaml'
|
|
2
|
+
|
|
3
|
+
module Mongo
|
|
4
|
+
module Sugar
|
|
5
|
+
module Connection
|
|
6
|
+
|
|
7
|
+
#
|
|
8
|
+
# Config file reader
|
|
9
|
+
#
|
|
10
|
+
class Config
|
|
11
|
+
include Singleton
|
|
12
|
+
|
|
13
|
+
def initialize(config=ENV['MONGODB_CONFIG'], environment=ENV['RACK_ENV'])
|
|
14
|
+
config ||= 'config/mongo.yml'
|
|
15
|
+
@config = (File.exists?(config)) ? YAML.load_file(config)[environment] : {}
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Retrieve a configuration key
|
|
19
|
+
# @param [String] key
|
|
20
|
+
# @return [String] database uri
|
|
21
|
+
def get(name)
|
|
22
|
+
@config[name]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
require 'singleton'
|
|
2
|
+
|
|
3
|
+
module Mongo
|
|
4
|
+
module Sugar
|
|
5
|
+
module Connection
|
|
6
|
+
|
|
7
|
+
#
|
|
8
|
+
# Manages multiple database connections
|
|
9
|
+
#
|
|
10
|
+
class Manager
|
|
11
|
+
include Singleton
|
|
12
|
+
attr_reader :connections
|
|
13
|
+
|
|
14
|
+
def initialize
|
|
15
|
+
@connections = {}
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Create MongoDB connection
|
|
19
|
+
#
|
|
20
|
+
# @param [String] connection name
|
|
21
|
+
# @param [String] connection URI (optional. default: ENV['MONGODB_URI'])
|
|
22
|
+
#
|
|
23
|
+
# @return [Mongo::Sugar::Connection] connection instance
|
|
24
|
+
def self.create(name, uri=nil)
|
|
25
|
+
self.instance.connections[name] = Connection.new(Config.instance.get(name) || ENV['MONGODB_URI'])
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Get MongoDB connection
|
|
29
|
+
#
|
|
30
|
+
# @param [String] connection name
|
|
31
|
+
#
|
|
32
|
+
# @return [Mongo::Connection, nil] connection or nil, if connection doesn't have been created
|
|
33
|
+
def self.get(name)
|
|
34
|
+
self.instance.connections[name] or self.create(name)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Get database instance
|
|
38
|
+
#
|
|
39
|
+
# @param [String] connection name
|
|
40
|
+
# @param [String] database to connect. (optional, inherit from your database URI, if specified)
|
|
41
|
+
#
|
|
42
|
+
# return [Mongo::DB] database instance
|
|
43
|
+
def self.database(name, database=nil)
|
|
44
|
+
self.get(name).database(database)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Mongo::Sugar::Collection do
|
|
4
|
+
subject { Mongo::Sugar::Collection }
|
|
5
|
+
|
|
6
|
+
before(:all) do
|
|
7
|
+
ENV['MONGODB_URI'] = 'mongodb://localhost:27017/mongo_sugar_test'
|
|
8
|
+
ENV['MONGODB_CONFIG'] = 'spec/fixtures/config.yml'
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
context "defining collections" do
|
|
12
|
+
|
|
13
|
+
it "without #store_in call" do
|
|
14
|
+
class DummyCollection < subject; end
|
|
15
|
+
DummyCollection.collection.name.should == 'dummy_collection'
|
|
16
|
+
DummyCollection.database.name.should == 'mongo_sugar_test'
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "change database and collection with #store_in call" do
|
|
20
|
+
class CustomCollection < subject
|
|
21
|
+
store_in :collection => 'custom'
|
|
22
|
+
end
|
|
23
|
+
CustomCollection.collection.name.should == 'custom'
|
|
24
|
+
CustomCollection.database.name.should == 'mongo_sugar_test'
|
|
25
|
+
|
|
26
|
+
class CustomDatabaseCollection < subject
|
|
27
|
+
store_in :collection => 'custom', :database => 'mongo_sugar_two'
|
|
28
|
+
end
|
|
29
|
+
CustomDatabaseCollection.collection.name.should == 'custom'
|
|
30
|
+
CustomDatabaseCollection.database.name.should == 'mongo_sugar_two'
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it "has @collection visible for custom methods" do
|
|
34
|
+
class IHateFooExamples < subject
|
|
35
|
+
store_in :collection => 'bazinga_bar'
|
|
36
|
+
def respond_to_find?
|
|
37
|
+
@collection.respond_to?(:find)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
IHateFooExamples.collection.name.should == 'bazinga_bar'
|
|
41
|
+
IHateFooExamples.collection.respond_to_find?.should == true
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
it "specifying another connection" do
|
|
45
|
+
class AnotherConnection < subject
|
|
46
|
+
store_in :collection => "another", :connection => 'second'
|
|
47
|
+
end
|
|
48
|
+
AnotherConnection.collection.name.should == 'another'
|
|
49
|
+
AnotherConnection.database.name.should == 'mongo_sugar_test_2'
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: mongo-sugar
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: '0.1'
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Endel Dreyer
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2012-12-01 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: mongo
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ! '>='
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '1.1'
|
|
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: '1.1'
|
|
30
|
+
- !ruby/object:Gem::Dependency
|
|
31
|
+
name: bson_ext
|
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
|
33
|
+
none: false
|
|
34
|
+
requirements:
|
|
35
|
+
- - ! '>='
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: '1.1'
|
|
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: '1.1'
|
|
46
|
+
description: Provide a syntatic sugar for MongoDB ruby driver, similar to an ORM.
|
|
47
|
+
email:
|
|
48
|
+
- endel.dreyer@gmail.com
|
|
49
|
+
executables: []
|
|
50
|
+
extensions: []
|
|
51
|
+
extra_rdoc_files: []
|
|
52
|
+
files:
|
|
53
|
+
- lib/mongo-sugar/collection.rb
|
|
54
|
+
- lib/mongo-sugar/connection/config.rb
|
|
55
|
+
- lib/mongo-sugar/connection/manager.rb
|
|
56
|
+
- lib/mongo-sugar/connection.rb
|
|
57
|
+
- lib/mongo-sugar/version.rb
|
|
58
|
+
- lib/mongo-sugar.rb
|
|
59
|
+
- LICENSE
|
|
60
|
+
- README.md
|
|
61
|
+
- spec/fixtures/config.yml
|
|
62
|
+
- spec/functional/collection.rb
|
|
63
|
+
- spec/spec_helper.rb
|
|
64
|
+
homepage: https://github.com/endel/mongo-sugar
|
|
65
|
+
licenses: []
|
|
66
|
+
post_install_message:
|
|
67
|
+
rdoc_options: []
|
|
68
|
+
require_paths:
|
|
69
|
+
- lib
|
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
71
|
+
none: false
|
|
72
|
+
requirements:
|
|
73
|
+
- - ! '>='
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '0'
|
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
|
+
none: false
|
|
78
|
+
requirements:
|
|
79
|
+
- - ! '>='
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '0'
|
|
82
|
+
requirements: []
|
|
83
|
+
rubyforge_project:
|
|
84
|
+
rubygems_version: 1.8.24
|
|
85
|
+
signing_key:
|
|
86
|
+
specification_version: 3
|
|
87
|
+
summary: Sugar Syntatic for Official MongoDB Driver
|
|
88
|
+
test_files:
|
|
89
|
+
- spec/fixtures/config.yml
|
|
90
|
+
- spec/functional/collection.rb
|
|
91
|
+
- spec/spec_helper.rb
|
|
92
|
+
has_rdoc:
|