mongous 0.1.8 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/README.adoc +115 -30
  3. data/README.ja.adoc +115 -30
  4. data/lib/mongous/document.rb +50 -27
  5. data/lib/mongous/extention.rb +50 -43
  6. data/lib/mongous/filter.rb +45 -51
  7. data/lib/mongous/version.rb +1 -1
  8. data/sample/declare_compact_1.rb +1 -1
  9. data/sample/declare_label_1.rb +1 -1
  10. data/sample/declare_ndex_1.rb +1 -1
  11. data/sample/declare_ndex_2.rb +1 -1
  12. data/sample/declare_strict_1.rb +7 -5
  13. data/sample/declare_strict_2.rb +3 -2
  14. data/sample/{multi_docs_1.rb → multi_collection_1.rb} +0 -0
  15. data/sample/multi_collection_2.rb +61 -0
  16. data/sample/query_basic_1.rb +0 -1
  17. data/sample/query_basic_2.rb +4 -5
  18. data/sample/query_basic_3.rb +2 -3
  19. data/sample/query_basic_4.rb +3 -4
  20. data/sample/query_basic_5.rb +2 -3
  21. data/sample/query_detail_3.rb +3 -2
  22. data/sample/{query_basic_6.rb → query_filter_1.rb} +7 -4
  23. data/sample/query_filter_2.rb +50 -0
  24. data/sample/{query_super_1.rb → query_find_1.rb} +0 -1
  25. data/sample/query_projection_1.rb +5 -8
  26. data/sample/query_skip_limit_1.rb +37 -0
  27. data/sample/query_skip_limit_2.rb +49 -0
  28. data/sample/query_sort_order_1.rb +46 -0
  29. data/sample/save_basic_1.rb +0 -1
  30. data/sample/save_basic_2.rb +0 -1
  31. data/sample/save_basic_3.rb +0 -1
  32. data/sample/save_detail_1.rb +4 -3
  33. data/sample/save_detail_2.rb +4 -3
  34. data/sample/save_detail_3.rb +6 -5
  35. data/sample/save_verify_1.rb +3 -2
  36. data/sample/save_verify_3.rb +1 -1
  37. data/sample/zap_basic_2.rb +1 -1
  38. data/sample/zap_basic_3.rb +1 -1
  39. metadata +10 -11
  40. data/sample/multi_docs_2.rb +0 -58
  41. data/sample/query_basic_7.rb +0 -38
  42. data/sample/query_limit_1.rb +0 -44
  43. data/sample/query_order_1.rb +0 -49
  44. data/sample/template_article.rb +0 -5
  45. 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: 26e437e93bf76ff87a088329b744df4f1425d7b14c106d80edb46f12bbfde12f
4
+ data.tar.gz: 4e6bb7dd48f0799ce33165629c658b174316fd9d271f4f51ae866b804607ae59
5
5
  SHA512:
6
- metadata.gz: ce0e4ff3b90f13ac1bdb328ecac4c8986120e780575dbb97a242638d2cfd2c11581963cc2141c2f09f3a184cdcef85420354f4a12d02eca890c66b1458c9ea8e
7
- data.tar.gz: 6e78c1a620e8cdc974e9e60bca01f54b0127b225a7e1e0f567c7354ec7d43adf1cd2e3ffa72859f6805ffcacd8b3a198258763a4f14ab0f216b6f4d585d56e0d
6
+ metadata.gz: 392818352168bf2c196c90693559f421e35fe6ed15bdaa013a9a3f651a5dbda66f2eb0f4ed38c25e28c5d77c1c7d5122d8384c8de56bb701b896ffe2133b3ef0
7
+ data.tar.gz: 401266df4545a41f3b8d5e090f986dc2cd8215b48d153da195b8c276316a3e9ebed07b8a24e5dd677cc469d9676415a30ffb844f29211064c3d1fefa4c6badd5
@@ -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
@@ -100,31 +103,33 @@ Book.create( **doc )
100
103
  ----
101
104
  pp books = Book.all
102
105
 
