mongous 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +23 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +6 -0
  5. data/Gemfile +5 -0
  6. data/README.adoc +239 -0
  7. data/README.ja.adoc +238 -0
  8. data/Rakefile +96 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/lib/mongous.rb +11 -0
  12. data/lib/mongous/base.rb +68 -0
  13. data/lib/mongous/document.rb +159 -0
  14. data/lib/mongous/extention.rb +167 -0
  15. data/lib/mongous/filter.rb +124 -0
  16. data/lib/mongous/version.rb +3 -0
  17. data/mongous.gemspec +26 -0
  18. data/sample/connect_auto_1.rb +14 -0
  19. data/sample/connect_auto_2.rb +15 -0
  20. data/sample/connect_default_1.rb +14 -0
  21. data/sample/connect_default_2.rb +15 -0
  22. data/sample/connect_environ_1.rb +14 -0
  23. data/sample/connect_environ_2.rb +15 -0
  24. data/sample/connect_loadfile_1.rb +15 -0
  25. data/sample/connect_loadfile_2.rb +16 -0
  26. data/sample/connect_mongo.yml +15 -0
  27. data/sample/declare_compact_1.rb +27 -0
  28. data/sample/declare_label_1.rb +39 -0
  29. data/sample/declare_ndex_1.rb +41 -0
  30. data/sample/declare_ndex_2.rb +41 -0
  31. data/sample/declare_strict_1.rb +47 -0
  32. data/sample/declare_strict_2.rb +35 -0
  33. data/sample/multi_docs_1.rb +27 -0
  34. data/sample/multi_docs_2.rb +58 -0
  35. data/sample/query_basic_1.rb +18 -0
  36. data/sample/query_basic_2.rb +18 -0
  37. data/sample/query_basic_3.rb +19 -0
  38. data/sample/query_basic_4.rb +25 -0
  39. data/sample/query_basic_5.rb +39 -0
  40. data/sample/query_detail_1.rb +24 -0
  41. data/sample/query_detail_2.rb +23 -0
  42. data/sample/query_detail_3.rb +29 -0
  43. data/sample/query_limit_1.rb +44 -0
  44. data/sample/query_order_1.rb +49 -0
  45. data/sample/query_projection_1.rb +44 -0
  46. data/sample/query_raw_1.rb +33 -0
  47. data/sample/save_basic_1.rb +13 -0
  48. data/sample/save_basic_2.rb +18 -0
  49. data/sample/save_basic_3.rb +13 -0
  50. data/sample/save_detail_1.rb +34 -0
  51. data/sample/save_detail_2.rb +34 -0
  52. data/sample/save_detail_3.rb +40 -0
  53. data/sample/save_verify_1.rb +46 -0
  54. data/sample/save_verify_2.rb +28 -0
  55. data/sample/save_verify_3.rb +26 -0
  56. data/sample/save_verify_4.rb +29 -0
  57. data/sample/save_verify_5.rb +23 -0
  58. data/sample/save_verify_6.rb +23 -0
  59. data/sample/template_article.rb +5 -0
  60. data/sample/template_person.rb +12 -0
  61. data/sample/zap_basic_1.rb +11 -0
  62. data/sample/zap_basic_2.rb +11 -0
  63. data/sample/zap_basic_3.rb +11 -0
  64. metadata +147 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 95182d94474fd128deef15317dc1131c44d3eee27e920f1cfca3030cc387a3dc
