mongous 0.1.6 → 0.1.8

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '099aa151de9cf50a6c25b861ad877eafc6e16972f12f463181d735d8fb4ae71f'
4
- data.tar.gz: b41e34b02325dcb2933ec9e195452d5e2a027ee375d56cb0fe44a2144b275928
3
+ metadata.gz: 3ac08d4dc3cd1b50de8ae75c4db5f63b9604abefa5890f046564d44619d75cf4
4
+ data.tar.gz: 446aae2fcd699ad4a27cc5f15790a00d84eaceff3e38ef4ded3b5aec425dcba4
5
5
  SHA512:
6
- metadata.gz: a058325e628afba439934ea18e82a2cbc80e4053c00aff7c7ca3326da6be2b588a37361152c359e4ed558f375ab2c7bc1e77f6fe8968a62bdc432733298fc660
7
- data.tar.gz: 8bc4c0bdef2be7e1c4bad582a5c0dc28f167fb0f99a7a74b800793b84c19db68a5a20564d8f7a6d6d41d210c78c78670bdd63ef12aec20f2375c56bb83237120
6
+ metadata.gz: ce0e4ff3b90f13ac1bdb328ecac4c8986120e780575dbb97a242638d2cfd2c11581963cc2141c2f09f3a184cdcef85420354f4a12d02eca890c66b1458c9ea8e
7
+ data.tar.gz: 6e78c1a620e8cdc974e9e60bca01f54b0127b225a7e1e0f567c7354ec7d43adf1cd2e3ffa72859f6805ffcacd8b3a198258763a4f14ab0f216b6f4d585d56e0d
@@ -128,9 +128,11 @@ Book.or( filter1, filter3 ).each do |book|
128
128
  p book
129
129
  end
130
130
 
131
- Book.find( {}, { projection: {_id: 0} } ).each do |book|
131
+ Book.find( { title: /title/ }, { projection: {_id: 0} } ).each do |book|
132
132
  p book
133
133
  end
134
+
135
+ pp Book.filter( title: /title/ )[0, 5].all
134
136
  ----
135
137
 
136
138
  === Update document
@@ -187,6 +189,19 @@ Mongous.connect( hosts_or_uri = nil, **opts )
187
189
  include Mongous::Document
188
190
  ----
189
191
 
192
+ === Bind another database.
193
+
194
+ [source,ruby]
195
+ ----
196
+ set_client( client )
197
+ ----
198
+
199
+ * Result:
200
+ ** nil.
201
+
202
+ * Parameter:
203
+ ** client: Mongo::Client instance.
204
+
190
205
  === Declare document structure.
191
206
 
192
207
  [source,ruby]
@@ -128,9 +128,11 @@ Book.or( filter1, filter3 ).each do |book|
128
128
  p book
129
129
  end
130
130
 
131
- Book.find( {}, { projection: {_id: 0} } ).each do |book|
131
+ Book.find( { title: /title/ }, { projection: {_id: 0} } ).each do |book|
132
132
  p book
133
133
  end
134
+
135
+ pp Book.filter( title: /title/ )[0, 5].all
134
136
  ----
135
137
 
136
138
  === ドキュメント更新
@@ -187,6 +189,19 @@ Mongous.connect( hosts_or_uri = nil, **opts )
187
189
  include Mongous::Document
188
190
  ----
189
191
 
192
+ === 別のデータベースを割り当てる.
193
+
194
+ [source,ruby]
195
+ ----
196
+ set_client( client )
197
+ ----
198
+
199
+ * Result:
200
+ ** nil.
201
+
202
+ * Parameter:
203
+ ** client: Mongo::Client instance.
204
+
190
205
  === ドキュメントの要素を定義する.
191
206
 
192
207
  [source,ruby]
@@ -10,7 +10,7 @@ module Mongous
10
10
  end
11
11
  end
12
12
 
13
- def set_client( _client )
13
+ def client=( _client )
14
14
  m = /(.*?):(\d+)/.match( caller()[0] )
15
15
  call_from = [ m[1], m[2] ].join(":")
16
16
  if !_client.is_a?( Mongo::Client )
@@ -28,7 +28,7 @@ module Mongous
28
28
  end
29
29
  end
30
30
 
31
- def set_collection_name( _collection_name )
31
+ def collection_name=( _collection_name )
32
32
  self.class_variable_set( :@@collection_name, _collection_name )
33
33
  if self.class_variable_defined?( :@@collection )
34
34
  self.remove_class_variable( :@@collection )
@@ -1,6 +1,10 @@
1
1
 
2
2
  module Mongous
3
3
  module Extention
4
+ def count
5
+ self.collection.find.count
6
+ end
7
+
4
8
  def first
5
9
  doc = self.collection.find.first
6
10
  self.new( **doc ) if doc
@@ -126,51 +130,59 @@ module Mongous
126
130
  end
127
131
 
128
132
  def option( _option )
129
- self.option!( _option )
130
- self.dup
131
- end
132
-
133
- def option!( _option )
134
133
  @option.merge!( _option )
134
+ self.dup
135
135
  end
136
136
 
137
137
  def projection( _projection )
138
- self.projection!( _projection )
138
+ @projection = _projection
139
139
  self.dup
140
140
  end
141
141
  alias :select :projection
142
142
 
143
- def projection!( _projection )
144
- @projection = _projection
145
- end
146
-
147
143
  def sort( _sort )
148
- self.sort!( _sort )
144
+ @sort = _sort
149
145
  self.dup
150
146
  end
151
147
  alias :order :sort
152
148
 
153
- def sort!( _sort )
154
- @sort = _sort
155
- end
156
-
157
149
  def skip( _skip )
158
- self.skip!( _skip )
150
+ @skip = _skip
159
151
  self.dup
160
152
  end
161
153
  alias :offset :skip
162
154
 
163
- def skip!( _skip )
164
- @skip = _skip
165
- end
166
-
167
155
  def limit( _limit )
168
- self.limit!( _limit )
156
+ @limit = _limit
169
157
  self.dup
170
158
  end
171
159
 
172
- def limit!( _limit )
173
- @limit = _limit
160
+ def []( nth_or_range, len = 1 )
161
+ case nth_or_range
162
+ when Integer
163
+ raise Mongous::Error, "invalid nth. : #{ nth_or_range }" if len < 0
164
+ @skip = nth_or_range
165
+
166
+ raise Mongous::Error, "invalid len. : #{ len }" if !len.is_a? Integer || len <= 0
167
+ @limit = len
168
+
169
+ when Range
170
+ from = nth_or_range.begin
171
+ raise Mongous::Error, "invalid range. : #{ nth_or_range }" unless from.is_a? Integer
172
+
173
+ to = nth_or_range.end
174
+ raise Mongous::Error, "invalid range. : #{ nth_or_range }" unless to.is_a? Integer
175
+
176
+ to -= 1 if nth_or_range.exclude_end?
177
+ @skip = from
178
+ @limit = to - from + 1
179
+
180
+ else
181
+ raise Mongous::Error, "invalid class. : #{ nth_or_range }"
182
+
183
+ end
184
+
185
+ self.dup
174
186
  end
175
187
 
176
188
  def do_find
@@ -184,6 +196,19 @@ module Mongous
184
196
  found
185
197
  end
186
198
 
199
+ def count
200
+ _count = do_find.count
201
+ if @skip && @limit
202
+ [_count - @skip, @limit].min
203
+ elsif @skip.nil? && @limit
204
+ [_count, @limit].min
205
+ elsif @skip && @limit.nil?
206
+ [_count - @skip, 0].max
207
+ else
208
+ _count
209
+ end
210
+ end
211
+
187
212
  def first
188
213
  doc = do_find.first
189
214
  @klass.new( **doc ) if doc
@@ -1,3 +1,3 @@
1
1
  module Mongous
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.8"
3
3
  end
@@ -5,7 +5,8 @@ $client = Mongous.connect
5
5
 
6
6
  class Book
7
7
  include Mongous::Document
8
- set_client $client
8
+ self.client = $client
9
+ self.collection_name = "Book"
9
10
  end
10
11
 
11
12
 
@@ -5,7 +5,8 @@ $client = Mongous.connect( ["localhost:27017"], database: "test" )
5
5
 
6
6
  class Book
7
7
  include Mongous::Document
8
- set_client $client
8
+ self.client = $client
9
+ self.collection_name = "Book"
9
10
  end
10
11
 
11
12
 
@@ -5,7 +5,8 @@ $client = Mongous.connect( ENV["MONGOLAB_URI"] || "mongodb://localhost:27017/t
5
5
 
6
6
  class Book
7
7
  include Mongous::Document
8
- set_client $client
8
+ self.client = $client
9
+ self.collection_name = "Book"
9
10
  end
10
11
 
11
12
 
@@ -6,7 +6,8 @@ $client = Mongous.connect( file: filepath, mode: ENV["RACK_ENV"] || "developme
6
6
 
7
7
  class Book
8
8
  include Mongous::Document
9
- set_client $client
9
+ self.client = $client
10
+ self.collection_name = "Book"
10
11
  end
11
12
 
12
13
 
@@ -11,6 +11,9 @@ end
11
11
  p book = Book.first
12
12
  puts
13
13
 
14
+ p count = Book.count
15
+ puts
16
+
14
17
  pp books = Book.all
15
18
  puts
16
19
 
@@ -11,6 +11,9 @@ end
11
11
  p book = Book.filter( title: /title/ ).first
12
12
  puts
13
13
 
14
+ p count = Book.filter( title: /title/ ).count
15
+ puts
16
+
14
17
  pp books = Book.filter( title: /title/ ).all
15
18
  puts
16
19
 
@@ -0,0 +1,38 @@
1
+
2
+ require "mongous"
3
+
4
+ Mongous.connect!
5
+
6
+ class Book
7
+ include Mongous::Document
8
+ end
9
+
10
+
11
+ p count = Book.filter.count
12
+ puts
13
+
14
+ p count = Book.filter[0..4].count
15
+ puts
16
+
17
+ p count = Book.filter[0...4].count
18
+ puts
19
+
20
+ p count = Book.filter[0, 4].count
21
+ puts
22
+
23
+ p count = Book.filter[20,10].count
24
+ puts
25
+
26
+ pp books = Book.filter[0, 2].all
27
+ puts
28
+
29
+ filter = Book.filter( title: /title/ )
30
+ filter[0, 4].each do |book|
31
+ p book
32
+ end
33
+ puts
34
+ filter[4, 4].each do |book|
35
+ p book
36
+ end
37
+ puts
38
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongous
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - arimay
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-04 00:00:00.000000000 Z
11
+ date: 2020-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongo
@@ -98,6 +98,7 @@ files:
98
98
  - sample/query_basic_4.rb
99
99
  - sample/query_basic_5.rb
100
100
  - sample/query_basic_6.rb
101
+ - sample/query_basic_7.rb
101
102
  - sample/query_detail_1.rb
102
103
  - sample/query_detail_2.rb
103
104
  - sample/query_detail_3.rb