103
- p book = Book.filter( title: "title 1" ).first
106
+ p book = Book.where( title: "title 1" ).first
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), style: ["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( style: ["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,14 @@ 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
+ pp Book.where( title: /title/ )[0, 5].all
136
141
  ----
137
142
 
138
143
  === Update document
139
144
 
140
145
  [source,ruby]
141
146
  ----
142
- book = Book.filter( title: "title 1" ).first
147
+ book = Book.where( title: "title 1" ).first
143
148
  book.title = "title 1 [update]"
144
149
  book.save
145
150
  ----
@@ -148,7 +153,7 @@ book.save
148
153
 
149
154
  [source,ruby]
150
155
  ----
151
- book = Book.filter( title: "title 1" ).first
156
+ book = Book.where( title: "title 1" ).first
152
157
  book.delete
153
158
  ----
154
159
 
@@ -158,7 +163,7 @@ book.delete
158
163
 
159
164
  [source,ruby]
160
165
  ----
161
- Mongous.connect!( hosts_or_uri = nil, **opts )
166
+ Mongous.connect!( hosts_or_uri = nil, **options )
162
167
  ----
163
168
 
164
169
  * Result:
@@ -166,21 +171,21 @@ Mongous.connect!( hosts_or_uri = nil, **opts )
166
171
 
167
172
  * Parameter:
168
173
  ** hosts_or_uri: Array of hosts, or URI (default: ["localhost:21017"])
169
- ** opts: Options.
174
+ ** options: Options.
170
175
  *** file: Path to database configuration file.
171
176
  *** mode: Execution mode. (default: "development")
172
177
  *** database: Database name. (default: "test")
173
- *** * Other optional arguments for Mongo::Client.new.
178
+ *** Other optional arguments for Mongo::Client.new.
174
179
 
175
180
  === Connect database.
176
181
 
177
182
  [source,ruby]
178
183
  ----
179
- Mongous.connect( hosts_or_uri = nil, **opts )
184
+ Mongous.connect( hosts_or_uri = nil, **options )
180
185
  ----
181
186
 
182
187
  * Result:
183
- ** Mongo::Client.
188
+ ** Mongo::Client instance.
184
189
 
185
190
  === Include document functions.
186
191
 
@@ -193,42 +198,58 @@ include Mongous::Document
193
198
 
194
199
  [source,ruby]
195
200
  ----
196
- set_client( client )
201
+ self.client=( _client )
197
202
  ----
198
203
 
199
204
  * Result:
200
- ** nil.
205
+ ** Mongo::Client instance.
201
206
 
202
207
  * Parameter:
203
208
  ** client: Mongo::Client instance.
204
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
+
205
223
  === Declare document structure.
206
224
 
207
225
  [source,ruby]
208
226
  ----
209
- field( label, *attrs, &block )
227
+ field( symbol, *attrs, **items )
210
228
  ----
211
229
 
212
230
  * Parameter:
213
- ** label: Field label symbol.
231
+ ** symbol: Field name.
214
232
  ** attrs: Field attributes.
215
233
  *** Class: Class for field verification.
216
234
  *** Proc: Proc for field verification.
217
235
  *** Range: Range for field verification.
218
236
  *** Array: Array for field verification.
219
- *** Symbol: Special delectives.
237
+ *** Symbol: Special directive symbol.
220
238
  **** must: Not nil nor empty.
221
- ** 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.
222
243
 
223
244
  === Verify before save or assignment action.
224
245
 
225
246
  [source,ruby]
226
247
  ----
227
- verify( *syms, &block )
248
+ verify( *directives, &block )
228
249
  ----
229
250
 
230
251
  * Parameter:
231
- ** syms: Conditional symbols.
252
+ ** directives: Special directive symbol.
232
253
  *** strict: Verify that it is a defined item name.
233
254
  ** block: Describe the content that verifies each item value and returns the truth.
234
255
 
@@ -236,12 +257,12 @@ verify( *syms, &block )
236
257
 
237
258
  [source,ruby]
238
259
  ----
239
- index( *syms, **opts )
260
+ index( *symbols, **options )
240
261
  ----
241
262
 
242
263
  * Parameter:
243
- ** syms: Field label symbols.
244
- ** opts: Options for Mongo::Collection#indexes().
264
+ ** symbols: Field names.
265
+ ** options: Options for Mongo::Collection#indexes().
245
266
 
246
267
  === Verify field value is not nil nor empty.
247
268
 
@@ -256,6 +277,70 @@ having?( label )
256
277
  * Parameter:
257
278
  ** label: Field label for method call.
258
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.
259
344
 
260
345
  == Contributing
261
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
@@ -100,31 +103,33 @@ Book.create( **doc )
100
103
  ----
101
104
  pp books = Book.all
102
105
 
103
- p book = Book.filter( title: "title 1" ).first
106
+ p book = Book.where( title: "title 1" ).first
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), style: ["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( style: ["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,14 @@ 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
+ pp Book.where( title: /title/ )[0, 5].all
136
141
  ----
137
142
 
138
143
  === ドキュメント更新
139
144
 
140
145
  [source,ruby]
141
146
  ----
142
- book = Book.filter( title: "title 1" ).first
147
+ book = Book.where( title: "title 1" ).first
143
148
  book.title = "title 1 [update]"
144
149
  book.save
145
150
  ----
@@ -148,7 +153,7 @@ book.save
148
153
 
149
154
  [source,ruby]
150
155
  ----
151
- book = Book.filter( title: "title 1" ).first
156
+ book = Book.where( title: "title 1" ).first
152
157
  book.delete
153
158
  ----
154
159
 
@@ -158,7 +163,7 @@ book.delete
158
163
 
159
164
  [source,ruby]
160
165
  ----
161
- Mongous.connect!( hosts_or_uri = nil, **opts )
166
+ Mongous.connect!( hosts_or_uri = nil, **options )
162
167
  ----
163
168
 
164
169
  * Result:
@@ -166,21 +171,21 @@ Mongous.connect!( hosts_or_uri = nil, **opts )
166
171
 
167
172
  * Parameter:
168
173
  ** hosts_or_uri: ホスト配列または URI (default: ["localhost:21017"])
169
- ** opts: オプション
174
+ ** options: オプション
170
175
  *** file: データベース構成定義ファイルへのパス
171
176
  *** mode: 実行モード (default: "development")
172
177
  *** database: データベース名 (default: "test")
173
- *** * Mongo::Client.new のその他オプション引数
178
+ *** Mongo::Client.new のその他オプション引数
174
179
 
175
180
  === データベースに接続する
176
181
 
177
182
  [source,ruby]
178
183
  ----
179
- Mongous.connect( hosts_or_uri = nil, **opts )
184
+ Mongous.connect( hosts_or_uri = nil, **options )
180
185
  ----
181
186
 
182
187
  * Result:
183
- ** Mongo::Client.
188
+ ** Mongo::Client インスタンス.
184
189
 
185
190
  === ドキュメントの機能を取り入れる.
186
191
 
@@ -193,24 +198,37 @@ include Mongous::Document
193
198
 
194
199
  [source,ruby]
195
200
  ----
196
- set_client( client )
201
+ self.client=( client )
197
202
  ----
198
203
 
199
204
  * Result:
200
- ** nil.
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 文字列.
201
219
 
202
220
  * Parameter:
203
- ** client: Mongo::Client instance.
221
+ ** collection_name: コレクション名.
204
222
 
205
223
  === ドキュメントの要素を定義する.
206
224
 
207
225
  [source,ruby]
208
226
  ----
209
- field( label, *attrs, &block )
227
+ field( symbol, *attrs, **items )
210
228
  ----
211
229
 
212
230
  * Parameter:
213
- ** label: 項目名シンボル
231
+ ** symbol: 項目名
214
232
  ** attrs: 項目属性
215
233
  *** Class: 項目検証用 Class
216
234
  *** Proc: 項目検証用 Proc
@@ -218,17 +236,20 @@ field( label, *attrs, &block )
218
236
  *** Array: 項目検証用配列
219
237
  *** Symbol: 特別な指示子
220
238
  **** must: ナル値でも空文字列でもない
221
- ** block: デフォルト値を返す
239
+ ** items: 保存時の操作.
240
+ *** default: 未定義のときの値または Proc.
241
+ *** create: ドキュメントを新規保存するときの値または Proc.
242
+ *** update: ドキュメントを更新するときの値または Proc.
222
243
 
223
244
  === 保存や代入の前にドキュメントの要素を検証する.
224
245
 
225
246
  [source,ruby]
226
247
  ----
227
- verify( *syms, &block )
248
+ verify( *directives, &block )
228
249
  ----
229
250
 
230
251
  * Parameter:
231
- ** syms: 条件シンボル
252
+ ** directives: 条件シンボル
232
253
  *** strict: 定義済み項目名であることを検証する.
233
254
  ** block: 各項目値を検証して真偽を返す内容を記述する.
234
255
 
@@ -236,12 +257,12 @@ verify( *syms, &block )
236
257
 
237
258
  [source,ruby]
238
259
  ----
239
- index( *syms, **opts )
260
+ index( *symbols, **options )
240
261
  ----
241
262
 
242
263
  * Parameter:
243
- ** syms: 項目名シンボル
244
- ** opts: Mongo::Collection#indexes() のオプション.
264
+ ** symbols: 項目名
265
+ ** options: Mongo::Collection#indexes() のオプション.
245
266
 
246
267
  === 項目値がナル値でも空文字列でもないことを検証する.
247
268
 
@@ -256,6 +277,70 @@ having?( label )
256
277
  * Parameter:
257
278
  ** label: メソッド呼び出しする項目名.
258
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: 項目名またはフィルタインスタンス
259
344
 
260
345
  == 貢献
261
346