mongocore 0.2.0 → 0.2.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 +1 -1
- data/lib/mongocore/query.rb +33 -17
- data/lib/mongocore/schema.rb +28 -11
- data/lib/mongocore.rb +1 -1
- data/mongocore.gemspec +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e2b421c9156412c98e0da302d515db0283ba45ba
|
4
|
+
data.tar.gz: a14a5b1d90a91328eab16f8d2e6f083725ab8658
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c95e808a73109dbb45e4fb1538028c6cc453501a1fa90dcbae5ff3d0e2cc6ceb66fd9d0187a238dde05ca82df04e91ee50e438a3de73cc68198aba7eeff360b7
|
7
|
+
data.tar.gz: e904842600c7643bf831745ff487639e0b64b6221092eb650f9b7c8f657d90cbc526bbc5211ba3a75036cde2ae777d9ecf85225af4e53a6ec26940f31948e519
|
data/README.md
CHANGED
@@ -21,7 +21,7 @@ Please read [the source code](https://github.com/fugroup/mongocore/tree/master/l
|
|
21
21
|
| -------------------------------------- | ----- | ------- | ------------- |
|
22
22
|
| [Mongoid](http://mongoid.com) | 256 | 14371 | 10590 |
|
23
23
|
| [MongoMapper](http://mongomapper.com) | 91 | 200 | 4070 |
|
24
|
-
| [Mongocore](http://mongocore.com) | 8 |
|
24
|
+
| [Mongocore](http://mongocore.com) | 8 | 256 | 454 |
|
25
25
|
|
26
26
|
<br>
|
27
27
|
|
data/lib/mongocore/query.rb
CHANGED
@@ -26,7 +26,7 @@ module Mongocore
|
|
26
26
|
|
27
27
|
# Storing query and options. Sort and limit is stored in options
|
28
28
|
s[:sort] ||= {}; s[:limit] ||= 0; s[:chain] ||= []; s[:source] ||= nil; s[:fields] ||= {}; s[:skip] ||= 0
|
29
|
-
@query, @options, @store =
|
29
|
+
@query, @options, @store = ids(transform(hashify(q))), o, s
|
30
30
|
|
31
31
|
# Set up cache
|
32
32
|
@cache = Mongocore::Cache.new(self)
|
@@ -34,22 +34,35 @@ module Mongocore
|
|
34
34
|
|
35
35
|
# Find. Returns a Mongocore::Query
|
36
36
|
def find(q = {}, o = {}, s = {})
|
37
|
-
self.class.new(@model, @query.merge(
|
37
|
+
self.class.new(@model, @query.merge(hashify(q)), @options.merge(o), @store.merge(s))
|
38
38
|
end
|
39
39
|
alias_method :where, :find
|
40
40
|
|
41
|
-
#
|
42
|
-
def
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
# Support passing :id as :_id
|
47
|
-
q[:_id] = q.delete(:id) if q[:id]
|
41
|
+
# Convert string query to hash
|
42
|
+
def hashify(q)
|
43
|
+
q.is_a?(Hash) ? q : {:_id => q}
|
44
|
+
end
|
48
45
|
|
49
|
-
|
50
|
-
|
46
|
+
# Setup query, replace :id with :_id, set up ObjectIds
|
47
|
+
def ids(h)
|
48
|
+
h.each do |k, v|
|
49
|
+
case v
|
50
|
+
when Hash
|
51
|
+
# Call hashes recursively
|
52
|
+
ids(transform(v))
|
53
|
+
when Array
|
54
|
+
# Return mapped array or recurse hashes
|
55
|
+
h[k] = v.map{|r| r.is_a?(Hash) ? ids(transform(r)) : oid(r)}
|
56
|
+
else
|
57
|
+
# Convert to object ID if applicable
|
58
|
+
h[k] = oid(v) if v.is_a?(String)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
51
62
|
|
52
|
-
|
63
|
+
# Transform :id to :_id
|
64
|
+
def transform(h)
|
65
|
+
h.transform_keys!{|k| k == :id ? :_id : k}
|
53
66
|
end
|
54
67
|
|
55
68
|
# Cursor
|
@@ -172,11 +185,14 @@ module Mongocore
|
|
172
185
|
@key ||= "#{@model}#{@query.sort}#{@options.sort}#{@store.values}"
|
173
186
|
end
|
174
187
|
|
175
|
-
#
|
176
|
-
def oid(
|
177
|
-
|
178
|
-
|
179
|
-
|
188
|
+
# Schema short cut for oid
|
189
|
+
def oid(k)
|
190
|
+
@model.schema.oid(k)
|
191
|
+
end
|
192
|
+
|
193
|
+
# Schema short cut for oid?
|
194
|
+
def oid?(k)
|
195
|
+
@model.schema.oid?(k)
|
180
196
|
end
|
181
197
|
|
182
198
|
# Call and return the scope if it exists
|
data/lib/mongocore/schema.rb
CHANGED
@@ -46,22 +46,39 @@ module Mongocore
|
|
46
46
|
# Convert type if val and schema type is set
|
47
47
|
def convert(key, val)
|
48
48
|
return nil if val.nil?
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
49
|
+
case find_type(key)
|
50
|
+
when :string
|
51
|
+
val.to_s
|
52
|
+
when :integer
|
53
|
+
val.to_i
|
54
|
+
when :float
|
55
|
+
val.to_f
|
56
|
+
when :boolean
|
57
|
+
val.to_s.to_bool
|
58
|
+
when :object_id
|
59
|
+
oid(val)
|
60
60
|
else
|
61
61
|
val
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
|
+
# Find type as defined in schema
|
66
|
+
def find_type(key)
|
67
|
+
@keys[key][:type].to_sym rescue :string
|
68
|
+
end
|
69
|
+
|
70
|
+
# This key is a BSON::Object ID?
|
71
|
+
def oid?(key)
|
72
|
+
find_type(key) === :object_id
|
73
|
+
end
|
74
|
+
|
75
|
+
# Convert to BSON::ObjectId
|
76
|
+
def oid(id = nil)
|
77
|
+
return id if id.is_a?(BSON::ObjectId)
|
78
|
+
return BSON::ObjectId.new if !id
|
79
|
+
BSON::ObjectId.from_string(id) rescue id
|
80
|
+
end
|
81
|
+
|
65
82
|
# # # # # # # # #
|
66
83
|
# Templates for foreign key, many-associations and scopes.
|
67
84
|
#
|
data/lib/mongocore.rb
CHANGED
data/mongocore.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'mongocore'
|
3
|
-
s.version = '0.2.
|
4
|
-
s.date = '2017-10-
|
3
|
+
s.version = '0.2.1'
|
4
|
+
s.date = '2017-10-27'
|
5
5
|
s.summary = "MongoDB ORM implementation on top of the Ruby MongoDB driver"
|
6
6
|
s.description = "Does validations, associations, scopes, filters, pagination, counter cache, request cache, and nested queries. Using a YAML schema file, which supports default values, data types, and security levels for each key."
|
7
7
|
s.authors = ["Fugroup Limited"]
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongocore
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fugroup Limited
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-10-
|
11
|
+
date: 2017-10-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongo
|