mongous 0.2.0 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -4,10 +4,12 @@
4
4
 
5
5
  == 特徴
6
6
 
7
- * 組込みと標準添付を除いて、mongo driver と bson のみに依存する軽いライブラリである。
8
- * 項目デフォルト値をブロックで記述できる.
9
- * 項目制約条件を Array, Range, Proc で記述できる.
10
- * 保存時および値設定時に制約条件を満たすか検査する.
7
+ * 組込みと標準添付を除いて、mongo driver と bson のみに依存する軽いライブラリである.
8
+ * ドキュメント項目の制約条件を Array, Range, Regexp, Proc インスタンスまたは基本クラスで記述できる.
9
+ * ドキュメント保存時に制約条件を満たすか検査する.
10
+ * ドキュメントの項目値設定時に制約条件を満たすか検査する.
11
+ * ドキュメントの未定義項目の内容を値または Proc で記述できる.
12
+ * ドキュメント作成時および更新時の項目内容を値または Proc で記述できる.
11
13
 
12
14
  == 導入
13
15
 
@@ -30,17 +32,13 @@ gem 'mongous'
30
32
 
31
33
  == 使い方
32
34
 
33
- === 簡素な定義
35
+ === 簡潔な定義
34
36
 
35
37
  [source,ruby]
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
  === 詳細な定義
@@ -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
  === ドキュメント更新
@@ -187,7 +195,22 @@ Mongous.connect( hosts_or_uri = nil, **options )
187
195
  * Result:
188
196
  ** Mongo::Client インスタンス.
189
197
 
190
- === ドキュメントの機能を取り入れる.
198
+ === コレクション操作クラスをデフォルト設定で定義する.
199
+
200
+ [source,ruby]
201
+ ----
202
+ Mongous.document!( *names, **options )
203
+ ----
204
+
205
+ * Result:
206
+ ** nil.
207
+
208
+ * Parameter:
209
+ ** names: コレクション名. (String または Symbol)
210
+ ** options: オプション
211
+ *** timestamp: 真のとき, フィールド :created_at, :updated_at を追加する.
212
+
213
+ === コレクション操作クラスにドキュメントの機能を取り入れる.
191
214
 
192
215
  [source,ruby]
193
216
  ----
@@ -207,6 +230,19 @@ self.client=( client )
207
230
  * Parameter:
208
231
  ** client: Mongo::Client インスタンス.
209
232
 
233
+ === Get binded database.
234
+
235
+ [source,ruby]
236
+ ----
237
+ self.client
238
+ ----
239
+
240
+ * Result:
241
+ ** Mongo::Client インスタンス.
242
+
243
+ * Parameter:
244
+ ** None.
245
+
210
246
  === 別のコレクションを割り当てる.
211
247
 
212
248
  [source,ruby]
@@ -220,11 +256,37 @@ self.collection_name=( collection_name )
220
256
  * Parameter:
221
257
  ** 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 文字列.
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 インスタンス.
281
+
282
+ * Parameter:
283
+ ** collection_name: 一時指定するコレクション名.
284
+
223
285
  === ドキュメントの要素を定義する.
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: 項目名
289
351
  ** filter_or_cond: フィルタまたは検索条件
290
352
 
353
+ === 出力項目を選択する.
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: 項目名
366
+ ** kwargs: 項目名と項目値.
367
+
291
368
  === 検索条件.
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: 項目名またはフィルタインスタンス
@@ -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: 項目名またはフィルタインスタンス
@@ -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: 項目名またはフィルタインスタンス
@@ -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: 項目名またはフィルタインスタンス
344
425
 
426
+ === ドキュメントを作成.
427
+
428
+ [source,ruby]
429
+ ----
430
+ Mongous::Document.create( **doc )
431
+ ----
432
+
433
+ * Result:
434
+ ** nil.
435
+
436
+ * Parameter:
437
+ ** doc: キーワード引数.
438
+
439
+ === ドキュメントを保存.
440
+
441
+ [source,ruby]
442
+ ----
443
+ Mongous::Document#save
444
+ ----
445
+
446
+ * Result:
447
+ ** nil.
448
+
449
+ === ドキュメントをHashに変換.
450
+
451
+ [source,ruby]
452
+ ----
453
+ Mongous::Document#to_hash
454
+ ----
455
+
456
+ * Result:
457
+ ** Hash object.
458
+
459
+ === ドキュメントをJSONに変換.
460
+
461
+ [source,ruby]
462
+ ----
463
+ Mongous::Document#to_json
464
+ ----
465
+
466
+ * Result:
467
+ ** JSON String.
468
+
469
+ === ドキュメントの項目値を読む.
470
+
471
+ [source,ruby]
472
+ ----
473
+ Mongous::Document#[]( field_name )
474
+ Mongous::Document#field_name
475
+ ----
476
+
477
+ * Result:
478
+ ** 項目値.
479
+
480
+ * Parameter:
481
+ ** field_name: 項目名.
482
+
483
+ === ドキュメントの項目値を書く.
484
+
485
+ [source,ruby]
486
+ ----
487
+ Mongous::Document#[]=( field_name, field_value )
488
+ Mongous::Document#field_name = field_value
489
+ ----
490
+
491
+ * Result:
492
+ ** 項目値.
493
+
494
+ * Parameter:
495
+ ** field_name: 項目名.
496
+ ** field_value: 項目値.
497
+
345
498
  == 貢献
346
499
 
347
500
  不具合報告とプルリクエストは GitHub https://github.com/arimay/mongous まで.
data/Rakefile CHANGED
@@ -13,7 +13,7 @@ class Bundler::GemHelper
13
13
  dest_path = File.join(dir, "#{name}-#{version}.zip")
