mongous 0.2.0 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +73 -0
  3. data/README.adoc +191 -38
  4. data/README.ja.adoc +192 -39
  5. data/Rakefile +8 -8
  6. data/lib/mongous/base.rb +52 -8
  7. data/lib/mongous/document.rb +36 -13
  8. data/lib/mongous/extention.rb +51 -39
  9. data/lib/mongous/filter.rb +316 -227
  10. data/lib/mongous/version.rb +1 -1
  11. data/mongous.gemspec +4 -4
  12. data/sample/connect_auto_0.rb +9 -0
  13. data/sample/declare_compact_1.rb +6 -8
  14. data/sample/declare_compact_2.rb +38 -0
  15. data/sample/declare_label_1.rb +9 -7
  16. data/sample/declare_ndex_1.rb +8 -6
  17. data/sample/declare_ndex_2.rb +9 -7
  18. data/sample/declare_strict_1.rb +9 -9
  19. data/sample/declare_strict_2.rb +4 -5
  20. data/sample/multi_collection_1.rb +0 -2
  21. data/sample/multi_collection_2.rb +5 -5
  22. data/sample/multi_collection_3.rb +24 -0
  23. data/sample/query_basic_1.rb +8 -2
  24. data/sample/query_basic_2.rb +8 -2
  25. data/sample/query_basic_3.rb +0 -2
  26. data/sample/query_basic_4.rb +3 -5
  27. data/sample/query_basic_5.rb +0 -2
  28. data/sample/query_basic_6.rb +56 -0
  29. data/sample/query_detail_1.rb +3 -3
  30. data/sample/query_detail_2.rb +3 -3
  31. data/sample/query_detail_3.rb +4 -5
  32. data/sample/query_filter_1.rb +1 -3
  33. data/sample/query_filter_2.rb +2 -4
  34. data/sample/query_find_1.rb +6 -5
  35. data/sample/query_select_1.rb +54 -0
  36. data/sample/query_skip_limit_1.rb +25 -14
  37. data/sample/query_skip_limit_2.rb +20 -11
  38. data/sample/query_sort_1.rb +43 -0
  39. data/sample/save_basic_1.rb +2 -6
  40. data/sample/save_basic_2.rb +2 -6
  41. data/sample/save_basic_3.rb +2 -6
  42. data/sample/save_detail_1.rb +3 -3
  43. data/sample/save_detail_2.rb +3 -3
  44. data/sample/save_detail_3.rb +10 -10
  45. data/sample/save_verify_1.rb +6 -6
  46. data/sample/save_verify_2.rb +0 -2
  47. data/sample/save_verify_3.rb +0 -2
  48. data/sample/save_verify_4.rb +0 -2
  49. data/sample/save_verify_5.rb +3 -5
  50. data/sample/save_verify_6.rb +3 -5
  51. data/sample/save_verify_7.rb +21 -0
  52. data/sample/update_basic_1.rb +9 -0
  53. data/sample/zap_basic_1.rb +1 -5
  54. data/sample/zap_basic_2.rb +2 -6
  55. data/sample/zap_basic_3.rb +2 -6
  56. data/sample/zbenchmark_search_1.rb +114 -0
  57. data/sample/zbenchmark_search_2.rb +114 -0
  58. data/sample/zbenchmark_search_3.rb +88 -0
  59. metadata +31 -21
  60. data/sample/query_projection_1.rb +0 -41
  61. data/sample/query_sort_order_1.rb +0 -46
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 26e437e93bf76ff87a088329b744df4f1425d7b14c106d80edb46f12bbfde12f
4
- data.tar.gz: 4e6bb7dd48f0799ce33165629c658b174316fd9d271f4f51ae866b804607ae59
3
+ metadata.gz: 6a7aef9ade90f7665f9152ca0fce4831c5f9ac3263bba36f1014b2f51d1596fb
4
+ data.tar.gz: 5b69a145e019b335bb49de2eec293fbe2109ada1d1702dcd58e27e7e918ff408
5
5
  SHA512:
6
- metadata.gz: 392818352168bf2c196c90693559f421e35fe6ed15bdaa013a9a3f651a5dbda66f2eb0f4ed38c25e28c5d77c1c7d5122d8384c8de56bb701b896ffe2133b3ef0
7
- data.tar.gz: 401266df4545a41f3b8d5e090f986dc2cd8215b48d153da195b8c276316a3e9ebed07b8a24e5dd677cc469d9676415a30ffb844f29211064c3d1fefa4c6badd5
6
+ metadata.gz: 6d81649ab8678859734b621712493adde5d7d0056f532c4abcb0297894442acd1be905b60e725eb85070aa95a75f4c2353d9e54bcbb44fb20346afd398cc3448
7
+ data.tar.gz: 0e7001c64b912df122d67c1bd67cbc378b6e9974c24b1a94235348c8ed74cd1fa22d4c65eba2d14e7968387ae8ca22ae375ae46ece75faa039cd8d4936cf1bc3
@@ -0,0 +1,73 @@
1
+
2
+ AllCops:
3
+ Exclude:
4
+ - '.*'
5
+ - '*'
6
+ - '*.gemspec'
7
+ - 'Gemfile*'
8
+ - 'bin/**'
9
+ - 'exe/**'
10
+ - 'config/**/*'
11
+ - 'spec/**/*'
12
+ - 'tmp/**/*'
13
+ - 'vendor/**/*'
14
+ TargetRubyVersion: 2.7
15
+
16
+ Layout/ExtraSpacing:
17
+ Enabled: false
18
+
19
+ Layout/SpaceAroundOperators:
20
+ Enabled: false
21
+
22
+ Layout/SpaceBeforeFirstArg:
23
+ Enabled: false
24
+
25
+ Layout/SpaceInsideParens:
26
+ Enabled: false
27
+
28
+ Layout/SpaceInsideArrayLiteralBrackets:
29
+ Enabled: false
30
+
31
+ Layout/SpaceInsideRangeLiteral:
32
+ Enabled: false
33
+
34
+ Layout/SpaceInsideReferenceBrackets:
35
+ Enabled: false
36
+
37
+ Layout/SpaceInsideStringInterpolation:
38
+ Enabled: false
39
+
40
+ Lint/AmbiguousBlockAssociation:
41
+ Enabled: false
42
+
43
+ Lint/EmptyWhen:
44
+ Enabled: false
45
+
46
+ Lint/ReturnInVoidContext:
47
+ Enabled: false
48
+
49
+ Lint/UselessAssignment:
50
+ Enabled: false
51
+
52
+ Metrics/BlockLength:
53
+ Enabled: false
54
+
55
+ Metrics/LineLength:
56
+ Enabled: false
57
+ Max: 120
58
+
59
+ Metrics/MethodLength:
60
+ Enabled: false
61
+
62
+ Style/Documentation:
63
+ Enabled: true
64
+
65
+ Style/FrozenStringLiteralComment:
66
+ Enabled: false
67
+
68
+ Style/NumericPredicate:
69
+ Enabled: false
70
+
71
+ Style/StringLiterals:
72
+ Enabled: false
73
+
@@ -5,9 +5,11 @@ Yet another mongo wrapper library.
5
5
  == Features
6
6
 
7
7
  * A light library that depends only on the mongo driver and bson, except for built-in and standard attachments.
8
- * Item default value can be described in the block.
9
- * Item constraints can be described by Array, Range, and Proc.
10
- * Checks that constraints are met when saving and setting values.
8
+ * Document item constraints can be described in Array, Range, Regexp, Proc instances, or base classes.
9
+ * Check if the constraint conditions are met when saving the document.
10
+ * Check if the constraint condition is satisfied when setting the item value of the document.
11
+ * The contents of undefined items in the document can be described by value or Proc.
12
+ * Item contents at the time of document creation and update can be described by value or Proc.
11
13
 
12
14
  == Installation
13
15
 
@@ -36,11 +38,7 @@ Or install it yourself as:
36
38
  ----
37
39
  require "mongous"
38
40
 
