mongous 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/README.adoc +142 -27
  3. data/README.ja.adoc +140 -25
  4. data/lib/mongous/document.rb +50 -27
  5. data/lib/mongous/extention.rb +56 -45
  6. data/lib/mongous/filter.rb +76 -57
  7. data/lib/mongous/version.rb +1 -1
  8. data/mongous.gemspec +3 -2
  9. data/sample/connect_auto_2.rb +2 -1
  10. data/sample/connect_default_2.rb +2 -1
  11. data/sample/connect_environ_2.rb +2 -1
  12. data/sample/connect_loadfile_2.rb +2 -1
  13. data/sample/declare_compact_1.rb +1 -1
  14. data/sample/declare_label_1.rb +1 -1
  15. data/sample/declare_ndex_1.rb +1 -1
  16. data/sample/declare_ndex_2.rb +1 -1
  17. data/sample/declare_strict_1.rb +7 -5
  18. data/sample/declare_strict_2.rb +3 -2
  19. data/sample/{multi_docs_1.rb → multi_collection_1.rb} +0 -0
  20. data/sample/multi_collection_2.rb +61 -0
  21. data/sample/query_basic_1.rb +3 -1
  22. data/sample/query_basic_2.rb +5 -3
  23. data/sample/query_basic_3.rb +2 -3
  24. data/sample/query_basic_4.rb +3 -4
  25. data/sample/query_basic_5.rb +2 -3
  26. data/sample/query_detail_3.rb +3 -2
  27. data/sample/{query_basic_6.rb → query_filter_1.rb} +8 -5
  28. data/sample/query_filter_2.rb +50 -0
  29. data/sample/query_find_1.rb +31 -0
  30. data/sample/query_projection_1.rb +5 -8
  31. data/sample/query_skip_limit_1.rb +37 -0
  32. data/sample/query_skip_limit_2.rb +49 -0
  33. data/sample/query_sort_order_1.rb +46 -0
  34. data/sample/save_basic_1.rb +0 -1
  35. data/sample/save_basic_2.rb +0 -1
  36. data/sample/save_basic_3.rb +0 -1
  37. data/sample/save_detail_1.rb +4 -3
  38. data/sample/save_detail_2.rb +4 -3
  39. data/sample/save_detail_3.rb +6 -5
  40. data/sample/save_verify_1.rb +3 -2
  41. data/sample/save_verify_3.rb +1 -1
  42. data/sample/zap_basic_2.rb +1 -1
  43. data/sample/zap_basic_3.rb +1 -1
  44. metadata +15 -14
  45. data/sample/multi_docs_2.rb +0 -58
  46. data/sample/query_limit_1.rb +0 -44
  47. data/sample/query_order_1.rb +0 -49
  48. data/sample/query_raw_1.rb +0 -33
  49. data/sample/template_article.rb +0 -5
  50. data/sample/template_person.rb +0 -12
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: de69281ec1896f88f5b8d5c7c30654a12559cf3a7c97ea7d58cd2fde6e8579fd
4
- data.tar.gz: 8db5720698450a5f9fcbf912fab5611d800588e02bfea09141c9bf23a1c0a088
3
+ metadata.gz: 26e437e93bf76ff87a088329b744df4f1425d7b14c106d80edb46f12bbfde12f
4
+ data.tar.gz: 4e6bb7dd48f0799ce33165629c658b174316fd9d271f4f51ae866b804607ae59
5
5
  SHA512:
