mongo-document 0.0.0 → 1.0.0
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/lib/mongo/document.rb +99 -0
- metadata +19 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 321421ae3e2f2c78d3c86a04f509894de8ee3365
|
|
4
|
+
data.tar.gz: aa7d497d6c2e69fa0dd318358fd0667fa29cc956
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8680a58b6b74cf10a201e0abe7158aa591f402742fc80b33e015142b3d949e97166d06765352cad759952b122d7c6279e27c45b24ea291251cd61b4fcad9ef3d
|
|
7
|
+
data.tar.gz: 7f5905060f92d6bde8dfa85615afbbd0d2f9c4173811b97d4ccf3eaa9164bb231eaed89c327dd265b5b3748b9fc9072ff9824cbaaca8d4617e5d0d5d3fbb379e
|
data/lib/mongo/document.rb
CHANGED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
require 'mongo'
|
|
2
|
+
require 'yaml'
|
|
3
|
+
require 'active_support'
|
|
4
|
+
require 'active_support/core_ext/string/inflections'
|
|
5
|
+
require 'active_support/core_ext/hash/keys'
|
|
6
|
+
|
|
7
|
+
module Mongo
|
|
8
|
+
module DocumentClass
|
|
9
|
+
def client
|
|
10
|
+
if !@client
|
|
11
|
+
raise "No connection! You should call Mongo::Document.establish_connection before."
|
|
12
|
+
end
|
|
13
|
+
@client
|
|
14
|
+
end
|
|
15
|
+
alias connection client
|
|
16
|
+
|
|
17
|
+
def collection
|
|
18
|
+
@collection ||= client[collection_name]
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def collection_name
|
|
22
|
+
@collection_name ||= name.underscore.pluralize
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def collection_name=(name)
|
|
26
|
+
@collection_name = name
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# A little optimazation for the most frequently-used method.
|
|
30
|
+
def find(*args)
|
|
31
|
+
collection.find(*args)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# For other methods on collection(or not), just send to it.
|
|
35
|
+
def method_missing(name, *args)
|
|
36
|
+
collection.send(name, *args)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def respond_to_missing?(name, include_private = false)
|
|
40
|
+
collection.respond_to?(name, include_private) || super
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
module Document
|
|
46
|
+
class << self
|
|
47
|
+
def included(cls)
|
|
48
|
+
cls.extend DocumentClass
|
|
49
|
+
set_connection(cls)
|
|
50
|
+
descendants << cls
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def descendants
|
|
54
|
+
@descendants ||= []
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def client
|
|
58
|
+
@client
|
|
59
|
+
end
|
|
60
|
+
alias connection client
|
|
61
|
+
|
|
62
|
+
def establish_connection(spec)
|
|
63
|
+
spec = spec.symbolize_keys
|
|
64
|
+
hosts_or_uri = nil
|
|
65
|
+
spec.delete_if do |k, v|
|
|
66
|
+
if k == :hosts || k == :uri
|
|
67
|
+
hosts_or_uri = v
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
@client = Mongo::Client.new(hosts_or_uri, spec)
|
|
71
|
+
|
|
72
|
+
descendants.each do |model|
|
|
73
|
+
set_connection(model)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def database_file=(file)
|
|
78
|
+
config = YAML.load(File.read(file))
|
|
79
|
+
env = ENV['RAILS_ENV'] || ENV['RACK_ENV']
|
|
80
|
+
spec = config[env]
|
|
81
|
+
establish_connection(spec)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
private
|
|
85
|
+
|
|
86
|
+
def set_connection(cls)
|
|
87
|
+
cls.instance_variable_set(:@client, @client) if @client
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Auto read database config
|
|
93
|
+
if !ENV["MD_NO_AUTO_CONFIG"] && File.exist?("#{Dir.pwd}/config/database.yml")
|
|
94
|
+
self.database_file = "#{Dir.pwd}/config/database.yml"
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
end
|
metadata
CHANGED
|
@@ -1,16 +1,30 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mongo-document
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Robert Zhang
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
12
|
-
dependencies:
|
|
13
|
-
|
|
11
|
+
date: 2016-10-04 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: mongo
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '2.2'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '2.2'
|
|
27
|
+
description:
|
|
14
28
|
email: louirobert@gmail.com
|
|
15
29
|
executables: []
|
|
16
30
|
extensions: []
|
|
@@ -37,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
37
51
|
version: '0'
|
|
38
52
|
requirements: []
|
|
39
53
|
rubyforge_project:
|
|
40
|
-
rubygems_version: 2.
|
|
54
|
+
rubygems_version: 2.5.1
|
|
41
55
|
signing_key:
|
|
42
56
|
specification_version: 4
|
|
43
57
|
summary: A slim Mongo document model
|