mongocore 0.3.2 → 0.4.0

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: 2892cb201bc815741fda85f48d3dcd2b4546dec7
4
- data.tar.gz: 36cfc6ded9f8b95fc7be644669c298511b2a102e
3
+ metadata.gz: 6530290c756f80c471d9c5565e7885fcf80a2c33
4
+ data.tar.gz: ba0ea77310386d919069e77552aee4620223e6ae
5
5
  SHA512:
6
- metadata.gz: 0d15a8105f04db2f5583badc9c27005bc95b49cbc82897d47694bc6b4555f70ea9a5131dfbde0fe4eae09b86a5bffc046820b4a8e97a279966d976d6b3ebfd5c
7
- data.tar.gz: 39a1baeb1e02ced7ef9b9d61d243ac79c362a69447bebe709399e84b0d9e8abab6063eb6b04119633dc92867c60d3c29cca402f98171ff539ba3ed1360b6a12d
6
+ metadata.gz: ecfc84ad83cf9f41669613c5925ff13aa632ace888bb4b6351d352a221a885b8114363f08cdec8449312330822d6947f2be51682190b9acfe134814d0f00d1ee
7
+ data.tar.gz: 70327cb292727616e66d15c842184746be4bd8e06fd362543d358dbfec4c593d963a31a89c7a6c16c624f4e4b02d540cd50ce9263fba3a4c54382a89168e92aa
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ **Version 0.4.0** - *2017-12-01*
2
+
3
+ - Added each_with_index, each_with_object and map
4
+ - Added id = method
5
+ - Refactored cursor, faster finds
6
+ - Added paginate to model class methods
7
+
8
+
1
9
  **Version 0.3.2** - *2017-11-25*
2
10
 
3
11
  - Alias for insert is create
data/README.md CHANGED
@@ -57,7 +57,7 @@ Mongocore.access = true
57
57
  Mongocore.timestamps = true
58
58
 
59
59
  # Default sorting, last will be opposite. Should be indexed.
60
- Mongocore.sort = {:_id => 1}
60
+ Mongocore.sort = {}
61
61
 
62
62
  # Pagination results per page
63
63
  Mogocore.per_page = 20
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.3.2'
10
+ VERSION = '0.4.0'
11
11
 
12
12
  # # # # # #
13
13
  # Mongocore Ruby Database Driver.
@@ -31,7 +31,7 @@ module Mongocore
31
31
  @timestamps = true
32
32
 
33
33
  # Default sorting, last will be opposite. Should be indexed.
34
- @sort = {:_id => 1}
34
+ @sort = {}
35
35
 
36
36
  # Pagination results per page
37
37
  @per_page = 20
@@ -221,6 +221,11 @@ module Mongocore
221
221
  @_id ? @_id.to_s : nil
222
222
  end
223
223
 
224
+ # Assign id
225
+ def id=(val)
226
+ @_id = val.is_a?(BSON::ObjectId) ? val : BSON::ObjectId.from_string(val)
227
+ end
228
+
224
229
  # Print info about the instance
225
230
  def inspect
226
231
  "#<#{self.class} #{attributes.sort.map{|r| %{#{r[0]}: #{r[1].inspect}}}.join(', ')}>"
@@ -277,6 +282,11 @@ module Mongocore
277
282
  find(*args).all
278
283
  end
279
284
 
285
+ # Paginate
286
+ def paginate(*args)
287
+ find({}).paginate(*args)
288
+ end
289
+
280
290
  # Sort
281
291
  def sort(o = {})
282
292
  find({}, {}, :sort => o)
@@ -292,10 +302,11 @@ module Mongocore
292
302
  find({}, {}, :skip => n)
293
303
  end
294
304
 
295
- # Fields (projection)
296
- def fields(o = {})
297
- find({}, {}, :fields => o)
305
+ # Projection
306
+ def projection(o = {})
307
+ find({}, {}, :projection => o)
298
308
  end
309
+ alias_method :fields, :projection
299
310
 
300
311
  # Insert
301
312
  def insert(a = {}, o = {})
@@ -308,6 +319,21 @@ module Mongocore
308
319
  find.each{|r| yield(r)}
309
320
  end
310
321
 