6
- metadata.gz: 90d5962f8140f085afc07e9b9c9380a6c38aca825dd977d5ee6a1e8d1042ad39514a6185eb9fed38f9a370e794dea2b8c99b5d99adb661dfc0e9306806084bdd
7
- data.tar.gz: 9c22f0d55962fce5fead46e9d4e0c27d89fb4b87ca268cef9c123c8737d9b5ae8987748d158a3b39d3acf1c21175729005ebce917fdc0d9118c2fc1dd19623d1
6
+ metadata.gz: 392818352168bf2c196c90693559f421e35fe6ed15bdaa013a9a3f651a5dbda66f2eb0f4ed38c25e28c5d77c1c7d5122d8384c8de56bb701b896ffe2133b3ef0
7
+ data.tar.gz: 401266df4545a41f3b8d5e090f986dc2cd8215b48d153da195b8c276316a3e9ebed07b8a24e5dd677cc469d9676415a30ffb844f29211064c3d1fefa4c6badd5
@@ -30,7 +30,7 @@ Or install it yourself as:
30
30
 
31
31
  == Usage
32
32
 
33
- === Simple description
33
+ === Simple declaration
34
34
 
35
35
  [source,ruby]
36
36
  ----
@@ -60,9 +60,12 @@ class Book
60
60
  field :style, String, ["A4","B5","A5","B6"]
61
61
  field :price, Integer, (0..1_000_000)
62
62
  field :page, Integer, proc{ page % 4 == 0 }
63
- field :publish_at, Date, &proc{ Date.today }
64
63
  field :isbn, String, proc{ isbn? }
65
- field :lang, String, &proc{ "ja" }
64
+ field :lang, String, default: "en"
65
+ field :created_at, Time, create: proc{ Time.now }
66
+ field :updated_at, Time, update: proc{ Time.now }
67
+
68
+ filter :foobar, {title: /foobar/}
66
69
 
67
70
  verify :strict
68
71
  verify do
@@ -91,38 +94,57 @@ book = Book.new( title: "title 2", price: 2000, style: "A5" )
91
94
  book.save
92
95
 
93
96
  doc = { title: "title 3", price: 3000, style: "A6" }
94
- Book.create( doc )
97
+ Book.create( **doc )
95
98
  ----
96
99
 
97
100
  === Search document
98
101
 
99
102
  [source,ruby]
100
103
  ----
101
- books = Book.all
102
- pp books
104
+ pp books = Book.all
103
105
 
104
- book = Book.filter( title: "title 1" ).first
105
- p book
106
+ p book = Book.where( title: "title 1" ).first
106
107
 
107
- books = Book.filter( title: /title/ ).all
108
+ books = Book.where( title: /title/ ).all
108
109
  books.each do |book|
109
110
  p book
110
111
  end
111
112
 
112
- Book.filter( title: /title/ ).projection( _id: 0 ).each do |book|
113
+ Book.where( title: /title/ ).projection( _id: 0 ).each do |book|
113
114
  p book
114
115
  end
115
116
 
116
- Book.filter( price: (1..2000), style: ["A4","A5"] ).each do |book|
117
+ Book.where( price: (1..2000), style: ["A4","A5"] ).each do |book|
118
+ p book
119
+ end
120
+
121
+ filter1 = Book.where( title: /title/ )
122
+ filter2 = Book.where( :foobar )
123
+ filter3 = Book.where( price: (1..2000) )
124
+ filter4 = Book.where( style: ["A4","A5"] )
125
+
126
+ Book.not( filter1 ).each do |book|
117
127
  p book
118
128
  end
129
+ Book.and( filter1, filter3 ).each do |book|
130
+ p book
131
+ end
132
+ Book.or( filter2, filter4 ).each do |book|
133
+ p book
134
+ end
135
+
136
+ Book.find( { title: /title/ }, { projection: {_id: 0} } ).each do |book|
137
+ p book
138
+ end
139
+
140
+ pp Book.where( title: /title/ )[0, 5].all
119
141
  ----
120
142
 
121
143
  === Update document
122
144
 
123
145
  [source,ruby]
124
146
  ----
125
- book = Book.filter( title: "title 1" ).first
147
+ book = Book.where( title: "title 1" ).first
126
148
  book.title = "title 1 [update]"
127
149
  book.save
128
150
  ----
@@ -131,7 +153,7 @@ book.save
131
153
 
132
154
  [source,ruby]
133
155
  ----
134
- book = Book.filter( title: "title 1" ).first
156
+ book = Book.where( title: "title 1" ).first
135
157
  book.delete
136
158
  ----
137
159
 
@@ -141,7 +163,7 @@ book.delete
141
163
 
142
164
  [source,ruby]
143
165
  ----
144
- Mongous.connect!( hosts_or_uri = nil, **opts )
166
+ Mongous.connect!( hosts_or_uri = nil, **options )
145
167
  ----
146
168
 
147
169
  * Result:
@@ -149,21 +171,21 @@ Mongous.connect!( hosts_or_uri = nil, **opts )
149
171
 
150
172
  * Parameter:
151
173
  ** hosts_or_uri: Array of hosts, or URI (default: ["localhost:21017"])
152
- ** opts: Options.
174
+ ** options: Options.
153
175
  *** file: Path to database configuration file.
154
176
  *** mode: Execution mode. (default: "development")
155
177
  *** database: Database name. (default: "test")
156
- *** * Other optional arguments for Mongo::Client.new.
178
+ *** Other optional arguments for Mongo::Client.new.
157
179
 
158
180
  === Connect database.
159
181
 
160
182
  [source,ruby]
161
183
  ----
162
- Mongous.connect( hosts_or_uri = nil, **opts )
184
+ Mongous.connect( hosts_or_uri = nil, **options )
163
185
  ----
164
186
 
165
187
  * Result:
166
- ** Mongo::Client.
188
+ ** Mongo::Client instance.
167
189
 
168
190
  === Include document functions.
169
191
 
@@ -172,33 +194,62 @@ Mongous.connect( hosts_or_uri = nil, **opts )
172
194
  include Mongous::Document
173
195
  ----
174
196
 
197
+ === Bind another database.
198
+
199
+ [source,ruby]
200
+ ----
201
+ self.client=( _client )
202
+ ----
203
+
204
+ * Result:
205
+ ** Mongo::Client instance.
206
+
207
+ * Parameter:
208
+ ** client: Mongo::Client instance.
209
+
210
+ === Bind another collection.
211
+
212
+ [source,ruby]
213
+ ----
214
+ self.collection_name=( _collection_name )
215
+ ----
216
+
217
+ * Result:
218
+ ** Collection name string.
219
+
220
+ * Parameter:
221
+ ** collection_name: Collection name.
222
+
175
223
  === Declare document structure.
176
224
 
177
225
  [source,ruby]
178
226
  ----
179
- field( label, *attrs, &block )
227
+ field( symbol, *attrs, **items )
180
228
  ----
181
229
 
182
230
  * Parameter:
183
- ** label: Field label symbol.
231
+ ** symbol: Field name.
184
232
  ** attrs: Field attributes.
185
233
  *** Class: Class for field verification.
186
234
  *** Proc: Proc for field verification.
187
235
  *** Range: Range for field verification.
188
236
  *** Array: Array for field verification.
189
- *** Symbol: Special delectives.
237
+ *** Symbol: Special directive symbol.
190
238
  **** must: Not nil nor empty.
191
- ** block: Returning default value.
239
+ ** items: Operation when saving.
240
+ *** default: Value or proc when undefined.
241
+ *** create: Value or proc when saving a new document.
242
+ *** update: Value or proc when saving update document.
192
243
 
193
244
  === Verify before save or assignment action.
194
245
 
195
246
  [source,ruby]
196
247
  ----
197
- verify( *syms, &block )
248
+ verify( *directives, &block )
198
249
  ----
199
250
 
200
251
  * Parameter:
201
- ** syms: Conditional symbols.
252
+ ** directives: Special directive symbol.
202
253
  *** strict: Verify that it is a defined item name.
203
254
  ** block: Describe the content that verifies each item value and returns the truth.
204
255
 
@@ -206,12 +257,12 @@ verify( *syms, &block )
206
257
 
207
258
  [source,ruby]
208
259
  ----
209
- index( *syms, **opts )
260
+ index( *symbols, **options )
210
261
  ----
211
262
 
212
263
  * Parameter:
213
- ** syms: Field label symbols.
214
- ** opts: Options for Mongo::Collection#indexes().
264
+ ** symbols: Field names.
265
+ ** options: Options for Mongo::Collection#indexes().
215
266
 
216
267
  === Verify field value is not nil nor empty.
217
268
 
@@ -226,6 +277,70 @@ having?( label )
226
277
  * Parameter:
227
278
  ** label: Field label for method call.
228
279
 
280
+ === Name the search condition.
281
+
282
+ [source,ruby]
283
+ ----
284
+ filter( symbol, filter_or_cond )
285
+ ----
286
+
287
+ * Parameter:
288
+ ** symbol: Filter name.
289
+ ** filter_or_cond: Filter or search criteria.
290
+
291
+ === Search condition.
292
+
293
+ [source,ruby]
294
+ ----
295
+ where( filter = nil, **conditions )
296
+ ----
297
+
298
+ * Result:
299
+ ** Filter instance.
300
+
301
+ * Parameter:
302
+ ** filter: Filter name symbol, or filter instance.
303
+ ** conditions: Search criteria.
304
+
305
+ === NOT search condition.
306
+
307
+ [source,ruby]
308
+ ----
309
+ not( filter = nil, **conditions )
310
+ ----
311
+
312
+ * Result:
313
+ ** Filter instance.
314
+
315
+ * Parameter:
316
+ ** filter: Filter name symbol, or filter instance.
317
+ ** conditions: Search criteria.
318
+
319
+ === AND search condition.
320
+
321
+ [source,ruby]
322
+ ----
323
+ and( *filters )
324
+ ----
325
+
326
+ * Result:
327
+ ** Filter instance.
328
+
329
+ * Parameter:
330
+ ** filters: Filter name symbol, or filter instance.
331
+
332
+ === OR search condition.
333
+
334
+ [source,ruby]
335
+ ----
336
+ or( *filters )
337
+ ----
338
+
339
+ * Result:
340
+ ** Filter instance.
341
+
342
+ * Parameter:
343
+ ** filters: Field name symbol, or filter instance.
229
344
 
230
345
  == Contributing
231
346
 
@@ -60,9 +60,12 @@ class Book
60
60
  field :style, String, ["A4","B5","A5","B6"]
61
61
  field :price, Integer, (0..1_000_000)
62
62
  field :page, Integer, proc{ page % 4 == 0 }
63
- field :publish_at, Date, &proc{ Date.today }
64
63
  field :isbn, String, proc{ isbn? }
65
- field :lang, String, &proc{ "ja" }
64
+ field :lang, String, default: "en"
65
+ field :created_at, Time, create: proc{ Time.now }
66
+ field :updated_at, Time, update: proc{ Time.now }
67
+
68
+ filter :foobar, {title: /foobar/}
66
69
 
67
70
  verify :strict
68
71
  verify do
@@ -91,38 +94,57 @@ book = Book.new( title: "title 2", price: 2000, style: "A5" )
91
94
  book.save
92
95
 
93
96
  doc = { title: "title 3", price: 3000, style: "A6" }
94
- Book.create( doc )
97
+ Book.create( **doc )
95
98
  ----
96
99
 
97
100
  === ドキュメント検索
98
101
 
99
102
  [source,ruby]
100
103
  ----
101
- books = Book.all
102
- pp books
104
+ pp books = Book.all
103
105
 
104
- book = Book.filter( title: "title 1" ).first
105
- p book
106
+ p book = Book.where( title: "title 1" ).first
106
107
 
107
- books = Book.filter( title: /title/ ).all
108
+ books = Book.where( title: /title/ ).all
108
109
  books.each do |book|
109
110
  p book
110
111
  end
111
112
 
112
- Book.filter( title: /title/ ).projection( _id: 0 ).each do |book|
113
+ Book.where( title: /title/ ).projection( _id: 0 ).each do |book|
113
114
  p book
114
115
  end
115
116
 
