mongous 0.1.8 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/README.adoc +115 -30
  3. data/README.ja.adoc +115 -30
  4. data/lib/mongous/document.rb +50 -27
  5. data/lib/mongous/extention.rb +50 -43
  6. data/lib/mongous/filter.rb +45 -51
  7. data/lib/mongous/version.rb +1 -1
  8. data/sample/declare_compact_1.rb +1 -1
  9. data/sample/declare_label_1.rb +1 -1
  10. data/sample/declare_ndex_1.rb +1 -1
  11. data/sample/declare_ndex_2.rb +1 -1
  12. data/sample/declare_strict_1.rb +7 -5
  13. data/sample/declare_strict_2.rb +3 -2
  14. data/sample/{multi_docs_1.rb → multi_collection_1.rb} +0 -0
  15. data/sample/multi_collection_2.rb +61 -0
  16. data/sample/query_basic_1.rb +0 -1
  17. data/sample/query_basic_2.rb +4 -5
  18. data/sample/query_basic_3.rb +2 -3
  19. data/sample/query_basic_4.rb +3 -4
  20. data/sample/query_basic_5.rb +2 -3
  21. data/sample/query_detail_3.rb +3 -2
  22. data/sample/{query_basic_6.rb → query_filter_1.rb} +7 -4
  23. data/sample/query_filter_2.rb +50 -0
  24. data/sample/{query_super_1.rb → query_find_1.rb} +0 -1
  25. data/sample/query_projection_1.rb +5 -8
  26. data/sample/query_skip_limit_1.rb +37 -0
  27. data/sample/query_skip_limit_2.rb +49 -0
  28. data/sample/query_sort_order_1.rb +46 -0
  29. data/sample/save_basic_1.rb +0 -1
  30. data/sample/save_basic_2.rb +0 -1
  31. data/sample/save_basic_3.rb +0 -1
  32. data/sample/save_detail_1.rb +4 -3
  33. data/sample/save_detail_2.rb +4 -3
  34. data/sample/save_detail_3.rb +6 -5
  35. data/sample/save_verify_1.rb +3 -2
  36. data/sample/save_verify_3.rb +1 -1
  37. data/sample/zap_basic_2.rb +1 -1
  38. data/sample/zap_basic_3.rb +1 -1
  39. metadata +10 -11
  40. data/sample/multi_docs_2.rb +0 -58
  41. data/sample/query_basic_7.rb +0 -38
  42. data/sample/query_limit_1.rb +0 -44
  43. data/sample/query_order_1.rb +0 -49
  44. data/sample/template_article.rb +0 -5
  45. data/sample/template_person.rb +0 -12
@@ -26,33 +26,58 @@ module Mongous
26
26
  end
27
27
  end
28
28
 
29
- def having?( value )
30
- case value
29
+ def having?( label )
30
+ case label
31
31
  when NilClass
32
32
  false
33
33
  when String
34
- !value.strip.empty?
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[:_args].include?(:must)
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 block = field[:_block]
49
- self[label] = block.call
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
- if @doc["_id"]
65
- _filter = { "_id"=> @doc["_id"] }
66
- if self.class.collection.find( _filter ).first
67
- self.class.collection.update_one( _filter, { '$set' => @doc } )
68
- return self
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 args = field[:_args] || []
112
- args.each do |arg|
113
- if arg.class == Class
114
- types << arg
115
- args -= [arg]
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 !(args & [:must, :not_null]).empty?
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 !(args & [:must, :not_null]).empty?
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
- args.each do |arg|
142
- case arg
164
+ attrs.each do |attr|
165
+ case attr
143
166
  when Proc
144
- if !self.instance_eval( &arg )
167
+ if !self.instance_eval( &attr )
145
168
  raise Mongous::Error, "violation detected. : #{ label } : #{ value }"
146
169
  end
147
170
  when Array
148
- if !arg.include?( value )
149
- raise Mongous::Error, "not include. : #{ label } :#{ value }"
171
+ if !attr.include?( value )
172
+ raise Mongous::Error, "not include. : #{ label } : #{ value }"
150
173
  end
151
174
  when Range
152
- if !arg.cover?( value )
153
- raise Mongous::Error, "out of range. : #{ label } :#{ value }"
175
+ if !attr.cover?( value )
176
+ raise Mongous::Error, "out of range. : #{ label } : #{ value }"
154
177
  end
155
178
  end
156
179
  end
@@ -60,34 +60,34 @@ module Mongous
60
60
  end
61
61
 
62
62
  def fields
63
- if self.class_variable_defined?( :@@fields )
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
- if self.class_variable_defined?( :@@symbols )
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
- if self.class_variable_defined?( :@@blocks )
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
- if self.class_variable_defined?( :@@indexes )
88
- self.class_variable_get( :@@indexes )
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( :@@indexes, [] )
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( label, *args, **opts, &block )
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
- args.each do |arg|
107
- if klass = arg.class
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. : #{arg} on #{ label } at #{ call_from }"
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
- opts.each do |key, value|
115
- case key
116
- when :default
117
- case value
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
- opts[:_args] = args
126
- opts[:_block] = block
127
- fields[label.to_s] = opts
120
+ items[:_attrs] = attrs
121
+ fields[symbol.to_s] = items
128
122
  end
129
123
 
130
- def verify( *syms, &block )
131
- if !syms.empty?
132
- syms.each do |sym|
133
- symbols[sym] = true
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( *syms, **opts )
143
- opts[:background] = true unless opts.has_key?(:background)
136
+ def index( *symbols, **options )
137
+ options[:background] = true unless options.has_key?(:background)
144
138
  keys = {}
145
- syms.each do |sym|
146
- keys[sym] = 1
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
@@ -24,55 +24,53 @@ module Mongous
24
24
  self.collection.delete_many({})
25
25
  end
26
26
 
27
- def filter( **conditions )
28
- Filter.new( self ).filter( conditions )
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
- filter ||= conditions
35
- condition = case filter
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
- case filter
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 ).filter({"$and" => conditions})
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
- case filter
67
- when Hash
68
- filter
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
- filter.to_condition
71
- else
72
- raise Mongous::Error, "Invalid args for #{self}.or. : #{filter}"
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 filter( conditions )
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 skip( _skip )
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
- raise Mongous::Error, "invalid len. : #{ len }" if !len.is_a? Integer || len <= 0
167
- @limit = len
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 && @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
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
- _count
198
+ if @limit
199
+ [_count, @limit].min
200
+ else
201
+ _count
202
+ end
209
203
  end
210
204
  end
211
205
 
@@ -1,3 +1,3 @@
1
1
  module Mongous
2
- VERSION = "0.1.8"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -9,7 +9,7 @@ end
9
9
 
10
10
 
11
11
  book = Book.new
12
- book.title = "title compact"
12
+ book.title = "declare compact"
13
13
  book.author = "foobar"
14
14
  book.publisher = nil
15
15
  book.style = "A4"
@@ -21,7 +21,7 @@ end
21
21
 
22
22
 
23
23
  book = Book.new
24
- book.title = "title label"
24
+ book.title = "declare label"
25
25
  book.author = "foobar"
26
26
  book.publisher = nil
27
27
  book.style = "A5"