mongous 0.1.8 → 0.2.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.
- checksums.yaml +4 -4
- data/README.adoc +115 -30
- data/README.ja.adoc +115 -30
- data/lib/mongous/document.rb +50 -27
- data/lib/mongous/extention.rb +50 -43
- data/lib/mongous/filter.rb +45 -51
- data/lib/mongous/version.rb +1 -1
- data/sample/declare_compact_1.rb +1 -1
- data/sample/declare_label_1.rb +1 -1
- data/sample/declare_ndex_1.rb +1 -1
- data/sample/declare_ndex_2.rb +1 -1
- data/sample/declare_strict_1.rb +7 -5
- data/sample/declare_strict_2.rb +3 -2
- data/sample/{multi_docs_1.rb → multi_collection_1.rb} +0 -0
- data/sample/multi_collection_2.rb +61 -0
- data/sample/query_basic_1.rb +0 -1
- data/sample/query_basic_2.rb +4 -5
- data/sample/query_basic_3.rb +2 -3
- data/sample/query_basic_4.rb +3 -4
- data/sample/query_basic_5.rb +2 -3
- data/sample/query_detail_3.rb +3 -2
- data/sample/{query_basic_6.rb → query_filter_1.rb} +7 -4
- data/sample/query_filter_2.rb +50 -0
- data/sample/{query_super_1.rb → query_find_1.rb} +0 -1
- data/sample/query_projection_1.rb +5 -8
- data/sample/query_skip_limit_1.rb +37 -0
- data/sample/query_skip_limit_2.rb +49 -0
- data/sample/query_sort_order_1.rb +46 -0
- data/sample/save_basic_1.rb +0 -1
- data/sample/save_basic_2.rb +0 -1
- data/sample/save_basic_3.rb +0 -1
- data/sample/save_detail_1.rb +4 -3
- data/sample/save_detail_2.rb +4 -3
- data/sample/save_detail_3.rb +6 -5
- data/sample/save_verify_1.rb +3 -2
- data/sample/save_verify_3.rb +1 -1
- data/sample/zap_basic_2.rb +1 -1
- data/sample/zap_basic_3.rb +1 -1
- metadata +10 -11
- data/sample/multi_docs_2.rb +0 -58
- data/sample/query_basic_7.rb +0 -38
- data/sample/query_limit_1.rb +0 -44
- data/sample/query_order_1.rb +0 -49
- data/sample/template_article.rb +0 -5
- data/sample/template_person.rb +0 -12
data/lib/mongous/document.rb
CHANGED
@@ -26,33 +26,58 @@ module Mongous
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
def having?(
|
30
|
-
case
|
29
|
+
def having?( label )
|
30
|
+
case label
|
31
31
|
when NilClass
|
32
32
|
false
|
33
33
|
when String
|
34
|
-
!
|
34
|
+
!label.strip.empty?
|
35
35
|
else
|
36
36
|
true
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
+
def getvalue_or_callproc( value_or_proc )
|
41
|
+
case value_or_proc
|
42
|
+
when Proc
|
43
|
+
value_or_proc.call
|
44
|
+
else
|
45
|
+
value_or_proc
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
40
49
|
def save
|
50
|
+
if @doc["_id"].nil? || self.class.collection.find( { "_id"=> @doc["_id"] } ).count == 0
|
51
|
+
savemode = :create
|
52
|
+
else
|
53
|
+
savemode = :update
|
54
|
+
end
|
41
55
|
self.class.fields.each do |label, field|
|
42
|
-
_must = field[:
|
56
|
+
_must = field[:_attrs].include?(:must)
|
43
57
|
if @doc.has_key?(label)
|
44
58
|
if _must && !having?( @doc[label] )
|
45
59
|
raise Mongous::Error, "must and not having field. : #{ label }"
|
46
60
|
end
|
47
61
|
else
|
48
|
-
if
|
49
|
-
self[label] =
|
62
|
+
if default_value = getvalue_or_callproc( field[:default] )
|
63
|
+
self[label] = default_value
|
50
64
|
elsif _must
|
51
65
|
raise Mongous::Error, "must and unassigned field. : #{ label }"
|
52
66
|
elsif self.class.symbols[:strict]
|
53
67
|
self[label] = nil
|
54
68
|
end
|
55
69
|
end
|
70
|
+
|
71
|
+
case savemode
|
72
|
+
when :create
|
73
|
+
if create_value = getvalue_or_callproc( field[:create] )
|
74
|
+
self[label] = create_value
|
75
|
+
end
|
76
|
+
when :update
|
77
|
+
if update_value = getvalue_or_callproc( field[:update] )
|
78
|
+
self[label] = update_value
|
79
|
+
end
|
80
|
+
end
|
56
81
|
end
|
57
82
|
|
58
83
|
self.class.blocks.each do |call_from, block|
|
@@ -61,15 +86,13 @@ module Mongous
|
|
61
86
|
end
|
62
87
|
end
|
63
88
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
end
|
89
|
+
case savemode
|
90
|
+
when :create
|
91
|
+
self.class.collection.insert_one( @doc )
|
92
|
+
when :update
|
93
|
+
self.class.collection.update_one( { "_id"=> @doc["_id"] }, { '$set' => @doc } )
|
70
94
|
end
|
71
95
|
|
72
|
-
self.class.collection.insert_one( @doc )
|
73
96
|
self
|
74
97
|
end
|
75
98
|
|
@@ -108,18 +131,18 @@ module Mongous
|
|
108
131
|
return @doc[label] = value if field.nil?
|
109
132
|
|
110
133
|
types = []
|
111
|
-
if
|
112
|
-
|
113
|
-
if
|
114
|
-
types <<
|
115
|
-
|
134
|
+
if attrs = field[:_attrs] || []
|
135
|
+
attrs.each do |attr|
|
136
|
+
if attr.class == Class
|
137
|
+
types << attr
|
138
|
+
attrs -= [attr]
|
116
139
|
break
|
117
140
|
end
|
118
141
|
end
|
119
142
|
end
|
120
143
|
|
121
144
|
if !types.empty?
|
122
|
-
if !(
|
145
|
+
if !(attrs & [:must, :not_null]).empty?
|
123
146
|
if !types.include?( value.class )
|
124
147
|
raise Mongous::Error, "invalid type. : #{ label } : #{ value.class }"
|
125
148
|
end
|
@@ -129,7 +152,7 @@ module Mongous
|
|
129
152
|
end
|
130
153
|
end
|
131
154
|
else
|
132
|
-
if !(
|
155
|
+
if !(attrs & [:must, :not_null]).empty?
|
133
156
|
if [NilClass].include?( value.class )
|
134
157
|
raise Mongous::Error, "invalid type. : #{ label } : #{ value.class }"
|
135
158
|
end
|
@@ -138,19 +161,19 @@ module Mongous
|
|
138
161
|
|
139
162
|
@doc[label] = value
|
140
163
|
|
141
|
-
|
142
|
-
case
|
164
|
+
attrs.each do |attr|
|
165
|
+
case attr
|
143
166
|
when Proc
|
144
|
-
if !self.instance_eval( &
|
167
|
+
if !self.instance_eval( &attr )
|
145
168
|
raise Mongous::Error, "violation detected. : #{ label } : #{ value }"
|
146
169
|
end
|
147
170
|
when Array
|
148
|
-
if !
|
149
|
-
raise Mongous::Error, "not include. : #{ label }
|
171
|
+
if !attr.include?( value )
|
172
|
+
raise Mongous::Error, "not include. : #{ label } : #{ value }"
|
150
173
|
end
|
151
174
|
when Range
|
152
|
-
if !
|
153
|
-
raise Mongous::Error, "out of range. : #{ label }
|
175
|
+
if !attr.cover?( value )
|
176
|
+
raise Mongous::Error, "out of range. : #{ label } : #{ value }"
|
154
177
|
end
|
155
178
|
end
|
156
179
|
end
|
data/lib/mongous/extention.rb
CHANGED
@@ -60,34 +60,34 @@ module Mongous
|
|
60
60
|
end
|
61
61
|
|
62
62
|
def fields
|
63
|
-
|
64
|
-
self.class_variable_get( :@@fields )
|
65
|
-
else
|
66
|
-
self.class_variable_set( :@@fields, {} )
|
67
|
-
end
|
63
|
+
self_class_variable( :@@fields, {} )
|
68
64
|
end
|
69
65
|
|
70
66
|
def symbols
|
71
|
-
|
72
|
-
self.class_variable_get( :@@symbols )
|
73
|
-
else
|
74
|
-
self.class_variable_set( :@@symbols, {} )
|
75
|
-
end
|
67
|
+
self_class_variable( :@@symbols, {} )
|
76
68
|
end
|
77
69
|
|
78
70
|
def blocks
|
79
|
-
|
80
|
-
self.class_variable_get( :@@blocks )
|
81
|
-
else
|
82
|
-
self.class_variable_set( :@@blocks, {} )
|
83
|
-
end
|
71
|
+
self_class_variable( :@@blocks, {} )
|
84
72
|
end
|
85
73
|
|
86
74
|
def indexes
|
87
|
-
|
88
|
-
|
75
|
+
self_class_variable( :@@indexes, [] )
|
76
|
+
end
|
77
|
+
|
78
|
+
def filters
|
79
|
+
self_class_variable( :@@filters, {} )
|
80
|
+
end
|
81
|
+
|
82
|
+
def defaults
|
83
|
+
self_class_variable( :@@defaults, {} )
|
84
|
+
end
|
85
|
+
|
86
|
+
def self_class_variable( symbol, default )
|
87
|
+
if self.class_variable_defined?( symbol )
|
88
|
+
self.class_variable_get( symbol )
|
89
89
|
else
|
90
|
-
self.class_variable_set(
|
90
|
+
self.class_variable_set( symbol, default )
|
91
91
|
end
|
92
92
|
end
|
93
93
|
|
@@ -99,38 +99,32 @@ module Mongous
|
|
99
99
|
self.collection.find( conditios, options )
|
100
100
|
end
|
101
101
|
|
102
|
-
def field(
|
102
|
+
def field( symbol, *attrs, **items )
|
103
103
|
m = /(.*?):(\d+)/.match( caller()[0] )
|
104
104
|
call_from = [ m[1], m[2] ].join(":")
|
105
105
|
|
106
|
-
|
107
|
-
if klass =
|
106
|
+
attrs.each do |attr|
|
107
|
+
if klass = attr.class
|
108
108
|
if ![Class, Range, Array, Proc, Symbol].include?(klass)
|
109
|
-
raise Mongous::Error, "field error. :
|
109
|
+
raise Mongous::Error, "field args error. : #{ attr } on #{ symbol } at #{ call_from }"
|
110
110
|
end
|
111
111
|
end
|
112
112
|
end
|
113
113
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
when Proc, String, Numeric
|
119
|
-
else
|
120
|
-
raise Mongous::Error, "field error. : #{key} on #{ label } at #{ call_from }"
|
121
|
-
end
|
122
|
-
end
|
114
|
+
items.each do |key, value|
|
115
|
+
next if [:default, :create, :update].include?(key) && [Proc, String, Numeric].include?(value.class)
|
116
|
+
|
117
|
+
raise Mongous::Error, "field opts error. : #{key} on #{ symbol } at #{ call_from }"
|
123
118
|
end
|
124
119
|
|
125
|
-
|
126
|
-
|
127
|
-
fields[label.to_s] = opts
|
120
|
+
items[:_attrs] = attrs
|
121
|
+
fields[symbol.to_s] = items
|
128
122
|
end
|
129
123
|
|
130
|
-
def verify( *
|
131
|
-
if !
|
132
|
-
|
133
|
-
symbols[
|
124
|
+
def verify( *directives, &block )
|
125
|
+
if !directives.empty?
|
126
|
+
directives.each do |directive|
|
127
|
+
symbols[directive] = true
|
134
128
|
end
|
135
129
|
elsif block
|
136
130
|
m = /(.*?):(\d+)/.match( caller()[0] )
|
@@ -139,13 +133,26 @@ module Mongous
|
|
139
133
|
end
|
140
134
|
end
|
141
135
|
|
142
|
-
def index( *
|
143
|
-
|
136
|
+
def index( *symbols, **options )
|
137
|
+
options[:background] = true unless options.has_key?(:background)
|
144
138
|
keys = {}
|
145
|
-
|
146
|
-
keys[
|
139
|
+
symbols.each do |symbol|
|
140
|
+
keys[symbol] = 1
|
141
|
+
end
|
142
|
+
indexes.push << [keys, options]
|
143
|
+
end
|
144
|
+
|
145
|
+
def filter( symbol, filter_or_condition )
|
146
|
+
case filter_or_condition
|
147
|
+
when Filter
|
148
|
+
filters[symbol] = filter_or_condition.to_condition
|
149
|
+
when Hash
|
150
|
+
filters[symbol] = filter_or_condition
|
151
|
+
else
|
152
|
+
m = /(.*?):(\d+)/.match( caller()[0] )
|
153
|
+
call_from = [ m[1], m[2] ].join(":")
|
154
|
+
raise Mongous::Error, "filter error. : #{symbol}, #{filter_or_condition} at #{ call_from }"
|
147
155
|
end
|
148
|
-
indexes.push << [keys, opts]
|
149
156
|
end
|
150
157
|
end
|
151
158
|
end
|
data/lib/mongous/filter.rb
CHANGED
@@ -24,55 +24,53 @@ module Mongous
|
|
24
24
|
self.collection.delete_many({})
|
25
25
|
end
|
26
26
|
|
27
|
-
def
|
28
|
-
|
27
|
+
def where( filter = nil, **conditions )
|
28
|
+
condition = normalize( filter, conditions )
|
29
|
+
Filter.new( self ).where( condition )
|
29
30
|
end
|
30
31
|
|
31
32
|
def not( filter = nil, **conditions )
|
32
33
|
raise Mongous::Error, "Unset args for #{self}.not." if filter.nil? && conditions.empty?
|
33
34
|
|
34
|
-
|
35
|
-
condition
|
36
|
-
when Hash
|
37
|
-
Filter.new( self ).filter( filter ).to_condition
|
38
|
-
when Filter
|
39
|
-
filter.to_condition
|
40
|
-
else
|
41
|
-
raise Mongous::Error, "Invalid args for #{self}.not. : #{filter}"
|
42
|
-
end
|
43
|
-
Filter.new( self ).filter({"$nor" => [condition]})
|
35
|
+
condition = normalize( filter, conditions )
|
36
|
+
Filter.new( self ).where({"$nor" => [condition]})
|
44
37
|
end
|
45
38
|
|
46
39
|
def and( *filters )
|
47
40
|
raise Mongous::Error, "Unset args for #{self}.and." if filters.empty?
|
48
41
|
|
49
42
|
conditions = filters.map do |filter|
|
50
|
-
|
51
|
-
when Hash
|
52
|
-
filter
|
53
|
-
when Filter
|
54
|
-
filter.to_condition
|
55
|
-
else
|
56
|
-
raise Mongous::Error, "Invalid args for #{self}.and. : #{filter}"
|
57
|
-
end
|
43
|
+
normalize( filter, {} )
|
58
44
|
end
|
59
|
-
Filter.new( self ).
|
45
|
+
Filter.new( self ).where({"$and" => conditions})
|
60
46
|
end
|
61
47
|
|
62
48
|
def or( *filters )
|
63
49
|
raise Mongous::Error, "Unset args for #{self}.or." if filters.empty?
|
64
50
|
|
65
51
|
conditions = filters.map do |filter|
|
66
|
-
|
67
|
-
|
68
|
-
|
52
|
+
normalize( filter, {} )
|
53
|
+
end
|
54
|
+
Filter.new( self ).where({"$or" => conditions})
|
55
|
+
end
|
56
|
+
|
57
|
+
def normalize( filter, conditions )
|
58
|
+
condition = case filter
|
59
|
+
when Filter
|
60
|
+
filter.to_condition
|
61
|
+
when Symbol
|
62
|
+
case _filter = filters[filter]
|
69
63
|
when Filter
|
70
|
-
|
71
|
-
|
72
|
-
|
64
|
+
_filter.to_condition
|
65
|
+
when Hash
|
66
|
+
_filter
|
73
67
|
end
|
68
|
+
when NilClass
|
69
|
+
Filter.new( self ).where( **conditions ).to_condition
|
70
|
+
else
|
71
|
+
caller_method = /`(.*?)'/.match( caller()[0] )[1]
|
72
|
+
raise Mongous::Error, "Invalid args for #{self}.#{ caller_method }. : #{filter}, #{conditions}"
|
74
73
|
end
|
75
|
-
Filter.new( self ).filter({"$or" => conditions})
|
76
74
|
end
|
77
75
|
end
|
78
76
|
end
|
@@ -85,7 +83,7 @@ module Mongous
|
|
85
83
|
@option = {}
|
86
84
|
end
|
87
85
|
|
88
|
-
def
|
86
|
+
def where( conditions )
|
89
87
|
hash = {}
|
90
88
|
conditions.each do |key, item|
|
91
89
|
case key
|
@@ -146,25 +144,15 @@ module Mongous
|
|
146
144
|
end
|
147
145
|
alias :order :sort
|
148
146
|
|
149
|
-
def
|
150
|
-
@skip = _skip
|
151
|
-
self.dup
|
152
|
-
end
|
153
|
-
alias :offset :skip
|
154
|
-
|
155
|
-
def limit( _limit )
|
156
|
-
@limit = _limit
|
157
|
-
self.dup
|
158
|
-
end
|
159
|
-
|
160
|
-
def []( nth_or_range, len = 1 )
|
147
|
+
def []( nth_or_range, len = nil )
|
161
148
|
case nth_or_range
|
162
149
|
when Integer
|
163
|
-
raise Mongous::Error, "invalid nth. : #{ nth_or_range }" if len < 0
|
164
150
|
@skip = nth_or_range
|
165
151
|
|
166
|
-
|
167
|
-
|
152
|
+
if len
|
153
|
+
raise Mongous::Error, "invalid len. : #{ len }" if !len.is_a? Integer || len <= 0
|
154
|
+
@limit = len
|
155
|
+
end
|
168
156
|
|
169
157
|
when Range
|
170
158
|
from = nth_or_range.begin
|
@@ -198,14 +186,20 @@ module Mongous
|
|
198
186
|
|
199
187
|
def count
|
200
188
|
_count = do_find.count
|
201
|
-
if @skip
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
189
|
+
if @skip
|
190
|
+
if @skip > _count
|
191
|
+
0
|
192
|
+
elsif @limit
|
193
|
+
[_count - @skip, @limit].min
|
194
|
+
else
|
195
|
+
_count - @skip
|
196
|
+
end
|
207
197
|
else
|
208
|
-
|
198
|
+
if @limit
|
199
|
+
[_count, @limit].min
|
200
|
+
else
|
201
|
+
_count
|
202
|
+
end
|
209
203
|
end
|
210
204
|
end
|
211
205
|
|
data/lib/mongous/version.rb
CHANGED
data/sample/declare_compact_1.rb
CHANGED