39
- Mongous.connect!
40
-
41
- class Book
42
- include Mongous::Document
43
- end
41
+ Mongous.document! :Book
44
42
  ----
45
43
 
46
44
  === Detail declaration
@@ -54,16 +52,17 @@ Mongous.connect! ["localhost:27017"], database: "test"
54
52
  class Book
55
53
  include Mongous::Document
56
54
 
57
- field :title, String, :must
55
+ field :title, String, :must
58
56
  field :author, String
59
57
  field :publisher, String
60
- field :style, String, ["A4","B5","A5","B6"]
61
- field :price, Integer, (0..1_000_000)
62
- field :page, Integer, proc{ page % 4 == 0 }
63
- field :isbn, String, proc{ isbn? }
64
- field :lang, String, default: "en"
65
- field :created_at, Time, create: proc{ Time.now }
66
- field :updated_at, Time, update: proc{ Time.now }
58
+ field :style, String, %w[hardcover, softcover, paperback]
59
+ field :size, String, /[AB]\d/
60
+ field :price, Integer, (0..1_000_000)
61
+ field :page, Integer, proc{ page % 4 == 0 }
62
+ field :isbn, String, proc{ isbn? }
63
+ field :lang, String, default: "en"
64
+ field :created_at, Time, create: proc{ Time.now }
65
+ field :updated_at, Time, update: proc{ Time.now }
67
66
 
68
67
  filter :foobar, {title: /foobar/}
69
68
 
@@ -87,13 +86,13 @@ end
87
86
  book = Book.new
88
87
  book.title = "title 1"
89
88
  book.price = 1000
90
- book.style = "A4"
89
+ book.size = "A4"
91
90
  book.save
92
91
 
93
- book = Book.new( title: "title 2", price: 2000, style: "A5" )
92
+ book = Book.new( title: "title 2", price: 2000, size: "A5" )
94
93
  book.save
95
94
 
96
- doc = { title: "title 3", price: 3000, style: "A6" }
95
+ doc = { title: "title 3", price: 3000, size: "A6" }
97
96
  Book.create( **doc )
98
97
  ----
99
98
 
@@ -103,7 +102,8 @@ Book.create( **doc )
103
102
  ----
104
103
  pp books = Book.all
105
104
 
106
- p book = Book.where( title: "title 1" ).first
105
+ p book = Book.where( title: /title/ ).first
106
+ p book = Book.where( title: /title/ ).last
107
107
 
108
108
  books = Book.where( title: /title/ ).all
109
109
  books.each do |book|
@@ -114,14 +114,14 @@ Book.where( title: /title/ ).projection( _id: 0 ).each do |book|
114
114
  p book
115
115
  end
116
116
 
117
- Book.where( price: (1..2000), style: ["A4","A5"] ).each do |book|
117
+ Book.where( price: (1..2000), size: ["A4","A5"] ).each do |book|
118
118
  p book
119
119
  end
120
120
 
121
121
  filter1 = Book.where( title: /title/ )
122
122
  filter2 = Book.where( :foobar )
123
123
  filter3 = Book.where( price: (1..2000) )
124
- filter4 = Book.where( style: ["A4","A5"] )
124
+ filter4 = Book.where( size: ["A4","A5"] )
125
125
 
126
126
  Book.not( filter1 ).each do |book|
127
127
  p book
@@ -137,7 +137,15 @@ Book.find( { title: /title/ }, { projection: {_id: 0} } ).each do |book|
137
137
  p book
138
138
  end
139
139
 
140
- pp Book.where( title: /title/ )[0, 5].all
140
+ Book.where( title: /title/ ).select( _id: 0 ).each do |book|
141
+ p book
142
+ end
143
+
144
+ Book.select( _id: 0 )[0, 5].each do |book|
145
+ p book
146
+ end
147
+
148
+ pp Book.select( :title, :price, :size )[5, 5].all
141
149
  ----
142
150
 
143
151
  === Update document
@@ -187,7 +195,22 @@ Mongous.connect( hosts_or_uri = nil, **options )
187
195
  * Result:
188
196
  ** Mongo::Client instance.
189
197
 
190
- === Include document functions.
198
+ === Define collection operate class with default settings.
199
+
200
+ [source,ruby]
201
+ ----
202
+ Mongous.document!( *names, **options )
203
+ ----
204
+
205
+ * Result:
206
+ ** nil.
207
+
208
+ * Parameter:
209
+ ** names: Collection names. (String or Symbol)
210
+ ** options: Options.
211
+ *** timestamp: If true then add fields :created_at, :updated_at.
212
+
213
+ === Include document functions into collection operate class.
191
214
 
192
215
  [source,ruby]
193
216
  ----
@@ -198,7 +221,7 @@ include Mongous::Document
198
221
 
199
222
  [source,ruby]
200
223
  ----
201
- self.client=( _client )
224
+ self.client=( client )
202
225
  ----
203
226
 
204
227
  * Result:
@@ -207,6 +230,19 @@ self.client=( _client )
207
230
  * Parameter:
208
231
  ** client: Mongo::Client instance.
209
232
 
233
+ === Get binded database.
234
+
235
+ [source,ruby]
236
+ ----
237
+ self.client
238
+ ----
239
+
240
+ * Result:
241
+ ** Mongo::Client instance.
242
+
243
+ * Parameter:
244
+ ** None.
245
+
210
246
  === Bind another collection.
211
247
 
212
248
  [source,ruby]
@@ -220,11 +256,37 @@ self.collection_name=( _collection_name )
220
256
  * Parameter:
221
257
  ** collection_name: Collection name.
222
258
 
259
+ === Get binded collection name.
260
+
261
+ [source,ruby]
262
+ ----
263
+ self.collection_name
264
+ ----
265
+
266
+ * Result:
267
+ ** Collection name string.
268
+
269
+ * Parameter:
270
+ ** None.
271
+
272
+ === Get collection.
273
+
274
+ [source,ruby]
275
+ ----
276
+ self.collection( collection_name = nil )
277
+ ----
278
+
279
+ * Result:
280
+ ** Mongo::Collection instance.
281
+
282
+ * Parameter:
283
+ ** collection_name: Tempolary collection name.
284
+
223
285
  === Declare document structure.
224
286
 
225
287
  [source,ruby]
226
288
  ----
227
- field( symbol, *attrs, **items )
289
+ self.field( symbol, *attrs, **items )
228
290
  ----
229
291
 
230
292
  * Parameter:
@@ -245,7 +307,7 @@ field( symbol, *attrs, **items )
245
307
 
246
308
  [source,ruby]
247
309
  ----
248
- verify( *directives, &block )
310
+ self.verify( *directives, &block )
249
311
  ----
250
312
 
251
313
  * Parameter:
@@ -257,7 +319,7 @@ verify( *directives, &block )
257
319
 
258
320
  [source,ruby]
259
321
  ----
260
- index( *symbols, **options )
322
+ self.index( *symbols, **options )
261
323
  ----
262
324
 
263
325
  * Parameter:
@@ -268,7 +330,7 @@ index( *symbols, **options )
268
330
 
269
331
  [source,ruby]
270
332
  ----
271
- having?( label )
333
+ self.having?( label )
272
334
  ----
273
335
 
274
336
  * Result:
@@ -281,22 +343,38 @@ having?( label )
281
343
 
282
344
  [source,ruby]
283
345
  ----
284
- filter( symbol, filter_or_cond )
346
+ self.filter( symbol, filter_or_cond )
285
347
  ----
286
348
 
287
349
  * Parameter:
288
350
  ** symbol: Filter name.
289
351
  ** filter_or_cond: Filter or search criteria.
290
352
 
353
+ === Select output fields.
354
+
355
+ [source,ruby]
356
+ ----
357
+ Mongous::Document.select( *keys, **kwargs )
358
+ Mongous::Filter#select( *keys, **kwargs )
359
+ ----
360
+
361
+ * Result:
362
+ ** Mongous::Filter instance.
363
+
364
+ * Parameter:
365
+ ** keys: Field symbols.
366
+ ** kwargs: Field symbols and values.
367
+
291
368
  === Search condition.