4
+ data.tar.gz: 2f11edcd8442be189916eadd722e131c2b63a70030285f42bc7347937351ab66
5
+ SHA512:
6
+ metadata.gz: 337bd873fe2ac4323cd1fd3606a32058f669ffe9aaf8ac6f0778b37959d771d47a79b0a54739f2864d525c6283868f96676a1980631e56bd8e962b4f967d7298
7
+ data.tar.gz: 4881ebeeb6e17ccbcfab55ba050485974225a76790f9ccc19a06192c3c179669d5b6aadb3f955f9e2cd673bd2fee8b8562c1713b58418ac898f3c5d1cdfaf972
@@ -0,0 +1,23 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ /vendor/
16
+ *.pid
17
+ *.log
18
+ *.log.*
19
+ /var/
20
+ /log/
21
+ *.swp
22
+ /.rspec_status
23
+
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mongoidtest.gemspec
4
+ gemspec
5
+
@@ -0,0 +1,239 @@
1
+ = Mongous
2
+
3
+ Yet another mongo wrapper library.
4
+
5
+ == Features
6
+
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.
11
+
12
+ == Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ [source,ruby]
17
+ ----
18
+ gem 'mongous'
19
+ ----
20
+
21
+ And then execute:
22
+
23
+ $ bundle install
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install mongous
28
+ or
29
+ $ gem install -l mongous-x.x.x.gem
30
+
31
+ == Usage
32
+
33
+ === Simple description
34
+
35
+ [source,ruby]
36
+ ----
37
+ require "mongous"
38
+
39
+ Mongous.connect!
40
+
41
+ class Book
42
+ include Mongous::Document
43
+ end
44
+ ----
45
+
46
+ === Detail declaration
47
+
48
+ [source,ruby]
49
+ ----
50
+ require "mongous"
51
+
52
+ Mongous.connect! ["localhost:27017"], database: "test"
53
+
54
+ class Book
55
+ include Mongous::Document
56
+
57
+ field :title, String, :must
58
+ field :author, String
59
+ 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" }
66
+
67
+ verify :strict
68
+ verify do
69
+ having?(author) | having?(publisher)
70
+ end
71
+
72
+ def isbn?
73
+ isbn.gsub(/[\D]*/, '').size == 13
74
+ end
75
+
76
+ index :title
77
+ end
78
+ ----
79
+
80
+ === Create document
81
+
82
+ [source,ruby]
83
+ ----
84
+ book = Book.new
85
+ book.title = "title 1"
86
+ book.price = 1000
87
+ book.style = "A4"
88
+ book.save
89
+
90
+ book = Book.new( title: "title 2", price: 2000, style: "A5" )
91
+ book.save
92
+
93
+ doc = { title: "title 3", price: 3000, style: "A6" }
94
+ Book.create( doc )
95
+ ----
96
+
97
+ === Search document
98
+
99
+ [source,ruby]
100
+ ----
101
+ books = Book.all
102
+ pp books
103
+
104
+ book = Book.filter( title: "title 1" ).first
105
+ p book
106
+
107
+ books = Book.filter( title: /title/ ).all
108
+ books.each do |book|
109
+ p book
110
+ end
111
+
112
+ Book.filter( title: /title/ ).projection( _id: 0 ).each do |book|
113
+ p book
114
+ end
115
+
116
+ Book.filter( price: (1..2000), style: ["A4","A5"] ).each do |book|
117
+ p book
118
+ end
119
+ ----
120
+
121
+ === Update document
122
+
123
+ [source,ruby]
124
+ ----
125
+ book = Book.filter( title: "title 1" ).first
126
+ book.title = "title 1 [update]"
127
+ book.save
128
+ ----
129
+
130
+ === Delete document
131
+
132
+ [source,ruby]
133
+ ----
134
+ book = Book.filter( title: "title 1" ).first
135
+ book.delete
136
+ ----
137
+
138
+ == Reference
139
+
140
+ === Connect default database.
141
+
142
+ [source,ruby]
143
+ ----
144
+ Mongous.connect!( hosts_or_uri = nil, **opts )
145
+ ----
146
+
147
+ * Result:
148
+ ** nil.
149
+
150
+ * Parameter:
151
+ ** hosts_or_uri: Array of hosts, or URI (default: ["localhost:21017"])
152
+ ** opts: Options.
153
+ *** file: Path to database configuration file.
154
+ *** mode: Execution mode. (default: "development")
155
+ *** database: Database name. (default: "test")
156
+ *** * Other optional arguments for Mongo::Client.new.
157
+
158
+ ** label: Field label symbol.
159
+ === Connect database.
160
+
161
+ [source,ruby]
162
+ ----
163
+ Mongous.connect( hosts_or_uri = nil, **opts )
164
+ ----
165
+
166
+ * Result:
167
+ ** Mongous::Client.
168
+
169
+ === Include document functions.
170
+
171
+ [source,ruby]
172
+ ----
173
+ include Mongous::Document
174
+ ----
175
+
176
+ === Declare document structure.
177
+
178
+ [source,ruby]
179
+ ----
180
+ field( label, *attrs, &block )
181
+ ----
182
+
183
+ * Parameter:
184
+ ** label: Field label symbol.
185
+ ** attrs: Field attributes.
186
+ *** Class: Class for field verification.
187
+ *** Proc: Proc for field verification.
188
+ *** Range: Range for field verification.
189
+ *** Array: Array for field verification.
190
+ *** Symbol: Special delectives.
191
+ **** must: Not nil nor empty.
192
+ ** block: Returning default value.
193
+
194
+ === Verify before save or assignment action.
195
+
196
+ [source,ruby]
197
+ ----
198
+ verify( *syms, &block )
199
+ ----
200
+
201
+ * Parameter:
202
+ ** syms: Conditional symbols.
203
+ *** strict: Verify that it is a defined item name.
204
+ ** block: Describe the content that verifies each item value and returns the truth.
205
+
206
+ === Make index.
207
+
208
+ [source,ruby]
209
+ ----
210
+ index( *syms, **opts )
211
+ ----
212
+
213
+ * Parameter:
214
+ ** syms: Field label symbols.
215
+ ** opts: Options for Mongo::Collection#indexes().
216
+
217
+ === Verify field value is not nil nor empty.
218
+
219
+ [source,ruby]
220
+ ----
221
+ having?( label )
222
+ ----
223
+
224
+ * Result:
225
+ ** Boolean
226
+
227
+ * Parameter:
228
+ ** label: Field label for method call.
229
+
230
+
231
+ == Contributing
232
+
233
+ Bug reports and pull requests are welcome on GitHub at https://github.com/arimay/mongous.
234
+
235
+ == License
236
+
237
+ The gem is available as open source under the terms of the http://opensource.org/licenses/MIT[MIT License].
238
+
239
+ Copyright (c) ARIMA Yasuhiro <arima.yasuhiro@gmail.com>
@@ -0,0 +1,238 @@
1
+ = Mongous
2
+
3
+ またまた新たな mongo ラッパーライブラリ.
4
+
5
+ == 特徴
6
+
7
+ * 組込みと標準添付を除いて、mongo driver と bson のみに依存する軽いライブラリである。
8
+ * 項目デフォルト値をブロックで記述できる.
9
+ * 項目制約条件を Array, Range, Proc で記述できる.
10
+ * 保存時および値設定時に制約条件を満たすか検査する.
11
+
12
+ == 導入
13
+
14
+ アプリの Gemfile にこの行を追加
15
+
16
+ [source,ruby]
17
+ ----
18
+ gem 'mongous'
19
+ ----
20
+
21
+ それから実行
22
+
23
+ $ bundle install
24
+
25
+ または次のように手動で導入
26
+
27
+ $ gem install mongous
28
+ or
29
+ $ gem install -l mongous-x.x.x.gem
30
+
31
+ == 使い方
32
+
33
+ === 簡素な定義
34
+
35
+ [source,ruby]
36
+ ----
37
+ require "mongous"
38
+
39
+ Mongous.connect!
40
+
41
+ class Book
42
+ include Mongous::Document
43
+ end
44
+ ----
45
+
46
+ === 詳細な定義
47
+
48
+ [source,ruby]
49
+ ----
50
+ require "mongous"
51
+
52
+ Mongous.connect! ["localhost:27017"], database: "test"
53
+
54
+ class Book
55
+ include Mongous::Document
56
+
57
+ field :title, String, :must
58
+ field :author, String
59
+ 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" }
66
+
67
+ verify :strict
68
+ verify do
69
+ having?(author) | having?(publisher)
70
+ end
71
+
72
+ def isbn?
73
+ isbn.gsub(/[\D]*/, '').size == 13
74
+ end
75
+
76
+ index :title
77
+ end
78
+ ----
79
+
80
+ === ドキュメント作成
81
+
82
+ [source,ruby]
83
+ ----
84
+ book = Book.new
85
+ book.title = "title 1"
86
+ book.price = 1000
87
+ book.style = "A4"
88
+ book.save
89
+
90
+ book = Book.new( title: "title 2", price: 2000, style: "A5" )
91
+ book.save
92
+
93
+ doc = { title: "title 3", price: 3000, style: "A6" }
94
+ Book.create( doc )
95
+ ----
96
+
97
+ === ドキュメント検索
98
+
99
+ [source,ruby]
100
+ ----
101
+ books = Book.all
102
+ pp books
103
+
104
+ book = Book.filter( title: "title 1" ).first
105
+ p book
106
+
107
+ books = Book.filter( title: /title/ ).all
108
+ books.each do |book|
109
+ p book
110
+ end
111
+
112
+ Book.filter( title: /title/ ).projection( _id: 0 ).each do |book|
113
+ p book
114
+ end
115
+
116
+ Book.filter( price: (1..2000), style: ["A4","A5"] ).each do |book|
117
+ p book
118
+ end
119
+ ----
120
+
121
+ === ドキュメント更新
122
+
123
+ [source,ruby]
124
+ ----
125
+ book = Book.filter( title: "title 1" ).first
126
+ book.title = "title 1 [update]"
127
+ book.save
128
+ ----
129
+
130
+ === ドキュメント削除
131
+
132
+ [source,ruby]
133
+ ----
134
+ book = Book.filter( title: "title 1" ).first
135
+ book.delete
136
+ ----
137
+
138
+ == リファレンス
139
+
140
+ === デフォルトデータベースに接続する
141
+
142
+ [source,ruby]
143
+ ----
144
+ Mongous.connect!( hosts_or_uri = nil, **opts )
145
+ ----
146
+
147
+ * Result:
148
+ ** nil.
149
+
150
+ * Parameter:
151
+ ** hosts_or_uri: ホスト配列または URI (default: ["localhost:21017"])
152
+ ** opts: オプション
153
+ *** file: データベース構成定義ファイルへのパス
154
+ *** mode: 実行モード (default: "development")
155
+ *** database: データベース名 (default: "test")
156
+ *** * Mongo::Client.new のその他オプション引数
157
+
158
+ === データベースに接続する
159
+
160
+ [source,ruby]
161
+ ----
162
+ Mongous.connect( hosts_or_uri = nil, **opts )
163
+ ----
164
+
165
+ * Result:
166
+ ** Mongous::Client.
167
+
168
+ === ドキュメントの機能を取り入れる.
169
+
170
+ [source,ruby]
171
+ ----
172
+ include Mongous::Document
173
+ ----
174
+
175
+ === ドキュメントの要素を定義する.
176
+
177
+ [source,ruby]
178
+ ----
179
+ field( label, *attrs, &block )
180
+ ----
181
+
182
+ * Parameter:
183
+ ** label: 項目名シンボル
184
+ ** attrs: 項目属性
185
+ *** Class: 項目検証用 Class
186
+ *** Proc: 項目検証用 Proc
187
+ *** Range: 項目検証用範囲
188
+ *** Array: 項目検証用配列
189
+ *** Symbol: 特別な指示子
190
+ **** must: ナル値でも空文字列でもない
191
+ ** block: デフォルト値を返す
192
+
193
+ === 保存や代入の前にドキュメントの要素を検証する.
194
+
195
+ [source,ruby]
196
+ ----
197
+ verify( *syms, &block )
198
+ ----
199
+
200
+ * Parameter:
201
+ ** syms: 条件シンボル
202
+ *** strict: 定義済み項目名であることを検証する.
203
+ ** block: 各項目値を検証して真偽を返す内容を記述する.
204
+
205
+ === 索引指定する.
206
+
207
+ [source,ruby]
208
+ ----
209
+ index( *syms, **opts )
210
+ ----
211
+
212
+ * Parameter:
213
+ ** syms: 項目名シンボル
214
+ ** opts: Mongo::Collection#indexes() のオプション.
215
+
216
+ === 項目値がナル値でも空文字列でもないことを検証する.
217
+
218
+ [source,ruby]
219
+ ----
220
+ having?( label )
221
+ ----
222
+
223
+ * Result:
224
+ ** 論理値
225
+
226
+ * Parameter:
227
+ ** label: メソッド呼び出しする項目名.
228
+
229
+
230
+ == 貢献
231
+
232
+ 不具合報告とプルリクエストは GitHub https://github.com/arimay/mongous まで.
233
+
234
+ == ライセンス
235
+
236
+ この Gem は、 http://opensource.org/licenses/MIT[MITライセンス] の条件に基づいてオープンソースとして入手できる.
237
+
238
+ Copyright (c) ARIMA Yasuhiro <arima.yasuhiro@gmail.com>