14
14
  cmnd = "git archive --format zip --prefix=#{name}/ HEAD > #{dest_path}"
15
15
 
16
- out, code = sh_with_status( cmnd )
16
+ _, code = sh_with_status( cmnd )
17
17
  raise "Couldn't archive gem," unless code == 0
18
18
 
19
19
  Bundler.ui.confirm "#{name} #{version} archived to #{dest_path}."
@@ -23,11 +23,11 @@ class Bundler::GemHelper
23
23
  ver = version.to_s
24
24
 
25
25
  cmnd = "git push origin #{ver} "
26
- out, code = sh_with_status( cmnd )
26
+ _, code = sh_with_status( cmnd )
27
27
  raise "Couldn't git push origin." unless code == 0
28
28
 
29
29
  cmnd = "git push "
30
- out, code = sh_with_status( cmnd )
30
+ _, code = sh_with_status( cmnd )
31
31
  raise "Couldn't git push." unless code == 0
32
32
 
33
33
  Bundler.ui.confirm "Git Push #{ver}."
@@ -43,15 +43,15 @@ class Bundler::GemHelper
43
43
  end
44
44
 
45
45
  cmnd = "git add #{version_pathname} "
46
- out, code = sh_with_status( cmnd )
46
+ _, code = sh_with_status( cmnd )
47
47
  raise "Couldn't git add," unless code == 0
48
48
 
49
49
  cmnd = "git commit -m '#{new_version}' "
50
- out, code = sh_with_status( cmnd )
50
+ _, code = sh_with_status( cmnd )
51
51
  raise "Couldn't git commit." unless code == 0
52
52
 
53
53
  cmnd = "git tag #{new_version} "
54
- out, code = sh_with_status( cmnd )
54
+ _, code = sh_with_status( cmnd )
55
55
  raise "Couldn't git tag." unless code == 0
56
56
 
57
57
  Bundler.ui.confirm "Update Tags to #{new_version}."
@@ -80,14 +80,14 @@ Bundler::GemHelper.new(Dir.pwd).instance_eval do
80
80
 
81
81
  desc "Update Version Minor"
82
82
  task 'minor' do
83
- major, minor, tiny = version.to_s.split('.')
83
+ major, minor, _tiny = version.to_s.split('.')
84
84
  new_version = [major, minor.to_i + 1, 0].join('.')
85
85
  update_version( new_version )
86
86
  end
87
87
 
88
88
  desc "Update Version Major"
89
89
  task 'major' do
90
- major, minor, tiny = version.to_s.split('.')
90
+ major, _minor, _tiny = version.to_s.split('.')
91
91
  new_version = [major.to_i + 1, 0, 0].join('.')
92
92
  update_version( new_version )
93
93
  end
@@ -11,7 +11,7 @@ module Mongous
11
11
  def connect( hosts_or_uri = nil, file: nil, mode: nil, loglevel: nil, **options )
12
12
  case hosts_or_uri
13
13
  when Array, String
14
- ;
14
+ hosts_or_uri
15
15
 
16
16
  when NilClass
17
17
  if file
@@ -46,19 +46,63 @@ module Mongous
46
46
  end
47
47
 
48
48
  def connect!( hosts_or_uri = nil, **options )
49
- _client = connect( hosts_or_uri, **options )
50
- self.class_variable_set( :@@client, _client )
49
+ client_ = connect( hosts_or_uri, **options )
50
+ self.class_variable_set( :@@client, client_ )
51
51
  end
52
52
 
53
+ def document!( *names, **opts )
54
+ raise Mongous::Error, "missing argument." if names.empty?
55
+
56
+ names.each do |name|
57
+ case name
58
+ when String
59
+ name
60
+ when Symbol
61
+ name = name.to_s
62
+ else
63
+ raise Mongous::Error, "type invalid. : #{ name }"
64
+ end
65
+
66
+ raise Mongous::Error, "missing argument." unless /^[A-Z]/.match(name)
67
+
68
+ if Object.const_defined?( name )
69
+ if Object.const_get( name ).include?( Mongous::Document )
70
+ Object.class_eval{ remove_const( name ) }
71
+ else
72
+ raise Mongous::Error, "type invalid. : #{ Object.class_eval(name) }"
73
+ end
74
+ end
75
+
76
+ klass = Object.class_eval <<-CLASS
77
+ class #{name}
78
+ include Mongous::Document
79
+ end
80
+ CLASS
81
+
82
+ if opts[:timestamp]
83
+ klass.class_eval do
84
+ field :created_at, Time, create: proc{ Time.now }
85
+ field :updated_at, Time, update: proc{ Time.now }
86
+ index :created_at
87
+ index :updated_at
88
+ end
89
+ end
90
+ end
91
+ end
92
+ alias :attach! :document!
93
+
53
94
  def client
54
- self.class_variable_get( :@@client ) rescue nil
95
+ if not self.class_variable_defined?( :@@client )
96
+ connect!
97
+ end
98
+ self.class_variable_get( :@@client )
55
99
  end
56
100
 
57
- def client=( _client )
58
- if !_client.is_a?( ::Mongo::Client )
59
- raise Mongous::Error, "type invalid. : #{ _client }"
101
+ def client=( new_client )
102
+ if not new_client.is_a?( ::Mongo::Client )
103
+ raise Mongous::Error, "type invalid. : #{ new_client }"
60
104
  end
61
- self.class_variable_set( :@@client, _client )
105
+ self.class_variable_set( :@@client, new_client )
62
106
  end
63
107
 
64
108
  def loger