292
369
 
293
370
  [source,ruby]
294
371
  ----
295
- where( filter = nil, **conditions )
372
+ Mongous::Document.where( filter = nil, **conditions )
373
+ Mongous::Filter#where( filter = nil, **conditions )
296
374
  ----
297
375
 
298
376
  * Result:
299
- ** Filter instance.
377
+ ** Mongous::Filter instance.
300
378
 
301
379
  * Parameter:
302
380
  ** filter: Filter name symbol, or filter instance.
@@ -306,11 +384,12 @@ where( filter = nil, **conditions )
306
384
 
307
385
  [source,ruby]
308
386
  ----
309
- not( filter = nil, **conditions )
387
+ Mongous::Document.not( filter = nil, **conditions )
388
+ Mongous::Filter#not( filter = nil, **conditions )
310
389
  ----
311
390
 
312
391
  * Result:
313
- ** Filter instance.
392
+ ** Mongous::Filter instance.
314
393
 
315
394
  * Parameter:
316
395
  ** filter: Filter name symbol, or filter instance.
@@ -320,11 +399,12 @@ not( filter = nil, **conditions )
320
399
 
321
400
  [source,ruby]
322
401
  ----
323
- and( *filters )
402
+ Mongous::Document.and( *filters )
403
+ Mongous::Filter#and( *filters )
324
404
  ----
325
405
 
326
406
  * Result:
327
- ** Filter instance.
407
+ ** Mongous::Filter instance.
328
408
 
329
409
  * Parameter:
330
410
  ** filters: Filter name symbol, or filter instance.
@@ -333,15 +413,88 @@ and( *filters )
333
413
 
334
414
  [source,ruby]
335
415
  ----
336
- or( *filters )
416
+ Mongous::Document.or( *filters )
417
+ Mongous::Filter#or( *filters )
337
418
  ----
338
419
 
339
420
  * Result:
340
- ** Filter instance.
421
+ ** Mongous::Filter instance.
341
422
 
342
423
  * Parameter:
343
424
  ** filters: Field name symbol, or filter instance.
344
425
 
426
+ === Create document.
427
+
428
+ [source,ruby]
429
+ ----
430
+ Mongous::Document.create( **doc )
431
+ ----
432
+
433
+ * Result:
434
+ ** nil.
435
+
436
+ * Parameter:
437
+ ** doc: Keyword arguments.
438
+
439
+ === Save document.
440
+
441
+ [source,ruby]
442
+ ----
443
+ Mongous::Document#save
444
+ ----
445
+
446
+ * Result:
447
+ ** nil.
448
+
449
+ === Convert document to Hash.
450
+
451
+ [source,ruby]
452
+ ----
453
+ Mongous::Document#to_hash
454
+ ----
455
+
456
+ * Result:
457
+ ** Hash object.
458
+
459
+ === Convert document to JSON.
460
+
461
+ [source,ruby]
462
+ ----
463
+ Mongous::Document#to_json
464
+ ----
465
+
466
+ * Result:
467
+ ** JSON String.
468
+
469
+ === Read document field.
470
+
471
+ [source,ruby]
472
+ ----
473
+ Mongous::Document#[]( field_name )
474
+ Mongous::Document#field_name
475
+ ----
476
+
477
+ * Result:
478
+ ** field_value.
479
+
480
+ * Parameter:
481
+ ** field_name: Field name.
482
+
483
+ === Write document field.
484
+
485
+ [source,ruby]
486
+ ----
487
+ Mongous::Document#[]=( field_name, field_value )
488
+ Mongous::Document#field_name = field_value
489
+ ----
490
+
491
+ * Result:
492
+ ** field_value.
493
+
494
+ * Parameter:
495
+ ** field_name: Field name.
496
+ ** field_value: Field value.
497
+
345
498
  == Contributing
346
499
 
347
500
  Bug reports and pull requests are welcome on GitHub at https://github.com/arimay/mongous.