mongous 0.1.6 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/README.adoc +296 -46
  3. data/README.ja.adoc +297 -47
  4. data/lib/mongous/base.rb +35 -2
  5. data/lib/mongous/document.rb +75 -32
  6. data/lib/mongous/extention.rb +72 -57
  7. data/lib/mongous/filter.rb +273 -208
  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/connect_auto_2.rb +2 -1
  12. data/sample/connect_default_2.rb +2 -1
  13. data/sample/connect_environ_2.rb +2 -1
  14. data/sample/connect_loadfile_2.rb +2 -1
  15. data/sample/declare_compact_1.rb +7 -7
  16. data/sample/declare_label_1.rb +10 -6
  17. data/sample/declare_ndex_1.rb +9 -5
  18. data/sample/declare_ndex_2.rb +10 -6
  19. data/sample/declare_strict_1.rb +14 -10
  20. data/sample/declare_strict_2.rb +5 -3
  21. data/sample/{multi_docs_1.rb → multi_collection_1.rb} +0 -0
  22. data/sample/multi_collection_2.rb +63 -0
  23. data/sample/multi_collection_3.rb +26 -0
  24. data/sample/query_basic_1.rb +3 -1
  25. data/sample/query_basic_2.rb +5 -3
  26. data/sample/query_basic_3.rb +2 -3
  27. data/sample/query_basic_4.rb +3 -4
  28. data/sample/query_basic_5.rb +2 -3
  29. data/sample/query_detail_1.rb +3 -1
  30. data/sample/query_detail_2.rb +3 -1
  31. data/sample/query_detail_3.rb +5 -3
  32. data/sample/{query_basic_6.rb → query_filter_1.rb} +7 -4
  33. data/sample/query_filter_2.rb +50 -0
  34. data/sample/{query_super_1.rb → query_find_1.rb} +0 -1
  35. data/sample/query_select_1.rb +51 -0
  36. data/sample/query_skip_limit_1.rb +47 -0
  37. data/sample/query_skip_limit_2.rb +49 -0
  38. data/sample/query_sort_order_1.rb +46 -0
  39. data/sample/save_basic_1.rb +1 -2
  40. data/sample/save_basic_2.rb +1 -2
  41. data/sample/save_basic_3.rb +1 -2
  42. data/sample/save_detail_1.rb +7 -4
  43. data/sample/save_detail_2.rb +7 -4
  44. data/sample/save_detail_3.rb +11 -8
  45. data/sample/save_verify_1.rb +7 -4
  46. data/sample/save_verify_3.rb +1 -1
  47. data/sample/save_verify_5.rb +3 -3
  48. data/sample/save_verify_6.rb +3 -3
  49. data/sample/save_verify_7.rb +23 -0
  50. data/sample/update_basic_1.rb +13 -0
  51. data/sample/zap_basic_2.rb +2 -2
  52. data/sample/zap_basic_3.rb +2 -2
  53. data/sample/zbenchmark_search_1.rb +110 -0
  54. data/sample/zbenchmark_search_2.rb +110 -0
  55. data/sample/zbenchmark_search_3.rb +90 -0
  56. metadata +19 -12
  57. data/sample/multi_docs_2.rb +0 -58
  58. data/sample/query_limit_1.rb +0 -44
  59. data/sample/query_order_1.rb +0 -49
  60. data/sample/query_projection_1.rb +0 -44
  61. data/sample/template_article.rb +0 -5
  62. data/sample/template_person.rb +0 -12
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '099aa151de9cf50a6c25b861ad877eafc6e16972f12f463181d735d8fb4ae71f'
4
- data.tar.gz: b41e34b02325dcb2933ec9e195452d5e2a027ee375d56cb0fe44a2144b275928
3
+ metadata.gz: bf1d2cbfd528ed1a623833025cf965b5200a1f973ce1200609ae459e6167694f
4
+ data.tar.gz: 785aea052055755ec01bab88f47da0915371546d9c84f14a7876f968c79a5e3d
5
5
  SHA512:
6
- metadata.gz: a058325e628afba439934ea18e82a2cbc80e4053c00aff7c7ca3326da6be2b588a37361152c359e4ed558f375ab2c7bc1e77f6fe8968a62bdc432733298fc660
7
- data.tar.gz: 8bc4c0bdef2be7e1c4bad582a5c0dc28f167fb0f99a7a74b800793b84c19db68a5a20564d8f7a6d6d41d210c78c78670bdd63ef12aec20f2375c56bb83237120
6
+ metadata.gz: 706bcfdac572ee3864a70d47101a689142134f030f20c37123e33f64575bd046eea74a656bdce29487f93c441a4374d77b97e96989fd234940daaf81171ace19
7
+ data.tar.gz: e4c1f4dae468a78f376b2a89a612a7f419e39939b0b486451980857e24127e0420e2fb2dc13f8afe8a7e1cb6686a5c2c82ce3e1b6a7dc94609327f258a785d12
@@ -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.attach! :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,44 +102,56 @@ 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 1" ).first
104
106
 
105
- books = Book.filter( title: /title/ ).all
107
+ books = Book.where( title: /title/ ).all
106
108
  books.each do |book|
107
109
  p book
108
110
  end
109
111
 
110
- Book.filter( title: /title/ ).projection( _id: 0 ).each do |book|
112
+ Book.where( title: /title/ ).projection( _id: 0 ).each do |book|
111
113
  p book
112
114
  end
113
115
 
114
- Book.filter( price: (1..2000), style: ["A4","A5"] ).each do |book|
116
+ Book.where( price: (1..2000), size: ["A4","A5"] ).each do |book|
115
117
  p book
116
118
  end
117
119
 
118
- filter1 = Book.filter( title: /title/ )
119
- filter2 = Book.filter( price: (1..2000) )
120
- filter3 = Book.filter( style: ["A4","A5"] )
120
+ filter1 = Book.where( title: /title/ )
121
+ filter2 = Book.where( :foobar )
122
+ filter3 = Book.where( price: (1..2000) )
123
+ filter4 = Book.where( size: ["A4","A5"] )
124
+
121
125
  Book.not( filter1 ).each do |book|
122
126
  p book
123
127
  end
124
- Book.and( filter1, filter2 ).each do |book|
128
+ Book.and( filter1, filter3 ).each do |book|
129
+ p book
130
+ end
131
+ Book.or( filter2, filter4 ).each do |book|
132
+ p book
133
+ end
134
+
135
+ Book.find( { title: /title/ }, { projection: {_id: 0} } ).each do |book|
125
136
  p book
126
137
  end
127
- Book.or( filter1, filter3 ).each do |book|
138
+
139
+ Book.where( title: /title/ ).select( _id: 0 ).each do |book|
128
140
  p book
129
141
  end
130
142
 
131
- Book.find( {}, { projection: {_id: 0} } ).each do |book|
143
+ Book.select( _id: 0 )[0, 5].each do |book|
132
144
  p book
133
145
  end
146
+
147
+ pp Book.select( :title, :price, :size )[5, 5].all
134
148
  ----
135
149
 
136
150
  === Update document
137
151
 
138
152
  [source,ruby]
139
153
  ----
140
- book = Book.filter( title: "title 1" ).first
154
+ book = Book.where( title: "title 1" ).first
141
155
  book.title = "title 1 [update]"
142
156
  book.save
143
157
  ----
@@ -146,7 +160,7 @@ book.save
146
160
 
147
161
  [source,ruby]
148
162
  ----
149
- book = Book.filter( title: "title 1" ).first
163
+ book = Book.where( title: "title 1" ).first
150
164
  book.delete
151
165
  ----
152
166
 
@@ -156,7 +170,7 @@ book.delete
156
170
 
157
171
  [source,ruby]
158
172
  ----
159
- Mongous.connect!( hosts_or_uri = nil, **opts )
173
+ Mongous.connect!( hosts_or_uri = nil, **options )
160
174
  ----
161
175
 
162
176
  * Result:
@@ -164,56 +178,137 @@ Mongous.connect!( hosts_or_uri = nil, **opts )
164
178
 
165
179
  * Parameter:
166
180
  ** hosts_or_uri: Array of hosts, or URI (default: ["localhost:21017"])
167
- ** opts: Options.
181
+ ** options: Options.
168
182
  *** file: Path to database configuration file.
169
183
  *** mode: Execution mode. (default: "development")
170
184
  *** database: Database name. (default: "test")
171
- *** * Other optional arguments for Mongo::Client.new.
185
+ *** Other optional arguments for Mongo::Client.new.
172
186
 
173
187
  === Connect database.
174
188
 
175
189
  [source,ruby]
176
190
  ----
177
- Mongous.connect( hosts_or_uri = nil, **opts )
191
+ Mongous.connect( hosts_or_uri = nil, **options )
178
192
  ----
179
193
 
180
194
  * Result:
181
- ** Mongo::Client.
195
+ ** Mongo::Client instance.
182
196
 
183
- === Include document functions.
197
+ === Define collection operate class with default settings.
198
+
199
+ [source,ruby]
200
+ ----
201
+ Mongous.attach!( *names )
202
+ ----
203
+
204
+ * Result:
205
+ ** nil.
206
+
207
+ * Parameter:
208
+ ** names: Collection names. (String or Symbol)
209
+
210
+ === Include document functions into collection operate class.
184
211
 
185
212
  [source,ruby]
186
213
  ----
187
214
  include Mongous::Document
188
215
  ----
189
216
 
217
+ === Bind another database.
218
+
219
+ [source,ruby]
220
+ ----
221
+ self.client=( client )
222
+ ----
223
+
224
+ * Result:
225
+ ** Mongo::Client instance.
226
+
227
+ * Parameter:
228
+ ** client: Mongo::Client instance.
229
+
230
+ === Get binded database.
231
+
232
+ [source,ruby]
233
+ ----
234
+ self.client
235
+ ----
236
+
237
+ * Result:
238
+ ** Mongo::Client instance.
239
+
240
+ * Parameter:
241
+ ** None.
242
+
243
+ === Bind another collection.
244
+
245
+ [source,ruby]
246
+ ----
247
+ self.collection_name=( _collection_name )
248
+ ----
249
+
250
+ * Result:
251
+ ** Collection name string.
252
+
253
+ * Parameter:
254
+ ** collection_name: Collection name.
255
+
256
+ === Get binded collection name.
257
+
258
+ [source,ruby]
259
+ ----
260
+ self.collection_name
261
+ ----
262
+
263
+ * Result:
264
+ ** Collection name string.
265
+
266
+ * Parameter:
267
+ ** None.
268
+
269
+ === Get collection.
270
+
271
+ [source,ruby]
272
+ ----
273
+ self.collection( collection_name = nil )
274
+ ----
275
+
276
+ * Result:
277
+ ** Mongo::Collection instance.
278
+
279
+ * Parameter:
280
+ ** collection_name: Tempolary collection name.
281
+
190
282
  === Declare document structure.
191
283
 
192
284
  [source,ruby]
193
285
  ----
194
- field( label, *attrs, &block )
286
+ self.field( symbol, *attrs, **items )
195
287
  ----
196
288
 
197
289
  * Parameter:
198
- ** label: Field label symbol.
290
+ ** symbol: Field name.
199
291
  ** attrs: Field attributes.
200
292
  *** Class: Class for field verification.
201
293
  *** Proc: Proc for field verification.
202
294
  *** Range: Range for field verification.
203
295
  *** Array: Array for field verification.
204
- *** Symbol: Special delectives.
296
+ *** Symbol: Special directive symbol.
205
297
  **** must: Not nil nor empty.
206
- ** block: Returning default value.
298
+ ** items: Operation when saving.
299
+ *** default: Value or proc when undefined.
300
+ *** create: Value or proc when saving a new document.
301
+ *** update: Value or proc when saving update document.
207
302
 
208
303
  === Verify before save or assignment action.
209
304
 
210
305
  [source,ruby]
211
306
  ----
212
- verify( *syms, &block )
307
+ self.verify( *directives, &block )
213
308
  ----
214
309
 
215
310
  * Parameter:
216
- ** syms: Conditional symbols.
311
+ ** directives: Special directive symbol.
217
312
  *** strict: Verify that it is a defined item name.
218
313
  ** block: Describe the content that verifies each item value and returns the truth.
219
314
 
@@ -221,18 +316,18 @@ verify( *syms, &block )
221
316
 
222
317
  [source,ruby]
223
318
  ----
224
- index( *syms, **opts )
319
+ self.index( *symbols, **options )
225
320
  ----
226
321
 
227
322
  * Parameter:
228
- ** syms: Field label symbols.
229
- ** opts: Options for Mongo::Collection#indexes().
323
+ ** symbols: Field names.
324
+ ** options: Options for Mongo::Collection#indexes().
230
325
 
231
326
  === Verify field value is not nil nor empty.
232
327
 
233
328
  [source,ruby]
234
329
  ----
235
- having?( label )
330
+ self.having?( label )
236
331
  ----