322
+ # Each with index
323
+ def each_with_index(&block)
324
+ find.each_with_index{|r, n| yield(r, n)}
325
+ end
326
+
327
+ # Each with object
328
+ def each_with_object(obj, &block)
329
+ find.each_with_object(obj){|r, o| yield(r, o)}
330
+ end
331
+
332
+ # Map
333
+ def map(&block)
334
+ find.map{|r| yield(r)}
335
+ end
336
+
311
337
 
312
338
  # # # # # # # # #
313
339
  # After, before and validation filters
@@ -25,7 +25,7 @@ module Mongocore
25
25
  @collection = Mongocore.db[@colname]
26
26
 
27
27
  # Storing query and options
28
- s[:limit] ||= 0; s[:chain] ||= []; s[:source] ||= nil; s[:fields] ||= {}; s[:skip] ||= 0
28
+ s[:chain] ||= []; s[:source] ||= nil; s[:sort] ||= Mongocore.sort; s[:projection] ||= {}; s[:skip] ||= 0; s[:limit] ||= 0
29
29
  @query, @options, @store = @model.schema.ids(hashify(q)), o, s
30
30
 
31
31
  # Set up cache
@@ -45,7 +45,12 @@ module Mongocore
45
45
 
46
46
  # Cursor
47
47
  def cursor
48
- @collection.find(@query, @options).projection(@store[:fields]).skip(@store[:skip]).sort(@store[:sort] || Mongocore.sort).limit(@store[:limit])
48
+ c = @collection.find(@query, @options)
49
+ c = c.sort(@store[:sort]) if @store[:sort].any?
50
+ c = c.projection(@store[:projection]) if @store[:projection].any?
51
+ c = c.skip(@store[:skip]) if @store[:skip] > 0
52
+ c = c.limit(@store[:limit]) if @store[:limit] > 0
53
+ c
49
54
  end
50
55
 
51
56
  # Insert
@@ -85,8 +90,10 @@ module Mongocore
85
90
  end
86
91
 
87
92
  # Return last document
93
+ # Uses the opposite of the Mongocore.sort setting
88
94
  def last(*args)
89
- sort((a = Mongocore.sort.dup).each{|k, v| a[k] = v * -1}).limit(1).first(*args)
95
+ a = Mongocore.sort.any? ? Mongocore.sort.dup : {:_id => 1}
96
+ sort(a.each{|k, v| a[k] = v * -1}).limit(1).first(*args)
90
97
  end
91
98
 
92
99
  # Return all documents
@@ -133,6 +140,21 @@ module Mongocore
133
140
  cursor.each{|r| yield(modelize(r))}
134
141
  end
135
142
 
143
+ # Each with index
144
+ def each_with_index(&block)
145
+ cursor.each_with_index{|r, n| yield(modelize(r), n)}
146
+ end
147
+
148
+ # Each with object
149
+ def each_with_object(obj, &block)
150
+ cursor.each_with_object(obj){|r, o| yield(modelize(r), o)}
151
+ end
152
+
153
+ # Map
154
+ def map(&block)
155
+ cursor.map{|r| yield(modelize(r))}
156
+ end
157
+
136
158
  # Sort
137
159
  def sort(o = {})
138
160
  find(@query, @options, @store.tap{(store[:sort] ||= {}).merge!(o)})
@@ -148,10 +170,11 @@ module Mongocore
148
170
  find(@query, @options, @store.tap{store[:skip] = n})
149
171
  end
150
172
 
151
- # Fields (projection)
152
- def fields(o = {})
153
- find(@query, @options, @store.tap{store[:fields].merge!(o)})
173
+ # Projection
174
+ def projection(o = {})
175
+ find(@query, @options, @store.tap{store[:projection].merge!(o)})
154
176
  end
177
+ alias_method :fields, :projection
155
178
 
156
179
  # JSON format
157
180
  def as_json(o = {})
data/mongocore.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'mongocore'
3
- s.version = '0.3.2'
4
- s.date = '2017-11-25'
3
+ s.version = '0.4.0'
4
+ s.date = '2017-12-01'
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.3.2
4
+ version: 0.4.0
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-11-25 00:00:00.000000000 Z
11
+ date: 2017-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongo