mongous 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/README.adoc +6 -3
  3. data/README.ja.adoc +6 -3
  4. data/lib/mongous/base.rb +12 -2
  5. data/lib/mongous/extention.rb +4 -0
  6. data/lib/mongous/filter.rb +57 -14
  7. data/lib/mongous/version.rb +1 -1
  8. data/sample/connect_auto_0.rb +1 -1
  9. data/sample/declare_compact_1.rb +0 -2
  10. data/sample/declare_compact_2.rb +38 -0
  11. data/sample/declare_label_1.rb +0 -2
  12. data/sample/declare_ndex_1.rb +0 -2
  13. data/sample/declare_ndex_2.rb +0 -2
  14. data/sample/declare_strict_1.rb +0 -2
  15. data/sample/declare_strict_2.rb +0 -2
  16. data/sample/multi_collection_1.rb +0 -2
  17. data/sample/multi_collection_2.rb +0 -2
  18. data/sample/multi_collection_3.rb +6 -8
  19. data/sample/query_basic_1.rb +8 -2
  20. data/sample/query_basic_2.rb +8 -2
  21. data/sample/query_basic_3.rb +0 -2
  22. data/sample/query_basic_4.rb +0 -2
  23. data/sample/query_basic_5.rb +0 -2
  24. data/sample/query_basic_6.rb +56 -0
  25. data/sample/query_detail_1.rb +0 -2
  26. data/sample/query_detail_2.rb +0 -2
  27. data/sample/query_detail_3.rb +0 -2
  28. data/sample/query_filter_1.rb +0 -2
  29. data/sample/query_filter_2.rb +0 -2
  30. data/sample/query_find_1.rb +6 -5
  31. data/sample/query_select_1.rb +8 -5
  32. data/sample/query_skip_limit_1.rb +17 -16
  33. data/sample/query_skip_limit_2.rb +19 -10
  34. data/sample/query_sort_1.rb +43 -0
  35. data/sample/save_basic_1.rb +1 -5
  36. data/sample/save_basic_2.rb +1 -5
  37. data/sample/save_basic_3.rb +1 -5
  38. data/sample/save_detail_1.rb +0 -2
  39. data/sample/save_detail_2.rb +0 -2
  40. data/sample/save_detail_3.rb +0 -2
  41. data/sample/save_verify_1.rb +0 -2
  42. data/sample/save_verify_2.rb +0 -2
  43. data/sample/save_verify_3.rb +0 -2
  44. data/sample/save_verify_4.rb +0 -2
  45. data/sample/save_verify_5.rb +0 -2
  46. data/sample/save_verify_6.rb +0 -2
  47. data/sample/save_verify_7.rb +0 -2
  48. data/sample/update_basic_1.rb +1 -5
  49. data/sample/zap_basic_1.rb +1 -5
  50. data/sample/zap_basic_2.rb +2 -6
  51. data/sample/zap_basic_3.rb +1 -5
  52. data/sample/zbenchmark_search_1.rb +19 -15
  53. data/sample/zbenchmark_search_2.rb +19 -15
  54. data/sample/zbenchmark_search_3.rb +25 -27
  55. metadata +5 -3
  56. data/sample/query_sort_order_1.rb +0 -46
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bf1d2cbfd528ed1a623833025cf965b5200a1f973ce1200609ae459e6167694f
4
- data.tar.gz: 785aea052055755ec01bab88f47da0915371546d9c84f14a7876f968c79a5e3d
3
+ metadata.gz: 8b0a73aa23287619bdae717f683f04e266d582dca4a6d055b798cf32243c6566
4
+ data.tar.gz: 14e22dcbf25b3a0fa200023c9f770c549607060ddb03057b32c8c1ccee3b58c2
5
5
  SHA512:
6
- metadata.gz: 706bcfdac572ee3864a70d47101a689142134f030f20c37123e33f64575bd046eea74a656bdce29487f93c441a4374d77b97e96989fd234940daaf81171ace19
7
- data.tar.gz: e4c1f4dae468a78f376b2a89a612a7f419e39939b0b486451980857e24127e0420e2fb2dc13f8afe8a7e1cb6686a5c2c82ce3e1b6a7dc94609327f258a785d12
6
+ metadata.gz: b37766d78679cc2a6bee8bfe256da0a363344b6e349552f0491d5a0892fa1dfeb7cca90d4b2e326ac7687cdd2c3a0561fdfe9fca4ce71382029077d8c289e6de
7
+ data.tar.gz: b5e9fee3f8f9d4f34d5203b9b260559d8ee1ca86e659d85629ef9c1b700bcfcb450b7ffc0447b7f3fa8c4245634f2e968619f9ba451c9591016e13c07d23060f
@@ -38,7 +38,7 @@ Or install it yourself as:
38
38
  ----
39
39
  require "mongous"
40
40
 
41
- Mongous.attach! :Book
41
+ Mongous.document! :Book
42
42
  ----
43
43
 
44
44
  === Detail declaration
@@ -102,7 +102,8 @@ Book.create( **doc )
102
102
  ----
103
103
  pp books = Book.all
104
104
 
105
- p book = Book.where( title: "title 1" ).first
105
+ p book = Book.where( title: /title/ ).first
106
+ p book = Book.where( title: /title/ ).last
106
107
 
107
108
  books = Book.where( title: /title/ ).all
108
109
  books.each do |book|
@@ -198,7 +199,7 @@ Mongous.connect( hosts_or_uri = nil, **options )
198
199
 
199
200
  [source,ruby]
200
201
  ----
201
- Mongous.attach!( *names )
202
+ Mongous.document!( *names, **options )
202
203
  ----
203
204
 
204
205
  * Result:
@@ -206,6 +207,8 @@ Mongous.attach!( *names )
206
207
 
207
208
  * Parameter:
208
209
  ** names: Collection names. (String or Symbol)
210
+ ** options: Options.
211
+ *** timestamp: If true then add fields :created_at, :updated_at.
209
212
 
210
213
  === Include document functions into collection operate class.
211
214
 
@@ -38,7 +38,7 @@ gem 'mongous'
38
38
  ----
39
39
  require "mongous"
40
40
 
41
- Mongous.attach! :Book
41
+ Mongous.document! :Book
42
42
  ----
43
43
 
44
44
  === 詳細な定義
@@ -102,7 +102,8 @@ Book.create( **doc )
102
102
  ----
103
103
  pp books = Book.all
104
104
 
105
- p book = Book.where( title: "title 1" ).first
105
+ p book = Book.where( title: /title/ ).first
106
+ p book = Book.where( title: /title/ ).last
106
107
 
107
108
  books = Book.where( title: /title/ ).all
108
109
  books.each do |book|
@@ -198,7 +199,7 @@ Mongous.connect( hosts_or_uri = nil, **options )
198
199
 
199
200
  [source,ruby]
200
201
  ----
201
- Mongous.attach!( *names )
202
+ Mongous.document!( *names, **options )
202
203
  ----
203
204
 
204
205
  * Result:
@@ -206,6 +207,8 @@ Mongous.attach!( *names )
206
207
 
207
208
  * Parameter:
208
209
  ** names: コレクション名. (String または Symbol)
210
+ ** options: オプション
211
+ *** timestamp: 真のとき, フィールド :created_at, :updated_at を追加する.
209
212
 
210
213
  === コレクション操作クラスにドキュメントの機能を取り入れる.
211
214
 
@@ -50,7 +50,7 @@ module Mongous
50
50
  self.class_variable_set( :@@client, _client )
51
51
  end
52
52
 
53
- def attach!( *names )
53
+ def document!( *names, **opts )
54
54
  raise Mongous::Error, "missing argument." if names.empty?
55
55
 
56
56
  names.each do |name|
@@ -72,13 +72,23 @@ module Mongous
72
72
  end
73
73
  end
74
74
 
75
- Object.class_eval <<-CLASS
75
+ klass = Object.class_eval <<-CLASS
76
76
  class #{name}
77
77
  include Mongous::Document
78
78
  end
79
79
  CLASS
80
+
81
+ if opts[:timestamp]
82
+ klass.class_eval do
83
+ field :created_at, Time, create: proc{ Time.now }
84
+ field :updated_at, Time, update: proc{ Time.now }
85
+ index :created_at
86
+ index :updated_at
87
+ end
88
+ end
80
89
  end
81
90
  end
91
+ alias :attach! :document!
82
92
 
83
93
  def client
84
94
  if not self.class_variable_defined?( :@@client )
@@ -101,6 +101,10 @@ module Mongous
101
101
  self.new( **doc ).save
102
102
  end
103
103
 
104
+ def drop
105
+ self.collection.drop
106
+ end
107
+
104
108
  def find( conditios = {}, options = {} )
105
109
  self.collection.find( conditios, options )
106
110
  end
@@ -5,11 +5,6 @@ module Mongous
5
5
  self.collection.estimated_document_count
6
6
  end
7
7
 
8
- def first
9
- doc = self.collection.find.first
10
- self.new( **doc ) if doc
11
- end
12
-
13
8
  def all
14
9
  self.collection.find.map do |doc|
15
10
  self.new( **doc )
@@ -32,6 +27,22 @@ module Mongous
32
27
  Filter.new( self ).attach( collection_name )
33
28
  end
34
29
 
30
+ def []( nth_or_range, len = nil )
31
+ Filter.new( self )[ nth_or_range, len ]
32
+ end
33
+
34
+ def first
35
+ Filter.new( self ).first
36
+ end
37
+
38
+ def last
39
+ Filter.new( self ).last
40
+ end
41
+
42
+ def sort( *keys, **hash )
43
+ Filter.new( self ).sort( *keys, **hash )
44
+ end
45
+
35
46
  def select( *keys, **hash )
36
47
  Filter.new( self ).select( *keys, **hash )
37
48
  end
@@ -45,7 +56,7 @@ module Mongous
45
56
  raise Mongous::Error, "Unset args for #{self}.not." if filter.nil? && conditions.empty?
46
57
 
47
58
  condition = normalize( filter, conditions )
48
- Filter.new( self ).where({"$nor" => [condition]})
59
+ Filter.new( self ).not( condition )
49
60
  end
50
61
 
51
62
  def and( *filters )
@@ -101,7 +112,7 @@ module Mongous
101
112
  w
102
113
  end
103
114
 
104
- def where( conditions )
115
+ def build_condition( conditions )
105
116
  hash = {}
106
117
  conditions.each do |key, item|
107
118
  case key
@@ -137,11 +148,23 @@ module Mongous
137
148
  end
138
149
  end
139
150
  end
151
+ hash
152
+ end
153
+
154
+ def where( conditions )
155
+ hash = build_condition( conditions )
140
156
  w = self.dup
141
157
  w.instance_variable_set( :@filter, @filter.merge( hash ) )
142
158
  w
143
159
  end
144
160
 
161
+ def not( conditions )
162
+ hash = build_condition( conditions )
163
+ w = self.dup
164
+ w.instance_variable_set( :@filter, @filter.merge( {"$nor" => [hash]} ) )
165
+ w
166
+ end
167
+
145
168
  def to_condition
146
169
  @filter.dup
147
170
  end
@@ -152,7 +175,7 @@ module Mongous
152
175
  w
153
176
  end
154
177
 
155
- def projection( *keys, **hash )
178
+ def select( *keys, **hash )
156
179
  if not keys.empty?
157
180
  _projection = Hash[ keys.zip( Array.new(keys.length, 1) ) ]
158
181
  elsif not hash.empty?
@@ -164,7 +187,6 @@ module Mongous
164
187
  w.instance_variable_set( :@projection, _projection )
165
188
  w
166
189
  end
167
- alias :select :projection
168
190
 
169
191
  def sort( *keys, **hash )
170
192
  if not keys.empty?
@@ -178,17 +200,20 @@ module Mongous
178
200
  w.instance_variable_set( :@sort, _sort )
179
201
  w
180
202
  end
181
- alias :order :sort
182
203
 
183
204
  def []( nth_or_range, len = nil )
184
205
  case nth_or_range
185
206
  when Integer
186
207
  _skip = nth_or_range
187
- _limit = nil
188
208
 
189
- if len
190
- raise Mongous::Error, "invalid len. : #{ len }" if !len.is_a? Integer || len <= 0
209
+ if len.is_a?(NilClass)
210
+ _limit = 1
211
+ elsif len.is_a?(Integer) && len == 0
212
+ _limit = nil
213
+ elsif len.is_a?(Integer) && len > 0
191
214
  _limit = len
215
+ else
216
+ raise Mongous::Error, "invalid len. : #{ len }"
192
217
  end
193
218
 
194
219
  when Range
@@ -247,7 +272,25 @@ module Mongous
247
272
  end
248
273
 
249
274
  def first
250
- doc = exec_query.first
275
+ _filter = @filter
276
+ _option = @option.dup
277
+ _option[:projection] = @projection if @projection
278
+ found = @klass.collection( @collection_name ).find( _filter, _option )
279
+ _order = @sort || { _id: 1 }
280
+ doc = found.sort( _order ).first
281
+ @klass.new( **doc ) if doc
282
+ end
283
+
284
+ def last
285
+ _filter = @filter
286
+ _option = @option.dup
287
+ _option[:projection] = @projection if @projection
288
+ found = @klass.collection( @collection_name ).find( _filter, _option )
289
+ _order = {}
290
+ ( @sort || {_id: 1} ).each do |k,v|
291
+ _order[k] = - v
292
+ end
293
+ doc = found.sort( _order ).first
251
294
  @klass.new( **doc ) if doc
252
295
  end
253
296
 
@@ -1,3 +1,3 @@
1
1
  module Mongous
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
@@ -1,7 +1,7 @@
1
1
 
2
2
  require "mongous"
3
3
 
4
- Mongous.attach! :Book
4
+ Mongous.document! :Book
5
5
 
6
6
  Book.each do |book|
7
7
  p book
@@ -1,8 +1,6 @@
1
1
 
2
2
  require "mongous"
3
3
 
4
- Mongous.connect!
5
-
6
4
  class Book
7
5
  include Mongous::Document
8
6
  end
@@ -0,0 +1,38 @@
1
+
2
+ require "mongous"
3
+
4
+ Mongous.document! :Book1
5
+
6
+ book = Book1.new
7
+ book.title = "book1"
8
+ book.save
9
+
10
+ book = Book1.where( title: "book1" ).first
11
+ book.author = "alice"
12
+ book.save
13
+
14
+ Book1.each do |book|
15
+ pp book
16
+ end
17
+ puts
18
+
19
+ Book1.drop
20
+
21
+
22
+ Mongous.document! :Book2, timestamp: true
23
+
24
+ book = Book2.new
25
+ book.title = "book2"
26
+ book.save
27
+
28
+ book = Book2.where( title: "book2" ).first
29
+ book.author = "bob"
30
+ book.save
31
+
32
+ Book2.each do |book|
33
+ pp book
34
+ end
35
+ puts
36
+
37
+ Book2.drop
38
+
@@ -1,8 +1,6 @@
1
1
 
2
2
  require "mongous"
3
3
 
4
- Mongous.connect!
5
-
6
4
  class Book
7
5
  include Mongous::Document
8
6
 
@@ -1,8 +1,6 @@
1
1
 
2
2
  require "mongous"
3
3
 
4
- Mongous.connect!
5
-
6
4
  class Book
7
5
  include Mongous::Document
8
6
 
@@ -1,8 +1,6 @@
1
1
 
2
2
  require "mongous"
3
3
 
4
- Mongous.connect!
5
-
6
4
  class Book
7
5
  include Mongous::Document
8
6
 
@@ -1,8 +1,6 @@
1
1
 
2
2
  require "mongous"
3
3
 
4
- Mongous.connect!
5
-
6
4
  class Book
7
5
  include Mongous::Document
8
6
 
@@ -1,8 +1,6 @@
1
1
 
2
2
  require "mongous"
3
3
 
4
- Mongous.connect!
5
-
6
4
  class Book
7
5
  include Mongous::Document
8
6
 
@@ -1,8 +1,6 @@
1
1
 
2
2
  require "mongous"
3
3
 
4
- Mongous.connect!
5
-
6
4
  class Book1
7
5
  include Mongous::Document
8
6
  end
@@ -1,8 +1,6 @@
1
1
 
2
2
  require "mongous"
3
3
 
4
- Mongous.connect!
5
-
6
4
  class Book1
7
5
  include Mongous::Document
8
6
  end
@@ -1,24 +1,22 @@
1
1
 
2
2
  require "mongous"
3
3
 
4
- Mongous.connect!
5
-
6
- Mongous.attach! :Book1
7
- Mongous.attach! :Book2
8
- Mongous.attach! :Book3
4
+ Mongous.document! :Book1
5
+ Mongous.document! :Book2
6
+ Mongous.document! :Book3
9
7
 
10
8
  pp Book1.collection
11
9
  pp Book2.collection
12
10
  pp Book3.collection
13
11
 
14
- Mongous.attach! :Book1
15
- Mongous.attach! :Book2, :Book3
12
+ Mongous.document! :Book1
13
+ Mongous.document! :Book2, :Book3
16
14
 
17
15
  pp Book1.collection
18
16
  pp Book2.collection
19
17
  pp Book3.collection
20
18
 
21
- Mongous.attach! :Book1, :Book2, :Book3
19
+ Mongous.document! :Book1, :Book2, :Book3
22
20
 
23
21
  pp Book1.collection
24
22
  pp Book2.collection
@@ -1,21 +1,27 @@
1
1
 
2
2
  require "mongous"
3
3
 
4
- Mongous.connect!
5
-
6
4
  class Book
7
5
  include Mongous::Document
8
6
  end
9
7
 
8
+ p "book = Book.first"
10
9
  p book = Book.first
11
10
  puts
12
11
 
12
+ p "book = Book.last"
13
+ p book = Book.last
14
+ puts
15
+
16
+ p "count = Book.count"
13
17
  p count = Book.count
14
18
  puts
15
19
 
20
+ p "books = Book.all"
16
21
  pp books = Book.all
17
22
  puts
18
23
 
24
+ p "Book.each do |book|"
19
25
  Book.each do |book|
20
26
  p book
21
27
  end