237
332
 
238
333
  * Result:
@@ -241,6 +336,161 @@ having?( label )
241
336
  * Parameter:
242
337
  ** label: Field label for method call.
243
338
 
339
+ === Name the search condition.
340
+
341
+ [source,ruby]
342
+ ----
343
+ self.filter( symbol, filter_or_cond )
344
+ ----
345
+
346
+ * Parameter:
347
+ ** symbol: Filter name.
348
+ ** filter_or_cond: Filter or search criteria.
349
+
350
+ === Select output fields.
351
+
352
+ [source,ruby]
353
+ ----
354
+ Mongous::Document.select( *keys, **kwargs )
355
+ Mongous::Filter#select( *keys, **kwargs )
356
+ ----
357
+
358
+ * Result:
359
+ ** Mongous::Filter instance.
360
+
361
+ * Parameter:
362
+ ** keys: Field symbols.
363
+ ** kwargs: Field symbols and values.
364
+
365
+ === Search condition.
366
+
367
+ [source,ruby]
368
+ ----
369
+ Mongous::Document.where( filter = nil, **conditions )
370
+ Mongous::Filter#where( filter = nil, **conditions )
371
+ ----
372
+
373
+ * Result:
374
+ ** Mongous::Filter instance.
375
+
376
+ * Parameter:
377
+ ** filter: Filter name symbol, or filter instance.
378
+ ** conditions: Search criteria.
379
+
380
+ === NOT search condition.
381
+
382
+ [source,ruby]
383
+ ----
384
+ Mongous::Document.not( filter = nil, **conditions )
385
+ Mongous::Filter#not( filter = nil, **conditions )
386
+ ----
387
+
388
+ * Result:
389
+ ** Mongous::Filter instance.
390
+
391
+ * Parameter:
392
+ ** filter: Filter name symbol, or filter instance.
393
+ ** conditions: Search criteria.
394
+
395
+ === AND search condition.
396
+
397
+ [source,ruby]
398
+ ----
399
+ Mongous::Document.and( *filters )
400
+ Mongous::Filter#and( *filters )
401
+ ----
402
+
403
+ * Result:
404
+ ** Mongous::Filter instance.
405
+
406
+ * Parameter:
407
+ ** filters: Filter name symbol, or filter instance.
408
+
409
+ === OR search condition.
410
+
411
+ [source,ruby]
412
+ ----
413
+ Mongous::Document.or( *filters )
414
+ Mongous::Filter#or( *filters )
415
+ ----
416
+
417
+ * Result:
418
+ ** Mongous::Filter instance.
419
+
420
+ * Parameter:
421
+ ** filters: Field name symbol, or filter instance.
422
+
423
+ === Create document.
424
+
425
+ [source,ruby]
426
+ ----
427
+ Mongous::Document.create( **doc )
428
+ ----
429
+
430
+ * Result:
431
+ ** nil.
432
+
433
+ * Parameter:
434
+ ** doc: Keyword arguments.
435
+
436
+ === Save document.
437
+
438
+ [source,ruby]
439
+ ----
440
+ Mongous::Document#save
441
+ ----
442
+
443
+ * Result:
444
+ ** nil.
445
+
446
+ === Convert document to Hash.
447
+
448
+ [source,ruby]
449
+ ----
450
+ Mongous::Document#to_hash
451
+ ----
452
+
453
+ * Result:
454
+ ** Hash object.
455
+
456
+ === Convert document to JSON.
457
+
458
+ [source,ruby]
459
+ ----
460
+ Mongous::Document#to_json
461
+ ----
462
+
463
+ * Result:
464
+ ** JSON String.
465
+
466
+ === Read document field.
467
+
468
+ [source,ruby]
469
+ ----
470
+ Mongous::Document#[]( field_name )
471
+ Mongous::Document#field_name
472
+ ----
473
+
474
+ * Result:
475
+ ** field_value.
476
+
477
+ * Parameter:
478
+ ** field_name: Field name.
479
+
480
+ === Write document field.
481
+
482
+ [source,ruby]
483
+ ----
484
+ Mongous::Document#[]=( field_name, field_value )
485
+ Mongous::Document#field_name = field_value
486
+ ----
487
+
488
+ * Result:
489
+ ** field_value.
490
+
491
+ * Parameter:
492
+ ** field_name: Field name.
493
+ ** field_value: Field value.
244
494
 
245
495
  == Contributing
246
496