mongo-document 1.0.0 → 1.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.
- checksums.yaml +4 -4
- data/README.md +73 -0
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7d8d5c567b18d43532542788c170d51e9fe112ca
|
4
|
+
data.tar.gz: 3e8bf1bcc1618cb5bf250ead4fa244799eed72f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d3098da5589bf030da7eedd9b34da6eb2c075aa5fbfb05f7284033741149a7a9dedacc7a538878b43e6a5dcd467e0c30bea1567c9a4ffbe93138534cff9f2fb9
|
7
|
+
data.tar.gz: c248ee5a2c011a59a79c244c87f3c7a48ab781d34b82aa484f086d82b91e3924719130417cc3f00d4242f682b09135ba0293e2e993363988848c2bac190beac4
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# mongo-document
|
2
|
+
A slim Mongo document model for MongoDB!
|
3
|
+
|
4
|
+
# Why do I need it?
|
5
|
+
1. You want something simple yet powerfull - The `mongo` Gem seems fit, but you can't build model class like `Mongoid` does with it.
|
6
|
+
2. However you don't want to turn to Mongoid since it's far away from the simple `mongo`.
|
7
|
+
|
8
|
+
`mongo-document` is just such a tiny helper for `mongo` that enables you to build model class like `Mongoid` does.
|
9
|
+
|
10
|
+
# Installation
|
11
|
+
```
|
12
|
+
gem install mongo-document
|
13
|
+
```
|
14
|
+
|
15
|
+
# Usage
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
require 'mongo/document'
|
19
|
+
|
20
|
+
Mongo::Document.establish_connection(
|
21
|
+
:hosts => ["localhost:27017"],
|
22
|
+
:database => "mongo-document-test",
|
23
|
+
)
|
24
|
+
|
25
|
+
class User
|
26
|
+
include Mongo::Document
|
27
|
+
|
28
|
+
indexes.create_many([
|
29
|
+
{ :key => { :email => 1 }, :unique => true, :sparse => true },
|
30
|
+
{ :key => { :type => 1 }},
|
31
|
+
])
|
32
|
+
end
|
33
|
+
|
34
|
+
User.insert_one(:email => 'a@b.com', :type => 'dev', :name => 'Bob')
|
35
|
+
users = User.find(:email => 'a@b.com')
|
36
|
+
...
|
37
|
+
|
38
|
+
```
|
39
|
+
|
40
|
+
The model class `User` defines a collection named `users`. The mapping from class name to collection name conforms to the behaviour of ActiveRecord. You can override this by explicitly naming the collection like:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
class User
|
44
|
+
include Mongo::Document
|
45
|
+
|
46
|
+
self.collection_name = 'old_users'
|
47
|
+
end
|
48
|
+
|
49
|
+
```
|
50
|
+
|
51
|
+
The model class `User` has all public methods defined in `Mongo::Collection` by `mongo` Gem. In fact the former forwards those method calls to the latter. You use the model class just as a `Mongo::Collection` object!
|
52
|
+
|
53
|
+
Before you call any methods of a `Mongo::Collection` object, you need to call `Mongo::Document.establish_connection` first. It takes a singel hash parameter, in which a `hosts` or a `uri` field must be provided. The `uri` conforms to the MongoDB document: https://docs.mongodb.com/manual/reference/connection-string/. The other fields of the `spec` hash are optional and documented in: https://docs.mongodb.com/ruby-driver/v2.2/tutorials/ruby-driver-create-client/#ruby-driver-client-options
|
54
|
+
|
55
|
+
What's more, if you have a config file `config/database.yml` under the current working directory, it will be read for a call to `Mongo::Document.establish_connection` automatically. That's a Rails/Rack convention. You can disable this behaviour by setting environment variable `MD_NO_AUTO_CONFIG=1`. The format of the file is like:
|
56
|
+
|
57
|
+
```yaml
|
58
|
+
development:
|
59
|
+
hosts:
|
60
|
+
-- localhost:27017
|
61
|
+
database: dev-db
|
62
|
+
|
63
|
+
test:
|
64
|
+
hosts:
|
65
|
+
-- localhost:27017
|
66
|
+
database: test-db
|
67
|
+
```
|
68
|
+
|
69
|
+
The parameters are documented as mentioned above for `Mongo::Document.establish_connection`.
|
70
|
+
|
71
|
+
# Very Light Footprint
|
72
|
+
|
73
|
+
You're encouraged to open the single source file to see how slim the implementation is! Total code is less than 100 lines!
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo-document
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Zhang
|
@@ -24,12 +24,27 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '2.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.0'
|
27
41
|
description:
|
28
42
|
email: louirobert@gmail.com
|
29
43
|
executables: []
|
30
44
|
extensions: []
|
31
45
|
extra_rdoc_files: []
|
32
46
|
files:
|
47
|
+
- README.md
|
33
48
|
- lib/mongo/document.rb
|
34
49
|
homepage: https://github.com/coin8086/mongo-document
|
35
50
|
licenses:
|
@@ -51,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
66
|
version: '0'
|
52
67
|
requirements: []
|
53
68
|
rubyforge_project:
|
54
|
-
rubygems_version: 2.
|
69
|
+
rubygems_version: 2.4.8
|
55
70
|
signing_key:
|
56
71
|
specification_version: 4
|
57
72
|
summary: A slim Mongo document model
|