mongous 0.3.0 → 0.4.0

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.
@@ -81,11 +81,14 @@ module Mongous
81
81
  end
82
82
 
83
83
  def client
84
- self.class_variable_get( :@@client ) rescue nil
84
+ if not self.class_variable_defined?( :@@client )
85
+ connect!
86
+ end
87
+ self.class_variable_get( :@@client )
85
88
  end
86
89
 
87
90
  def client=( _client )
88
- if !_client.is_a?( ::Mongo::Client )
91
+ if not _client.is_a?( ::Mongo::Client )
89
92
  raise Mongous::Error, "type invalid. : #{ _client }"
90
93
  end
91
94
  self.class_variable_set( :@@client, _client )
@@ -21,11 +21,11 @@ module Mongous
21
21
 
22
22
  def collection_name
23
23
  if self.class_variable_defined?( :@@collection_name )
24
- self.class_variable_get( :@@collection_name )
25
- else
26
- _client_name = self.name
27
- self.class_variable_set( :@@collection_name, _client_name )
24
+ value = self.class_variable_get( :@@collection_name )
25
+ return value if value
28
26
  end
27
+
28
+ self.class_variable_set( :@@collection_name, self.name )
29
29
  end
30
30
 
31
31
  def collection_name=( _collection_name )
@@ -35,14 +35,17 @@ module Mongous
35
35
  end
36
36
  end
37
37
 
38
- def collection
39
- if self.class_variable_defined?( :@@collection )
40
- if _collection = self.class_variable_get( :@@collection )
41
- return _collection
38
+ def collection( temp_collection_name = nil )
39
+ if temp_collection_name.nil?
40
+ if self.class_variable_defined?( :@@collection )
41
+ if _collection = self.class_variable_get( :@@collection )
42
+ return _collection
43
+ end
42
44
  end
45
+ _collection_name = collection_name
46
+ else
47
+ _collection_name = temp_collection_name
43
48
  end
44
-
45
- _collection_name = collection_name
46
49
  _client = client
47
50
 
48
51
  if _client.database.collection_names.include?( _collection_name )
@@ -56,36 +59,39 @@ module Mongous
56
59
  _collection.indexes.create_one( keys, opts ) rescue nil
57
60
  end
58
61
 
59
- self.class_variable_set( :@@collection, _collection )
62
+ self.class_variable_set( :@@collection, _collection ) if temp_collection_name.nil?
63
+ _collection
60
64
  end
61
65
 
62
66
  def fields
63
- self_class_variable( :@@fields, {} )
67
+ setup_class_variable( :@@fields, {} )
64
68
  end
65
69
 
66
70
  def symbols
67
- self_class_variable( :@@symbols, {} )
71
+ setup_class_variable( :@@symbols, {} )
68
72
  end
69
73
 
70
74
  def blocks
71
- self_class_variable( :@@blocks, {} )
75
+ setup_class_variable( :@@blocks, {} )
72
76
  end
73
77
 
74
78
  def indexes
75
- self_class_variable( :@@indexes, [] )
79
+ setup_class_variable( :@@indexes, [] )
76
80
  end
77
81
 
78
82
  def filters
79
- self_class_variable( :@@filters, {} )
83
+ setup_class_variable( :@@filters, {} )
80
84
  end
81
85
 
82
86
  def defaults
83
- self_class_variable( :@@defaults, {} )
87
+ setup_class_variable( :@@defaults, {} )
84
88
  end
85
89
 
86
- def self_class_variable( symbol, default )
90
+ def setup_class_variable( symbol, default = {}, &block )
87
91
  if self.class_variable_defined?( symbol )
88
92
  self.class_variable_get( symbol )
93
+ elsif block_given?
94
+ self.class_variable_set( symbol, block.call )
89
95
  else
90
96
  self.class_variable_set( symbol, default )
91
97
  end
@@ -1,230 +1,273 @@
1
-
2
- module Mongous
3
- module Extention
4
- def count
5
- # self.collection.find.count
6
- self.collection.estimated_document_count
7
- end
8
-
9
- def first
10
- doc = self.collection.find.first
11
- self.new( **doc ) if doc
12
- end
13
-
14
- def all
15
- self.collection.find.map do |doc|
16
- self.new( **doc )
17
- end
18
- end
19
-
20
- def each( &block )
21
- all.each( &block )
22
- end
23
-
24
- def delete
25
- self.collection.delete_many({})
26
- end
27
-
28
- def where( filter = nil, **conditions )
29
- condition = normalize( filter, conditions )
30
- Filter.new( self ).where( condition )
31
- end
32
-
33
- def not( filter = nil, **conditions )
34
- raise Mongous::Error, "Unset args for #{self}.not." if filter.nil? && conditions.empty?
35
-
36
- condition = normalize( filter, conditions )
37
- Filter.new( self ).where({"$nor" => [condition]})
38
- end
39
-
40
- def and( *filters )
41
- raise Mongous::Error, "Unset args for #{self}.and." if filters.empty?
42
-
43
- conditions = filters.map do |filter|
44
- normalize( filter, {} )
45
- end
46
- Filter.new( self ).where({"$and" => conditions})
47
- end
48
-
49
- def or( *filters )
50
- raise Mongous::Error, "Unset args for #{self}.or." if filters.empty?
51
-
52
- conditions = filters.map do |filter|
53
- normalize( filter, {} )
54
- end
55
- Filter.new( self ).where({"$or" => conditions})
56
- end
57
-
58
- def normalize( filter, conditions )
59
- condition = case filter
60
- when Filter
61
- filter.to_condition
62
- when Symbol
63
- case _filter = filters[filter]
64
- when Filter
65
- _filter.to_condition
66
- when Hash
67
- _filter
68
- end
69
- when NilClass
70
- Filter.new( self ).where( **conditions ).to_condition
71
- else
72
- caller_method = /`(.*?)'/.match( caller()[0] )[1]
73
- raise Mongous::Error, "Invalid args for #{self}.#{ caller_method }. : #{filter}, #{conditions}"
74
- end
75
- end
76
- end
77
- end
78
-
79
- module Mongous
80
- class Filter
81
- def initialize( klass )
82
- @klass = klass
83
- @filter = {}
84
- @option = {}
85
- end
86
-
87
- def where( conditions )
88
- hash = {}
89
- conditions.each do |key, item|
90
- case key
91
- when /\$(and|or|nor)/
92
- hash[key] = item
93
-
94
- else
95
- case item
96
- when Array
97
- hash[key] = {"$in"=>item}
98
-
99
- when Range
100
- _begin_oper = "$gte"
101
- _end_oper = item.exclude_end? ? "$lt" : "$lte"
102
-
103
- if item.begin && item.end
104
- hash[key] = { _begin_oper => item.begin, _end_oper => item.end }
105
-
106
- elsif !item.begin && item.end
107
- hash[key] = { _end_oper => item.end }
108
-
109
- elsif item.begin && !item.end
110
- hash[key] = { _begin_oper => item.begin }
111
-
112
- else
113
- raise Mongous::Error, "invalid range. : #{ item }"
114
-
115
- end
116
-
117
- else
118
- hash[key] = item
119
-
120
- end
121
- end
122
- end
123
- @filter.merge!( hash )
124
- self.dup
125
- end
126
-
127
- def to_condition
128
- @filter.dup
129
- end
130
-
131
- def option( _option )
132
- @option.merge!( _option )
133
- self.dup
134
- end
135
-
136
- def projection( _projection )
137
- @projection = _projection
138
- self.dup
139
- end
140
- alias :select :projection
141
-
142
- def sort( _sort )
143
- @sort = _sort
144
- self.dup
145
- end
146
- alias :order :sort
147
-
148
- def []( nth_or_range, len = nil )
149
- case nth_or_range
150
- when Integer
151
- @skip = nth_or_range
152
-
153
- if len
154
- raise Mongous::Error, "invalid len. : #{ len }" if !len.is_a? Integer || len <= 0
155
- @limit = len
156
- end
157
-
158
- when Range
159
- from = nth_or_range.begin
160
- raise Mongous::Error, "invalid range. : #{ nth_or_range }" unless from.is_a? Integer
161
-
162
- to = nth_or_range.end
163
- raise Mongous::Error, "invalid range. : #{ nth_or_range }" unless to.is_a? Integer
164
-
165
- to -= 1 if nth_or_range.exclude_end?
166
- @skip = from
167
- @limit = to - from + 1
168
-
169
- else
170
- raise Mongous::Error, "invalid class. : #{ nth_or_range }"
171
-
172
- end
173
-
174
- self.dup
175
- end
176
-
177
- def do_find
178
- _filter = @filter
179
- _option = @option.dup
180
- _option[:projection] = @projection if @projection
181
- found = @klass.collection.find( _filter, _option )
182
- found = found.sort( @sort ) if @sort
183
- found = found.skip( @skip ) if @skip
184
- found = found.limit( @limit ) if @limit
185
- found
186
- end
187
-
188
- def count
189
- found = @klass.collection.find( @filter )
190
- found = found.skip( @skip ) if @skip
191
- found = found.limit( @limit ) if @limit
192
- _count = found.count_documents
193
- if @skip
194
- if @skip > _count
195
- 0
196
- elsif @limit
197
- [_count - @skip, @limit].min
198
- else
199
- _count - @skip
200
- end
201
- else
202
- if @limit
203
- [_count, @limit].min
204
- else
205
- _count
206
- end
207
- end
208
- end
209
-
210
- def first
211
- doc = do_find.first
212
- @klass.new( **doc ) if doc
213
- end
214
-
215
- def all
216
- do_find.map do |doc|
217
- @klass.new( **doc )
218
- end
219
- end
220
-
221
- def each( &block )
222
- all.each( &block )
223
- end
224
-
225
- def delete
226
- @klass.collection.delete_many( @filter )
227
- end
228
- end
229
- end
230
-
1
+
2
+ module Mongous
3
+ module Extention
4
+ def count
5
+ self.collection.estimated_document_count
6
+ end
7
+
8
+ def first
9
+ doc = self.collection.find.first
10
+ self.new( **doc ) if doc
11
+ end
12
+
13
+ def all
14
+ self.collection.find.map do |doc|
15
+ self.new( **doc )
16
+ end
17
+ end
18
+
19
+ def each( &block )
20
+ all.each( &block )
21
+ end
22
+
23
+ def map( &block )
24
+ all.map( &block )
25
+ end
26
+
27
+ def delete
28
+ self.collection.delete_many({})
29
+ end
30
+
31
+ def attach( collection_name )
32
+ Filter.new( self ).attach( collection_name )
33
+ end
34
+
35
+ def select( *keys, **hash )
36
+ Filter.new( self ).select( *keys, **hash )
37
+ end
38
+
39
+ def where( filter = nil, **conditions )
40
+ condition = normalize( filter, conditions )
41
+ Filter.new( self ).where( condition )
42
+ end
43
+
44
+ def not( filter = nil, **conditions )
45
+ raise Mongous::Error, "Unset args for #{self}.not." if filter.nil? && conditions.empty?
46
+
47
+ condition = normalize( filter, conditions )
48
+ Filter.new( self ).where({"$nor" => [condition]})
49
+ end
50
+
51
+ def and( *filters )
52
+ raise Mongous::Error, "Unset args for #{self}.and." if filters.empty?
53
+
54
+ conditions = filters.map do |filter|
55
+ normalize( filter, {} )
56
+ end
57
+ Filter.new( self ).where({"$and" => conditions})
58
+ end
59
+
60
+ def or( *filters )
61
+ raise Mongous::Error, "Unset args for #{self}.or." if filters.empty?
62
+
63
+ conditions = filters.map do |filter|
64
+ normalize( filter, {} )
65
+ end
66
+ Filter.new( self ).where({"$or" => conditions})
67
+ end
68
+
69
+ def normalize( filter, conditions )
70
+ condition = case filter
71
+ when Filter
72
+ filter.to_condition
73
+ when Symbol
74
+ case _filter = filters[filter]
75
+ when Filter
76
+ _filter.to_condition
77
+ when Hash
78
+ _filter
79
+ end
80
+ when NilClass
81
+ Filter.new( self ).where( **conditions ).to_condition
82
+ else
83
+ caller_method = /`(.*?)'/.match( caller()[0] )[1]
84
+ raise Mongous::Error, "Invalid args for #{self}.#{ caller_method }. : #{filter}, #{conditions}"
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ module Mongous
91
+ class Filter
92
+ def initialize( klass )
93
+ @klass = klass
94
+ @filter = {}
95
+ @option = {}
96
+ end
97
+
98
+ def attach( collection_name )
99
+ w = self.dup
100
+ w.instance_variable_set( :@collection_name, collection_name.to_s )
101
+ w
102
+ end
103
+
104
+ def where( conditions )
105
+ hash = {}
106
+ conditions.each do |key, item|
107
+ case key
108
+ when /\$(and|or|nor)/
109
+ hash[key] = item
110
+
111
+ else
112
+ case item
113
+ when Array
114
+ hash[key] = {"$in"=>item}
115
+
116
+ when Range
117
+ _begin_oper = "$gte"
118
+ _end_oper = item.exclude_end? ? "$lt" : "$lte"
119
+
120
+ if item.begin && item.end
121
+ hash[key] = { _begin_oper => item.begin, _end_oper => item.end }
122
+
123
+ elsif !item.begin && item.end
124
+ hash[key] = { _end_oper => item.end }
125
+
126
+ elsif item.begin && !item.end
127
+ hash[key] = { _begin_oper => item.begin }
128
+
129
+ else
130
+ raise Mongous::Error, "invalid range. : #{ item }"
131
+
132
+ end
133
+
134
+ else
135
+ hash[key] = item
136
+
137
+ end
138
+ end
139
+ end
140
+ w = self.dup
141
+ w.instance_variable_set( :@filter, @filter.merge( hash ) )
142
+ w
143
+ end
144
+
145
+ def to_condition
146
+ @filter.dup
147
+ end
148
+
149
+ def option( _option )
150
+ w = self.dup
151
+ w.instance_variable_set( :@option, @option.merge( _option ) )
152
+ w
153
+ end
154
+
155
+ def projection( *keys, **hash )
156
+ if not keys.empty?
157
+ _projection = Hash[ keys.zip( Array.new(keys.length, 1) ) ]
158
+ elsif not hash.empty?
159
+ _projection = hash
160
+ else
161
+ _projection = nil
162
+ end
163
+ w = self.dup
164
+ w.instance_variable_set( :@projection, _projection )
165
+ w
166
+ end
167
+ alias :select :projection
168
+
169
+ def sort( *keys, **hash )
170
+ if not keys.empty?
171
+ _sort = Hash[ keys.zip( Array.new( keys.length, 1 ) ) ]
172
+ elsif not hash.empty?
173
+ _sort = hash
174
+ else
175
+ _sort = nil
176
+ end
177
+ w = self.dup
178
+ w.instance_variable_set( :@sort, _sort )
179
+ w
180
+ end
181
+ alias :order :sort
182
+
183
+ def []( nth_or_range, len = nil )
184
+ case nth_or_range
185
+ when Integer
186
+ _skip = nth_or_range
187
+ _limit = nil
188
+
189
+ if len
190
+ raise Mongous::Error, "invalid len. : #{ len }" if !len.is_a? Integer || len <= 0
191
+ _limit = len
192
+ end
193
+
194
+ when Range
195
+ from = nth_or_range.begin
196
+ raise Mongous::Error, "invalid range. : #{ nth_or_range }" unless from.is_a? Integer
197
+
198
+ to = nth_or_range.end
199
+ raise Mongous::Error, "invalid range. : #{ nth_or_range }" unless to.is_a? Integer
200
+
201
+ to -= 1 if nth_or_range.exclude_end?
202
+ _skip = from
203
+ _limit = to - from + 1
204
+
205
+ else
206
+ raise Mongous::Error, "invalid class. : #{ nth_or_range }"
207
+
208
+ end
209
+
210
+ w = self.dup
211
+ w.instance_variable_set( :@skip, _skip )
212
+ w.instance_variable_set( :@limit, _limit )
213
+ w
214
+ end
215
+
216
+ def exec_query
217
+ _filter = @filter
218
+ _option = @option.dup
219
+ _option[:projection] = @projection if @projection
220
+ found = @klass.collection( @collection_name ).find( _filter, _option )
221
+ found = found.sort( @sort ) if @sort
222
+ found = found.skip( @skip ) if @skip
223
+ found = found.limit( @limit ) if @limit
224
+ found
225
+ end
226
+
227
+ def count
228
+ found = @klass.collection.find( @filter )
229
+ found = found.skip( @skip ) if @skip
230
+ found = found.limit( @limit ) if @limit
231
+ _count = found.count_documents
232
+ if @skip
233
+ if @skip > _count
234
+ 0
235
+ elsif @limit
236
+ [_count - @skip, @limit].min
237
+ else
238
+ _count - @skip
239
+ end
240
+ else
241
+ if @limit
242
+ [_count, @limit].min
243
+ else
244
+ _count
245
+ end
246
+ end
247
+ end
248
+
249
+ def first
250
+ doc = exec_query.first
251
+ @klass.new( **doc ) if doc
252
+ end
253
+
254
+ def all
255
+ exec_query.map do |doc|
256
+ @klass.new( **doc )
257
+ end
258
+ end
259
+
260
+ def each( &block )
261
+ all.each( &block )
262
+ end
263
+
264
+ def map( &block )
265
+ all.map( &block )
266
+ end
267
+
268
+ def delete
269
+ @klass.collection.delete_many( @filter )
270
+ end
271
+ end
272
+ end
273
+