mongous 0.1.6 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.adoc +296 -46
- data/README.ja.adoc +297 -47
- data/lib/mongous/base.rb +35 -2
- data/lib/mongous/document.rb +75 -32
- data/lib/mongous/extention.rb +72 -57
- data/lib/mongous/filter.rb +273 -208
- data/lib/mongous/version.rb +1 -1
- data/mongous.gemspec +1 -1
- data/sample/connect_auto_0.rb +9 -0
- data/sample/connect_auto_2.rb +2 -1
- data/sample/connect_default_2.rb +2 -1
- data/sample/connect_environ_2.rb +2 -1
- data/sample/connect_loadfile_2.rb +2 -1
- data/sample/declare_compact_1.rb +7 -7
- data/sample/declare_label_1.rb +10 -6
- data/sample/declare_ndex_1.rb +9 -5
- data/sample/declare_ndex_2.rb +10 -6
- data/sample/declare_strict_1.rb +14 -10
- data/sample/declare_strict_2.rb +5 -3
- data/sample/{multi_docs_1.rb → multi_collection_1.rb} +0 -0
- data/sample/multi_collection_2.rb +63 -0
- data/sample/multi_collection_3.rb +26 -0
- data/sample/query_basic_1.rb +3 -1
- data/sample/query_basic_2.rb +5 -3
- data/sample/query_basic_3.rb +2 -3
- data/sample/query_basic_4.rb +3 -4
- data/sample/query_basic_5.rb +2 -3
- data/sample/query_detail_1.rb +3 -1
- data/sample/query_detail_2.rb +3 -1
- data/sample/query_detail_3.rb +5 -3
- data/sample/{query_basic_6.rb → query_filter_1.rb} +7 -4
- data/sample/query_filter_2.rb +50 -0
- data/sample/{query_super_1.rb → query_find_1.rb} +0 -1
- data/sample/query_select_1.rb +51 -0
- data/sample/query_skip_limit_1.rb +47 -0
- data/sample/query_skip_limit_2.rb +49 -0
- data/sample/query_sort_order_1.rb +46 -0
- data/sample/save_basic_1.rb +1 -2
- data/sample/save_basic_2.rb +1 -2
- data/sample/save_basic_3.rb +1 -2
- data/sample/save_detail_1.rb +7 -4
- data/sample/save_detail_2.rb +7 -4
- data/sample/save_detail_3.rb +11 -8
- data/sample/save_verify_1.rb +7 -4
- data/sample/save_verify_3.rb +1 -1
- data/sample/save_verify_5.rb +3 -3
- data/sample/save_verify_6.rb +3 -3
- data/sample/save_verify_7.rb +23 -0
- data/sample/update_basic_1.rb +13 -0
- data/sample/zap_basic_2.rb +2 -2
- data/sample/zap_basic_3.rb +2 -2
- data/sample/zbenchmark_search_1.rb +110 -0
- data/sample/zbenchmark_search_2.rb +110 -0
- data/sample/zbenchmark_search_3.rb +90 -0
- metadata +19 -12
- data/sample/multi_docs_2.rb +0 -58
- data/sample/query_limit_1.rb +0 -44
- data/sample/query_order_1.rb +0 -49
- data/sample/query_projection_1.rb +0 -44
- data/sample/template_article.rb +0 -5
- data/sample/template_person.rb +0 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bf1d2cbfd528ed1a623833025cf965b5200a1f973ce1200609ae459e6167694f
|
4
|
+
data.tar.gz: 785aea052055755ec01bab88f47da0915371546d9c84f14a7876f968c79a5e3d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 706bcfdac572ee3864a70d47101a689142134f030f20c37123e33f64575bd046eea74a656bdce29487f93c441a4374d77b97e96989fd234940daaf81171ace19
|
7
|
+
data.tar.gz: e4c1f4dae468a78f376b2a89a612a7f419e39939b0b486451980857e24127e0420e2fb2dc13f8afe8a7e1cb6686a5c2c82ce3e1b6a7dc94609327f258a785d12
|
data/README.adoc
CHANGED
@@ -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
|
-
*
|
9
|
-
*
|
10
|
-
*
|
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.
|
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,
|
55
|
+
field :title, String, :must
|
58
56
|
field :author, String
|
59
57
|
field :publisher, String
|
60
|
-
field :style, String,
|
61
|
-
field :
|
62
|
-
field :
|
63
|
-
field :
|
64
|
-
field :isbn, String,
|
65
|
-
field :lang, String,
|
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.
|
89
|
+
book.size = "A4"
|
88
90
|
book.save
|
89
91
|
|
90
|
-
book = Book.new( title: "title 2", price: 2000,
|
92
|
+
book = Book.new( title: "title 2", price: 2000, size: "A5" )
|
91
93
|
book.save
|
92
94
|
|
93
|
-
doc = { title: "title 3", price: 3000,
|
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.
|
105
|
+
p book = Book.where( title: "title 1" ).first
|
104
106
|
|
105
|
-
books = Book.
|
107
|
+
books = Book.where( title: /title/ ).all
|
106
108
|
books.each do |book|
|
107
109
|
p book
|
108
110
|
end
|
109
111
|
|
110
|
-
Book.
|
112
|
+
Book.where( title: /title/ ).projection( _id: 0 ).each do |book|
|
111
113
|
p book
|
112
114
|
end
|
113
115
|
|
114
|
-
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.
|
119
|
-
filter2 = Book.
|
120
|
-
filter3 = Book.
|
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,
|
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
|
-
|
138
|
+
|
139
|
+
Book.where( title: /title/ ).select( _id: 0 ).each do |book|
|
128
140
|
p book
|
129
141
|
end
|
130
142
|
|
131
|
-
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.
|
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.
|
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, **
|
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
|
-
**
|
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
|
-
***
|
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, **
|
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
|
-
===
|
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(
|
286
|
+
self.field( symbol, *attrs, **items )
|
195
287
|
----
|
196
288
|
|
197
289
|
* Parameter:
|
198
|
-
**
|
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
|
296
|
+
*** Symbol: Special directive symbol.
|
205
297
|
**** must: Not nil nor empty.
|
206
|
-
**
|
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( *
|
307
|
+
self.verify( *directives, &block )
|
213
308
|
----
|
214
309
|
|
215
310
|
* Parameter:
|
216
|
-
**
|
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( *
|
319
|
+
self.index( *symbols, **options )
|
225
320
|
----
|
226
321
|
|
227
322
|
* Parameter:
|
228
|
-
**
|
229
|
-
**
|
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
|
|