116
- Book.filter( price: (1..2000), style: ["A4","A5"] ).each do |book|
117
+ Book.where( price: (1..2000), style: ["A4","A5"] ).each do |book|
118
+ p book
119
+ end
120
+
121
+ filter1 = Book.where( title: /title/ )
122
+ filter2 = Book.where( :foobar )
123
+ filter3 = Book.where( price: (1..2000) )
124
+ filter4 = Book.where( style: ["A4","A5"] )
125
+
126
+ Book.not( filter1 ).each do |book|
117
127
  p book
118
128
  end
129
+ Book.and( filter1, filter3 ).each do |book|
130
+ p book
131
+ end
132
+ Book.or( filter2, filter4 ).each do |book|
133
+ p book
134
+ end
135
+
136
+ Book.find( { title: /title/ }, { projection: {_id: 0} } ).each do |book|
137
+ p book
138
+ end
139
+
140
+ pp Book.where( title: /title/ )[0, 5].all
119
141
  ----
120
142
 
121
143
  === ドキュメント更新
122
144
 
123
145
  [source,ruby]
124
146
  ----
125
- book = Book.filter( title: "title 1" ).first
147
+ book = Book.where( title: "title 1" ).first
126
148
  book.title = "title 1 [update]"
127
149
  book.save
128
150
  ----
@@ -131,7 +153,7 @@ book.save
131
153
 
132
154
  [source,ruby]
133
155
  ----
134
- book = Book.filter( title: "title 1" ).first
156
+ book = Book.where( title: "title 1" ).first
135
157
  book.delete
136
158
  ----
137
159
 
@@ -141,7 +163,7 @@ book.delete
141
163
 
142
164
  [source,ruby]
143
165
  ----
144
- Mongous.connect!( hosts_or_uri = nil, **opts )
166
+ Mongous.connect!( hosts_or_uri = nil, **options )
145
167
  ----
146
168
 
147
169
  * Result:
@@ -149,21 +171,21 @@ Mongous.connect!( hosts_or_uri = nil, **opts )
149
171
 
150
172
  * Parameter:
151
173
  ** hosts_or_uri: ホスト配列または URI (default: ["localhost:21017"])
152
- ** opts: オプション
174
+ ** options: オプション
153
175
  *** file: データベース構成定義ファイルへのパス
154
176
  *** mode: 実行モード (default: "development")
155
177
  *** database: データベース名 (default: "test")
156
- *** * Mongo::Client.new のその他オプション引数
178
+ *** Mongo::Client.new のその他オプション引数
157
179
 
158
180
  === データベースに接続する
159
181
 
160
182
  [source,ruby]
161
183
  ----
162
- Mongous.connect( hosts_or_uri = nil, **opts )
184
+ Mongous.connect( hosts_or_uri = nil, **options )
163
185
  ----
164
186
 
165
187
  * Result:
166
- ** Mongo::Client.
188
+ ** Mongo::Client インスタンス.
167
189
 
168
190
  === ドキュメントの機能を取り入れる.
169
191
 
@@ -172,15 +194,41 @@ Mongous.connect( hosts_or_uri = nil, **opts )
172
194
  include Mongous::Document
173
195
  ----
174
196
 
197
+ === 別のデータベースを割り当てる.
198
+
199
+ [source,ruby]
200
+ ----
201
+ self.client=( client )
202
+ ----
203
+
204
+ * Result:
205
+ ** Mongo::Client インスタンス.
206
+
207
+ * Parameter:
208
+ ** client: Mongo::Client インスタンス.
209
+
210
+ === 別のコレクションを割り当てる.
211
+
212
+ [source,ruby]
213
+ ----
214
+ self.collection_name=( collection_name )
215
+ ----
216
+
217
+ * Result:
218
+ ** Collection name 文字列.
219
+
220
+ * Parameter:
221
+ ** collection_name: コレクション名.
222
+
175
223
  === ドキュメントの要素を定義する.
176
224
 
177
225
  [source,ruby]
178
226
  ----
179
- field( label, *attrs, &block )
227
+ field( symbol, *attrs, **items )
180
228
  ----
181
229
 
182
230
  * Parameter:
183
- ** label: 項目名シンボル
231
+ ** symbol: 項目名
184
232
  ** attrs: 項目属性
185
233
  *** Class: 項目検証用 Class
186
234
  *** Proc: 項目検証用 Proc
@@ -188,17 +236,20 @@ field( label, *attrs, &block )
188
236
  *** Array: 項目検証用配列
189
237
  *** Symbol: 特別な指示子
190
238
  **** must: ナル値でも空文字列でもない
191
- ** block: デフォルト値を返す
239
+ ** items: 保存時の操作.
240
+ *** default: 未定義のときの値または Proc.
241
+ *** create: ドキュメントを新規保存するときの値または Proc.
242
+ *** update: ドキュメントを更新するときの値または Proc.
192
243
 
193
244
  === 保存や代入の前にドキュメントの要素を検証する.
194
245
 
195
246
  [source,ruby]
196
247
  ----
197
- verify( *syms, &block )
248
+ verify( *directives, &block )
198
249
  ----
199
250
 
200
251
  * Parameter:
201
- ** syms: 条件シンボル
252
+ ** directives: 条件シンボル
202
253
  *** strict: 定義済み項目名であることを検証する.
203
254
  ** block: 各項目値を検証して真偽を返す内容を記述する.
204
255
 
@@ -206,12 +257,12 @@ verify( *syms, &block )
206
257
 
207
258
  [source,ruby]
208
259
  ----
209
- index( *syms, **opts )
260
+ index( *symbols, **options )
210
261
  ----
211
262
 
212
263
  * Parameter:
213
- ** syms: 項目名シンボル
214
- ** opts: Mongo::Collection#indexes() のオプション.
264
+ ** symbols: 項目名
265
+ ** options: Mongo::Collection#indexes() のオプション.
215
266
 
216
267
  === 項目値がナル値でも空文字列でもないことを検証する.
217
268
 
@@ -226,6 +277,70 @@ having?( label )
226
277
  * Parameter:
227
278
  ** label: メソッド呼び出しする項目名.
228
279
 
280
+ === 検索条件に名前をつける.
281
+
282
+ [source,ruby]
283
+ ----
284
+ filter( symbol, filter_or_cond )
285
+ ----
286
+
287
+ * Parameter:
288
+ ** symbol: 項目名
289
+ ** filter_or_cond: フィルタまたは検索条件
290
+
291
+ === 検索条件.
292
+
293
+ [source,ruby]
294
+ ----
295
+ where( filter = nil, **conditions )
296
+ ----
297
+
298
+ * Result:
299
+ ** Filter instance.
300
+
301
+ * Parameter:
302
+ ** filter: 項目名またはフィルタインスタンス
303
+ ** conditions: 検索条件
304
+
305
+ === 否定検索条件.
306
+
307
+ [source,ruby]
308
+ ----
309
+ not( filter = nil, **conditions )
310
+ ----
311
+
312
+ * Result:
313
+ ** Filter instance.
314
+
315
+ * Parameter:
316
+ ** filter: 項目名またはフィルタインスタンス
317
+ ** conditions: 検索条件
318
+
319
+ === 論理積検索条件.
320
+
321
+ [source,ruby]
322
+ ----
323
+ and( *filters )
324
+ ----
325
+
326
+ * Result:
327
+ ** Filter instance.
328
+
329
+ * Parameter:
330
+ ** filters: 項目名またはフィルタインスタンス
331
+
332
+ === 論理和検索条件.
333
+
334
+ [source,ruby]
335
+ ----
336
+ or( *filters )
337
+ ----
338
+
339
+ * Result:
340
+ ** Filter instance.
341
+
342
+ * Parameter:
343
+ ** filters: 項目名またはフィルタインスタンス
229
344
 
230
345
  == 貢献
231
346