mongocore 0.1.4.1 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 82ab8bff97378c5d4201d927c111a9975d3a1b66
4
- data.tar.gz: a4c9fb66a260bc63c7bebc73a2420e268efbfa79
3
+ metadata.gz: 744f17825ab911d65f52d3503143554f866089b5
4
+ data.tar.gz: 99df9b5cf77a9b570314431bc886aff2ffca5337
5
5
  SHA512:
6
- metadata.gz: 8b946978a281316973d65c9f93d814a8c8b44eb84f11c71839bc994b77619632a3486750b99517408159080ab3616dd323a7db4540d1cc8c4274fb51250a15de
7
- data.tar.gz: b5f1b9c1eace40133164715217fec0ebc14a977b9be9a38b90b8044297a3c12d4581750fdc7c474897e6871db097e1bca5427a3fac49eacf106347775cba5c3b
6
+ metadata.gz: 51caf8f60dc78e033522e98d5f38ab0898fcd9924360ace6c2820ae513bb04f43bd519de7e680043eb24fbe2fe29e7b06a646f78de49dcc7d738adf739bb8e9d
7
+ data.tar.gz: '08f518db3a2d63ddcd4bfe528522309ea47e5379a7e61e371f01cda89ea2850551abba6fb877773e78deaecc9ecd041417e64eba1f2d8d00999c653110d6bc32'
@@ -15,15 +15,6 @@ module Mongocore
15
15
  # Mongocore query initializer
16
16
  def initialize(m, q = {}, o = {}, s = {})
17
17
 
18
- # Support find passing an ID
19
- q = {:_id => oid(q)} unless q.is_a?(Hash)
20
-
21
- # Support passing :id as :_id
22
- q[:_id] = q.delete(:id) if q[:id]
23
-
24
- # Support passing _id as a string
25
- q[:_id] = oid(q[:_id]) if q[:_id].is_a?(String)
26
-
27
18
  # Storing model class. The instance can be found in store[:source]
28
19
  @model = m
29
20
 
@@ -35,7 +26,7 @@ module Mongocore
35
26
 
36
27
  # Storing query and options. Sort and limit is stored in options
37
28
  s[:sort] ||= {}; s[:limit] ||= 0; s[:chain] ||= []; s[:source] ||= nil; s[:fields] ||= {}; s[:skip] ||= 0
38
- @query, @options, @store = q, o, s
29
+ @query, @options, @store = normalize(q), o, s
39
30
 
40
31
  # Set up cache
41
32
  @cache = Mongocore::Cache.new(self)
@@ -43,7 +34,21 @@ module Mongocore
43
34
 
44
35
  # Find. Returns a Mongocore::Query
45
36
  def find(q = {}, o = {}, s = {})
46
- Mongocore::Query.new(@model, @query.merge(q), @options.merge(o), @store.merge(s))
37
+ Mongocore::Query.new(@model, @query.merge(normalize(q)), @options.merge(o), @store.merge(s))
38
+ end
39
+
40
+ # Normalize query
41
+ def normalize(q)
42
+ # Support find passing an ID
43
+ q = {:_id => oid(q)} unless q.is_a?(Hash)
44
+
45
+ # Support passing :id as :_id
46
+ q[:_id] = q.delete(:id) if q[:id]
47
+
48
+ # Convert object ID's to BSON::ObjectIds
49
+ q.each{|k, v| q[k] = oid(v) if k =~ /_id$/}
50
+
51
+ q
47
52
  end
48
53
 
49
54
  # Cursor
@@ -68,7 +73,8 @@ module Mongocore
68
73
  end
69
74
 
70
75
  # Count. Returns the number of documents as an integer
71
- def count
76
+ def count(*args)
77
+ find(*args) if args.any?
72
78
  counter || fetch(:count)
73
79
  end
74
80
 
@@ -78,18 +84,25 @@ module Mongocore
78
84
  end
79
85
 
80
86
  # Return first document
81
- def first(doc = nil)
82
- (doc ||= fetch(:first)) ? @model.new(doc.to_hash) : nil
87
+ def first(*args)
88
+ find(*args) if args.any?
89
+ modelize(fetch(:first))
83
90
  end
84
91
 
85
92
  # Return last document
86
- def last
93
+ def last(*args)
94
+ find(*args) if args.any?
87
95
  sort(:_id => -1).limit(1).first
88
96
  end
89
97
 
90
98
  # Return all documents
91
99
  def all
92
- fetch(:to_a).map{|d| first(d)}
100
+ fetch(:to_a).map{|d| modelize(d)}
101
+ end
102
+
103
+ # Doc to model
104
+ def modelize(doc)
105
+ doc ? @model.new(doc.to_hash) : nil
93
106
  end
94
107
 
95
108
  # Paginate
@@ -111,7 +124,7 @@ module Mongocore
111
124
  store[:limit] = o[:per_page]
112
125
 
113
126
  # Fetch the result as array
114
- fetch(:to_a).map{|d| first(d)}.tap{|r| r.total = total}
127
+ fetch(:to_a).map{|d| modelize(d)}.tap{|r| r.total = total}
115
128
  end
116
129
 
117
130
  # Fetch docs, pass type :first, :to_a or :count
data/lib/mongocore.rb CHANGED
@@ -7,7 +7,7 @@ require 'mongo'
7
7
  require 'request_store'
8
8
 
9
9
  module Mongocore
10
- VERSION = '0.1.1'
10
+ VERSION = '0.1.5'
11
11
 
12
12
  # # # # # #
13
13
  # Mongocore Ruby Database Driver.
data/mongocore.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'mongocore'
3
- s.version = '0.1.4.1'
4
- s.date = '2017-10-16'
3
+ s.version = '0.1.5'
4
+ s.date = '2017-10-17'
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.1.4.1
4
+ version: 0.1.5
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-16 00:00:00.000000000 Z
11
+ date: 2017-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongo