mongous 0.1.8 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/README.adoc +286 -48
  3. data/README.ja.adoc +288 -50
  4. data/lib/mongous/base.rb +45 -2
  5. data/lib/mongous/document.rb +75 -32
  6. data/lib/mongous/extention.rb +74 -55
  7. data/lib/mongous/filter.rb +316 -233
  8. data/lib/mongous/version.rb +1 -1
  9. data/mongous.gemspec +1 -1
  10. data/sample/connect_auto_0.rb +9 -0
  11. data/sample/declare_compact_1.rb +7 -9
  12. data/sample/declare_compact_2.rb +38 -0
  13. data/sample/declare_label_1.rb +10 -8
  14. data/sample/declare_ndex_1.rb +9 -7
  15. data/sample/declare_ndex_2.rb +10 -8
  16. data/sample/declare_strict_1.rb +14 -12
  17. data/sample/declare_strict_2.rb +5 -5
  18. data/sample/{multi_docs_1.rb → multi_collection_1.rb} +0 -2
  19. data/sample/multi_collection_2.rb +61 -0
  20. data/sample/multi_collection_3.rb +24 -0
  21. data/sample/query_basic_1.rb +8 -3
  22. data/sample/query_basic_2.rb +11 -6
  23. data/sample/query_basic_3.rb +2 -5
  24. data/sample/query_basic_4.rb +3 -6
  25. data/sample/query_basic_5.rb +2 -5
  26. data/sample/query_basic_6.rb +35 -22
  27. data/sample/query_detail_1.rb +3 -3
  28. data/sample/query_detail_2.rb +3 -3
  29. data/sample/query_detail_3.rb +5 -5
  30. data/sample/query_filter_1.rb +44 -0
  31. data/sample/query_filter_2.rb +48 -0
  32. data/sample/{query_super_1.rb → query_find_1.rb} +6 -6
  33. data/sample/query_select_1.rb +54 -0
  34. data/sample/query_skip_limit_1.rb +48 -0
  35. data/sample/query_skip_limit_2.rb +58 -0
  36. data/sample/query_sort_1.rb +43 -0
  37. data/sample/save_basic_1.rb +2 -7
  38. data/sample/save_basic_2.rb +2 -7
  39. data/sample/save_basic_3.rb +2 -7
  40. data/sample/save_detail_1.rb +7 -6
  41. data/sample/save_detail_2.rb +7 -6
  42. data/sample/save_detail_3.rb +11 -10
  43. data/sample/save_verify_1.rb +7 -6
  44. data/sample/save_verify_2.rb +0 -2
  45. data/sample/save_verify_3.rb +1 -3
  46. data/sample/save_verify_4.rb +0 -2
  47. data/sample/save_verify_5.rb +3 -5
  48. data/sample/save_verify_6.rb +3 -5
  49. data/sample/save_verify_7.rb +21 -0
  50. data/sample/update_basic_1.rb +9 -0
  51. data/sample/zap_basic_1.rb +1 -5
  52. data/sample/zap_basic_2.rb +2 -6
  53. data/sample/zap_basic_3.rb +2 -6
  54. data/sample/zbenchmark_search_1.rb +114 -0
  55. data/sample/zbenchmark_search_2.rb +114 -0
  56. data/sample/zbenchmark_search_3.rb +88 -0
  57. metadata +20 -12
  58. data/sample/multi_docs_2.rb +0 -58
  59. data/sample/query_basic_7.rb +0 -38
  60. data/sample/query_limit_1.rb +0 -44
  61. data/sample/query_order_1.rb +0 -49
  62. data/sample/query_projection_1.rb +0 -44
  63. data/sample/template_article.rb +0 -5
  64. data/sample/template_person.rb +0 -12
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3ac08d4dc3cd1b50de8ae75c4db5f63b9604abefa5890f046564d44619d75cf4
4
- data.tar.gz: 446aae2fcd699ad4a27cc5f15790a00d84eaceff3e38ef4ded3b5aec425dcba4
3
+ metadata.gz: 8b0a73aa23287619bdae717f683f04e266d582dca4a6d055b798cf32243c6566
4
+ data.tar.gz: 14e22dcbf25b3a0fa200023c9f770c549607060ddb03057b32c8c1ccee3b58c2
5
5
  SHA512:
6
- metadata.gz: ce0e4ff3b90f13ac1bdb328ecac4c8986120e780575dbb97a242638d2cfd2c11581963cc2141c2f09f3a184cdcef85420354f4a12d02eca890c66b1458c9ea8e
7
- data.tar.gz: 6e78c1a620e8cdc974e9e60bca01f54b0127b225a7e1e0f567c7354ec7d43adf1cd2e3ffa72859f6805ffcacd8b3a198258763a4f14ab0f216b6f4d585d56e0d
6
+ metadata.gz: b37766d78679cc2a6bee8bfe256da0a363344b6e349552f0491d5a0892fa1dfeb7cca90d4b2e326ac7687cdd2c3a0561fdfe9fca4ce71382029077d8c289e6de
7
+ data.tar.gz: b5e9fee3f8f9d4f34d5203b9b260559d8ee1ca86e659d85629ef9c1b700bcfcb450b7ffc0447b7f3fa8c4245634f2e968619f9ba451c9591016e13c07d23060f
@@ -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,15 +52,19 @@ 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 :publish_at, Date, &proc{ Date.today }
64
- field :isbn, String, proc{ isbn? }
65
- field :lang, String, &proc{ "ja" }
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 }
66
+
67
+ filter :foobar, {title: /foobar/}
66
68
 
67
69
  verify :strict
68
70
  verify do
@@ -84,13 +86,13 @@ end
84
86
  book = Book.new
85
87
  book.title = "title 1"
86
88
  book.price = 1000
87
- book.style = "A4"
89
+ book.size = "A4"
88
90
  book.save
89
91
 
90
- book = Book.new( title: "title 2", price: 2000, style: "A5" )
92
+ book = Book.new( title: "title 2", price: 2000, size: "A5" )
91
93
  book.save
92
94
 
93
- doc = { title: "title 3", price: 3000, style: "A6" }
95
+ doc = { title: "title 3", price: 3000, size: "A6" }
94
96
  Book.create( **doc )
95
97
  ----
96
98
 
@@ -100,31 +102,34 @@ Book.create( **doc )
100
102
  ----
101
103
  pp books = Book.all
102
104
 
103
- p book = Book.filter( title: "title 1" ).first
105
+ p book = Book.where( title: /title/ ).first
106
+ p book = Book.where( title: /title/ ).last
104
107
 
105
- books = Book.filter( title: /title/ ).all
108
+ books = Book.where( title: /title/ ).all
106
109
  books.each do |book|
107
110
  p book
108
111
  end
109
112
 
110
- Book.filter( title: /title/ ).projection( _id: 0 ).each do |book|
113
+ Book.where( title: /title/ ).projection( _id: 0 ).each do |book|
111
114
  p book
112
115
  end
113
116
 
114
- Book.filter( price: (1..2000), style: ["A4","A5"] ).each do |book|
117
+ Book.where( price: (1..2000), size: ["A4","A5"] ).each do |book|
115
118
  p book
116
119
  end
117
120
 
118
- filter1 = Book.filter( title: /title/ )
119
- filter2 = Book.filter( price: (1..2000) )
120
- filter3 = Book.filter( style: ["A4","A5"] )
121
+ filter1 = Book.where( title: /title/ )
122
+ filter2 = Book.where( :foobar )
123
+ filter3 = Book.where( price: (1..2000) )
124
+ filter4 = Book.where( size: ["A4","A5"] )
125
+
121
126
  Book.not( filter1 ).each do |book|
122
127
  p book
123
128
  end
124
- Book.and( filter1, filter2 ).each do |book|
129
+ Book.and( filter1, filter3 ).each do |book|
125
130
  p book
126
131
  end
127
- Book.or( filter1, filter3 ).each do |book|
132
+ Book.or( filter2, filter4 ).each do |book|
128
133
  p book
129
134
  end
130
135
 
@@ -132,14 +137,22 @@ Book.find( { title: /title/ }, { projection: {_id: 0} } ).each do |book|
132
137
  p book
133
138
  end
134
139
 
135
- pp Book.filter( 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
136
149
  ----
137
150
 
138
151
  === Update document
139
152
 
140
153
  [source,ruby]
141
154
  ----
142
- book = Book.filter( title: "title 1" ).first
155
+ book = Book.where( title: "title 1" ).first
143
156
  book.title = "title 1 [update]"
144
157
  book.save
145
158
  ----
@@ -148,7 +161,7 @@ book.save
148
161
 
149
162
  [source,ruby]
150
163
  ----
151
- book = Book.filter( title: "title 1" ).first
164
+ book = Book.where( title: "title 1" ).first
152
165
  book.delete
153
166
  ----
154
167
 
@@ -158,7 +171,7 @@ book.delete
158
171
 
159
172
  [source,ruby]
160
173
  ----
161
- Mongous.connect!( hosts_or_uri = nil, **opts )
174
+ Mongous.connect!( hosts_or_uri = nil, **options )
162
175
  ----
163
176
 
164
177
  * Result:
@@ -166,23 +179,38 @@ Mongous.connect!( hosts_or_uri = nil, **opts )
166
179
 
167
180
  * Parameter:
168
181
  ** hosts_or_uri: Array of hosts, or URI (default: ["localhost:21017"])
169
- ** opts: Options.
182
+ ** options: Options.
170
183
  *** file: Path to database configuration file.
171
184
  *** mode: Execution mode. (default: "development")
172
185
  *** database: Database name. (default: "test")
173
- *** * Other optional arguments for Mongo::Client.new.
186
+ *** Other optional arguments for Mongo::Client.new.
174
187
 
175
188
  === Connect database.
176
189
 
177
190
  [source,ruby]
178
191
  ----
179
- Mongous.connect( hosts_or_uri = nil, **opts )
192
+ Mongous.connect( hosts_or_uri = nil, **options )
193
+ ----
194
+
195
+ * Result:
196
+ ** Mongo::Client instance.
197
+
198
+ === Define collection operate class with default settings.
199
+
200
+ [source,ruby]
201
+ ----
202
+ Mongous.document!( *names, **options )
180
203
  ----
181
204
 
182
205
  * Result:
183
- ** Mongo::Client.
206
+ ** nil.
184
207
 
185
- === Include document functions.
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.
186
214
 
187
215
  [source,ruby]
188
216
  ----
@@ -193,42 +221,97 @@ include Mongous::Document
193
221
 
194
222
  [source,ruby]
195
223
  ----
196
- set_client( client )
224
+ self.client=( client )
197
225
  ----
198
226
 
199
227
  * Result:
200
- ** nil.
228
+ ** Mongo::Client instance.
201
229
 
202
230
  * Parameter:
203
231
  ** client: Mongo::Client instance.
204
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
+
246
+ === Bind another collection.
247
+
248
+ [source,ruby]
249
+ ----
250
+ self.collection_name=( _collection_name )
251
+ ----
252
+
253
+ * Result:
254
+ ** Collection name string.
255
+
256
+ * Parameter:
257
+ ** collection_name: Collection name.
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
+
205
285
  === Declare document structure.
206
286
 
207
287
  [source,ruby]
208
288
  ----
209
- field( label, *attrs, &block )
289
+ self.field( symbol, *attrs, **items )
210
290
  ----
211
291
 
212
292
  * Parameter:
213
- ** label: Field label symbol.
293
+ ** symbol: Field name.
214
294
  ** attrs: Field attributes.
215
295
  *** Class: Class for field verification.
216
296
  *** Proc: Proc for field verification.
217
297
  *** Range: Range for field verification.
218
298
  *** Array: Array for field verification.
219
- *** Symbol: Special delectives.
299
+ *** Symbol: Special directive symbol.
220
300
  **** must: Not nil nor empty.
221
- ** block: Returning default value.
301
+ ** items: Operation when saving.
302
+ *** default: Value or proc when undefined.
303
+ *** create: Value or proc when saving a new document.
304
+ *** update: Value or proc when saving update document.
222
305
 
223
306
  === Verify before save or assignment action.
224
307
 
225
308
  [source,ruby]
226
309
  ----
227
- verify( *syms, &block )
310
+ self.verify( *directives, &block )
228
311
  ----
229
312
 
230
313
  * Parameter:
231
- ** syms: Conditional symbols.
314
+ ** directives: Special directive symbol.
232
315
  *** strict: Verify that it is a defined item name.
233
316
  ** block: Describe the content that verifies each item value and returns the truth.
234
317
 
@@ -236,18 +319,18 @@ verify( *syms, &block )
236
319
 
237
320
  [source,ruby]
238
321
  ----
239
- index( *syms, **opts )
322
+ self.index( *symbols, **options )
240
323
  ----
241
324
 
242
325
  * Parameter:
243
- ** syms: Field label symbols.
244
- ** opts: Options for Mongo::Collection#indexes().
326
+ ** symbols: Field names.
327
+ ** options: Options for Mongo::Collection#indexes().
245
328
 
246
329
  === Verify field value is not nil nor empty.
247
330
 
248
331
  [source,ruby]
249
332
  ----
250
- having?( label )
333
+ self.having?( label )
251
334
  ----
252
335
 
253
336
  * Result:
@@ -256,6 +339,161 @@ having?( label )
256
339
  * Parameter:
257
340
  ** label: Field label for method call.
258
341
 
342
+ === Name the search condition.
343
+
344
+ [source,ruby]
345
+ ----
346
+ self.filter( symbol, filter_or_cond )
347
+ ----
348
+
349
+ * Parameter:
350
+ ** symbol: Filter name.
351
+ ** filter_or_cond: Filter or search criteria.
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
+
368
+ === Search condition.
369
+
370
+ [source,ruby]
371
+ ----
372
+ Mongous::Document.where( filter = nil, **conditions )
373
+ Mongous::Filter#where( filter = nil, **conditions )
374
+ ----
375
+
376
+ * Result:
377
+ ** Mongous::Filter instance.
378
+
379
+ * Parameter:
380
+ ** filter: Filter name symbol, or filter instance.
381
+ ** conditions: Search criteria.
382
+
383
+ === NOT search condition.
384
+
385
+ [source,ruby]
386
+ ----
387
+ Mongous::Document.not( filter = nil, **conditions )
388
+ Mongous::Filter#not( filter = nil, **conditions )
389
+ ----
390
+
391
+ * Result:
392
+ ** Mongous::Filter instance.
393
+
394
+ * Parameter:
395
+ ** filter: Filter name symbol, or filter instance.
396
+ ** conditions: Search criteria.
397
+
398
+ === AND search condition.
399
+
400
+ [source,ruby]
401
+ ----
402
+ Mongous::Document.and( *filters )
403
+ Mongous::Filter#and( *filters )
404
+ ----
405
+
406
+ * Result:
407
+ ** Mongous::Filter instance.
408
+
409
+ * Parameter:
410
+ ** filters: Filter name symbol, or filter instance.
411
+
412
+ === OR search condition.
413
+
414
+ [source,ruby]
415
+ ----
416
+ Mongous::Document.or( *filters )
417
+ Mongous::Filter#or( *filters )
418
+ ----
419
+
420
+ * Result:
421
+ ** Mongous::Filter instance.
422
+
423
+ * Parameter:
424
+ ** filters: Field name symbol, or filter instance.
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.
259
497
 
260
498
  == Contributing
261
499