mongo_mapper-unstable 2010.2.9 → 2010.2.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,81 +5,20 @@ module MongoMapper
5
5
  def per_page
6
6
  25
7
7
  end
8
-
8
+
9
9
  def paginate(options)
10
10
  per_page = options.delete(:per_page) || self.per_page
11
11
  page = options.delete(:page)
12
12
  total_entries = count(options)
13
- pagination = Pagination::PaginationProxy.new(total_entries, page, per_page)
13
+ pagination = Pagination::Proxy.new(total_entries, page, per_page)
14
14
 
15
- options.merge!(:limit => pagination.limit, :skip => pagination.skip)
15
+ options.update(:limit => pagination.limit, :skip => pagination.skip)
16
16
  pagination.subject = find_many(options)
17
17
  pagination
18
18
  end
19
19
  end
20
-
21
- class PaginationProxy
22
- instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$|proxy_|^object_id$)/ }
23
-
24
- attr_accessor :subject
25
- attr_reader :total_entries, :per_page, :current_page
26
- alias limit per_page
27
-
28
- def initialize(total_entries, current_page, per_page=nil)
29
- @total_entries = total_entries.to_i
30
- self.per_page = per_page
31
- self.current_page = current_page
32
- end
33
-
34
- def total_pages
35
- (total_entries / per_page.to_f).ceil
36
- end
37
-
38
- def out_of_bounds?
39
- current_page > total_pages
40
- end
41
-
42
- def previous_page
43
- current_page > 1 ? (current_page - 1) : nil
44
- end
45
-
46
- def next_page
47
- current_page < total_pages ? (current_page + 1) : nil
48
- end
49
-
50
- def skip
51
- (current_page - 1) * per_page
52
- end
53
- alias offset skip # for will paginate support
54
-
55
- def send(method, *args, &block)
56
- if respond_to?(method)
57
- super
58
- else
59
- subject.send(method, *args, &block)
60
- end
61
- end
62
-
63
- def ===(other)
64
- other === subject
65
- end
66
-
67
- def method_missing(name, *args, &block)
68
- @subject.send(name, *args, &block)
69
- end
70
-
71
- private
72
- def per_page=(value)
73
- value = 25 if value.blank?
74
- @per_page = value.to_i
75
- end
76
-
77
- def current_page=(value)
78
- value = value.to_i
79
- value = 1 if value < 1
80
- @current_page = value
81
- end
82
- end
83
20
  end
84
21
  end
85
- end
22
+ end
23
+
24
+ require 'mongo_mapper/plugins/pagination/proxy'
@@ -3,28 +3,29 @@ module MongoMapper
3
3
  def plugins
4
4
  @plugins ||= []
5
5
  end
6
-
6
+
7
7
  def plugin(mod)
8
8
  extend mod::ClassMethods if mod.const_defined?(:ClassMethods)
9
9
  include mod::InstanceMethods if mod.const_defined?(:InstanceMethods)
10
10
  mod.configure(self) if mod.respond_to?(:configure)
11
11
  plugins << mod
12
12
  end
13
+
14
+ autoload :Callbacks, 'mongo_mapper/plugins/callbacks'
15
+ autoload :Clone, 'mongo_mapper/plugins/clone'
16
+ autoload :Descendants, 'mongo_mapper/plugins/descendants'
17
+ autoload :Dirty, 'mongo_mapper/plugins/dirty'
18
+ autoload :Equality, 'mongo_mapper/plugins/equality'
19
+ autoload :IdentityMap, 'mongo_mapper/plugins/identity_map'
20
+ autoload :Inspect, 'mongo_mapper/plugins/inspect'
21
+ autoload :Keys, 'mongo_mapper/plugins/keys'
22
+ autoload :Logger, 'mongo_mapper/plugins/logger'
23
+ autoload :Protected, 'mongo_mapper/plugins/protected'
24
+ autoload :Rails, 'mongo_mapper/plugins/rails'
25
+ autoload :Serialization, 'mongo_mapper/plugins/serialization'
26
+ autoload :Validations, 'mongo_mapper/plugins/validations'
13
27
  end
14
28
  end
15
29
 
16
30
  require 'mongo_mapper/plugins/associations'
17
- require 'mongo_mapper/plugins/callbacks'
18
- require 'mongo_mapper/plugins/clone'
19
- require 'mongo_mapper/plugins/descendants'
20
- require 'mongo_mapper/plugins/dirty'
21
- require 'mongo_mapper/plugins/equality'
22
- require 'mongo_mapper/plugins/identity_map'
23
- require 'mongo_mapper/plugins/inspect'
24
- require 'mongo_mapper/plugins/keys'
25
- require 'mongo_mapper/plugins/logger'
26
31
  require 'mongo_mapper/plugins/pagination'
27
- require 'mongo_mapper/plugins/protected'
28
- require 'mongo_mapper/plugins/rails'
29
- require 'mongo_mapper/plugins/serialization'
30
- require 'mongo_mapper/plugins/validations'
@@ -0,0 +1,130 @@
1
+ module MongoMapper
2
+ # IMPORTANT
3
+ # This class is private to MongoMapper and should not be considered part of MongoMapper's public API.
4
+ #
5
+ class Query
6
+ OptionKeys = [:fields, :select, :skip, :offset, :limit, :sort, :order]
7
+
8
+ attr_reader :model
9
+
10
+ def initialize(model, options)
11
+ raise ArgumentError, "Options must be a hash" unless options.is_a?(Hash)
12
+ @model, @options, @conditions, @original_options = model, {}, {}, options
13
+ separate_options_and_conditions
14
+ add_sci_condition
15
+ end
16
+
17
+ def criteria
18
+ to_criteria(@conditions)
19
+ end
20
+
21
+ def options
22
+ fields = @options[:fields] || @options[:select]
23
+ skip = @options[:skip] || @options[:offset] || 0
24
+ limit = @options[:limit] || 0
25
+ sort = @options[:sort] || normalized_sort(@options[:order])
26
+
27
+ {:fields => to_fields(fields), :skip => skip.to_i, :limit => limit.to_i, :sort => sort}
28
+ end
29
+
30
+ def to_a
31
+ [criteria, options]
32
+ end
33
+
34
+ private
35
+ def separate_options_and_conditions
36
+ @original_options.each_pair do |key, value|
37
+ key = key.respond_to?(:to_sym) ? key.to_sym : key
38
+
39
+ if OptionKeys.include?(key)
40
+ @options[key] = value
41
+ elsif key == :conditions
42
+ @conditions.update(value)
43
+ else
44
+ @conditions[key] = value
45
+ end
46
+ end
47
+ end
48
+
49
+ # adds _type single collection inheritance scope for models that need it
50
+ def add_sci_condition
51
+ @conditions[:_type] = model.to_s if model.single_collection_inherited?
52
+ end
53
+
54
+ def modifier?(field)
55
+ field.to_s =~ /^\$/
56
+ end
57
+
58
+ def symbol_operator?(object)
59
+ object.respond_to?(:field, :operator)
60
+ end
61
+
62
+ def to_criteria(conditions, parent_key=nil)
63
+ criteria = {}
64
+
65
+ conditions.each_pair do |key, value|
66
+ key = normalized_key(key)
67
+
68
+ if model.object_id_key?(key) && value.is_a?(String)
69
+ value = Mongo::ObjectID.from_string(value)
70
+ end
71
+
72
+ if symbol_operator?(key)
73
+ value = {"$#{key.operator}" => value}
74
+ key = normalized_key(key.field)
75
+ end
76
+
77
+ criteria[key] = normalized_value(key, value)
78
+ end
79
+
80
+ criteria
81
+ end
82
+
83
+ def to_fields(fields)
84
+ return if fields.blank?
85
+
86
+ if fields.respond_to?(:flatten, :compact)
87
+ fields.flatten.compact
88
+ else
89
+ fields.split(',').map { |field| field.strip }
90
+ end
91
+ end
92
+
93
+ def to_order(field, direction=nil)
94
+ direction ||= 'ASC'
95
+ direction = direction.upcase == 'ASC' ? 1 : -1
96
+ [field.to_s, direction]
97
+ end
98
+
99
+ def normalized_key(field)
100
+ field.to_s == 'id' ? :_id : field
101
+ end
102
+
103
+ def normalized_value(field, value)
104
+ case value
105
+ when Array
106
+ modifier?(field) ? value : {'$in' => value}
107
+ when Hash
108
+ to_criteria(value, field)
109
+ when Time
110
+ value.utc
111
+ else
112
+ value
113
+ end
114
+ end
115
+
116
+ def normalized_sort(sort)
117
+ return if sort.blank?
118
+
119
+ if sort.respond_to?(:all?) && sort.all? { |s| symbol_operator?(s) }
120
+ sort.map { |s| to_order(s.field, s.operator) }
121
+ elsif symbol_operator?(sort)
122
+ [to_order(sort.field, sort.operator)]
123
+ else
124
+ sort.split(',').map do |str|
125
+ to_order(*str.strip.split(' '))
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
@@ -164,21 +164,15 @@ class String
164
164
  end
165
165
 
166
166
  class SymbolOperator
167
+ attr_reader :field, :operator
168
+
167
169
  def initialize(field, operator, options={})
168
170
  @field, @operator = field, operator
169
171
  end unless method_defined?(:initialize)
170
-
171
- def to_mm_criteria(value)
172
- {MongoMapper::FinderOptions.normalized_field(@field) => {"$#{@operator}" => value}}
173
- end
174
-
175
- def to_mm_order
176
- [@field.to_s, MongoMapper::FinderOptions.normalized_order_direction(@operator)]
177
- end
178
172
  end
179
173
 
180
174
  class Symbol
181
- %w(gt lt gte lte ne in nin mod size where exists asc desc).each do |operator|
175
+ %w(gt lt gte lte ne in nin mod all size where exists asc desc).each do |operator|
182
176
  define_method(operator) do
183
177
  SymbolOperator.new(self, operator)
184
178
  end unless method_defined?(operator)
@@ -213,5 +207,9 @@ class Mongo::ObjectID
213
207
  end
214
208
  end
215
209
 
216
- require 'mongo_mapper/support/descendant_appends'
217
- require 'mongo_mapper/support/find'
210
+ module MongoMapper
211
+ module Support
212
+ autoload :DescendantAppends, 'mongo_mapper/support/descendant_appends'
213
+ autoload :Find, 'mongo_mapper/support/find'
214
+ end
215
+ end
data/lib/mongo_mapper.rb CHANGED
@@ -108,11 +108,11 @@ module MongoMapper
108
108
  def self.normalize_object_id(value)
109
109
  value.is_a?(String) ? Mongo::ObjectID.from_string(value) : value
110
110
  end
111
+
112
+ autoload :Query, 'mongo_mapper/query'
113
+ autoload :Document, 'mongo_mapper/document'
114
+ autoload :EmbeddedDocument, 'mongo_mapper/embedded_document'
111
115
  end
112
116
 
113
117
  require 'mongo_mapper/support'
114
- require 'mongo_mapper/finder_options'
115
- require 'mongo_mapper/plugins'
116
-
117
- require 'mongo_mapper/document'
118
- require 'mongo_mapper/embedded_document'
118
+ require 'mongo_mapper/plugins'
data/mongo_mapper.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongo_mapper}
8
- s.version = "0.6.10"
8
+ s.version = "0.7.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["John Nunemaker"]
12
- s.date = %q{2010-01-02}
12
+ s.date = %q{2010-02-10}
13
13
  s.default_executable = %q{mmconsole}
14
14
  s.email = %q{nunemaker@gmail.com}
15
15
  s.executables = ["mmconsole"]
@@ -25,35 +25,44 @@ Gem::Specification.new do |s|
25
25
  "VERSION",
26
26
  "bin/mmconsole",
27
27
  "lib/mongo_mapper.rb",
28
- "lib/mongo_mapper/associations.rb",
29
- "lib/mongo_mapper/associations/base.rb",
30
- "lib/mongo_mapper/associations/belongs_to_polymorphic_proxy.rb",
31
- "lib/mongo_mapper/associations/belongs_to_proxy.rb",
32
- "lib/mongo_mapper/associations/collection.rb",
33
- "lib/mongo_mapper/associations/in_array_proxy.rb",
34
- "lib/mongo_mapper/associations/many_documents_as_proxy.rb",
35
- "lib/mongo_mapper/associations/many_documents_proxy.rb",
36
- "lib/mongo_mapper/associations/many_embedded_polymorphic_proxy.rb",
37
- "lib/mongo_mapper/associations/many_embedded_proxy.rb",
38
- "lib/mongo_mapper/associations/many_polymorphic_proxy.rb",
39
- "lib/mongo_mapper/associations/one_proxy.rb",
40
- "lib/mongo_mapper/associations/proxy.rb",
41
- "lib/mongo_mapper/callbacks.rb",
42
- "lib/mongo_mapper/dirty.rb",
43
28
  "lib/mongo_mapper/document.rb",
44
- "lib/mongo_mapper/dynamic_finder.rb",
45
29
  "lib/mongo_mapper/embedded_document.rb",
46
- "lib/mongo_mapper/finder_options.rb",
47
- "lib/mongo_mapper/key.rb",
48
- "lib/mongo_mapper/mongo_mapper.rb",
49
- "lib/mongo_mapper/pagination.rb",
50
- "lib/mongo_mapper/rails_compatibility/document.rb",
51
- "lib/mongo_mapper/rails_compatibility/embedded_document.rb",
52
- "lib/mongo_mapper/serialization.rb",
53
- "lib/mongo_mapper/serializers/json_serializer.rb",
30
+ "lib/mongo_mapper/plugins.rb",
31
+ "lib/mongo_mapper/plugins/associations.rb",
32
+ "lib/mongo_mapper/plugins/associations/base.rb",
33
+ "lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb",
34
+ "lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb",
35
+ "lib/mongo_mapper/plugins/associations/collection.rb",
36
+ "lib/mongo_mapper/plugins/associations/embedded_collection.rb",
37
+ "lib/mongo_mapper/plugins/associations/in_array_proxy.rb",
38
+ "lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb",
39
+ "lib/mongo_mapper/plugins/associations/many_documents_proxy.rb",
40
+ "lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb",
41
+ "lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb",
42
+ "lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb",
43
+ "lib/mongo_mapper/plugins/associations/one_proxy.rb",
44
+ "lib/mongo_mapper/plugins/associations/proxy.rb",
45
+ "lib/mongo_mapper/plugins/callbacks.rb",
46
+ "lib/mongo_mapper/plugins/clone.rb",
47
+ "lib/mongo_mapper/plugins/descendants.rb",
48
+ "lib/mongo_mapper/plugins/dirty.rb",
49
+ "lib/mongo_mapper/plugins/equality.rb",
50
+ "lib/mongo_mapper/plugins/identity_map.rb",
51
+ "lib/mongo_mapper/plugins/inspect.rb",
52
+ "lib/mongo_mapper/plugins/keys.rb",
53
+ "lib/mongo_mapper/plugins/logger.rb",
54
+ "lib/mongo_mapper/plugins/pagination.rb",
55
+ "lib/mongo_mapper/plugins/pagination/proxy.rb",
56
+ "lib/mongo_mapper/plugins/protected.rb",
57
+ "lib/mongo_mapper/plugins/rails.rb",
58
+ "lib/mongo_mapper/plugins/serialization.rb",
59
+ "lib/mongo_mapper/plugins/validations.rb",
60
+ "lib/mongo_mapper/query.rb",
54
61
  "lib/mongo_mapper/support.rb",
55
- "lib/mongo_mapper/validations.rb",
62
+ "lib/mongo_mapper/support/descendant_appends.rb",
63
+ "lib/mongo_mapper/support/find.rb",
56
64
  "mongo_mapper.gemspec",
65
+ "performance/read_write.rb",
57
66
  "specs.watchr",
58
67
  "test/NOTE_ON_TESTING",
59
68
  "test/functional/associations/test_belongs_to_polymorphic_proxy.rb",
@@ -71,10 +80,11 @@ Gem::Specification.new do |s|
71
80
  "test/functional/test_dirty.rb",
72
81
  "test/functional/test_document.rb",
73
82
  "test/functional/test_embedded_document.rb",
83
+ "test/functional/test_identity_map.rb",
74
84
  "test/functional/test_logger.rb",
75
85
  "test/functional/test_modifiers.rb",
76
86
  "test/functional/test_pagination.rb",
77
- "test/functional/test_rails_compatibility.rb",
87
+ "test/functional/test_protected.rb",
78
88
  "test/functional/test_string_id_compatibility.rb",
79
89
  "test/functional/test_validations.rb",
80
90
  "test/models.rb",
@@ -84,15 +94,18 @@ Gem::Specification.new do |s|
84
94
  "test/unit/associations/test_base.rb",
85
95
  "test/unit/associations/test_proxy.rb",
86
96
  "test/unit/serializers/test_json_serializer.rb",
97
+ "test/unit/test_descendant_appends.rb",
87
98
  "test/unit/test_document.rb",
88
99
  "test/unit/test_dynamic_finder.rb",
89
100
  "test/unit/test_embedded_document.rb",
90
- "test/unit/test_finder_options.rb",
91
- "test/unit/test_key.rb",
101
+ "test/unit/test_keys.rb",
92
102
  "test/unit/test_mongo_mapper.rb",
93
103
  "test/unit/test_pagination.rb",
104
+ "test/unit/test_plugins.rb",
105
+ "test/unit/test_query.rb",
106
+ "test/unit/test_rails.rb",
94
107
  "test/unit/test_rails_compatibility.rb",
95
- "test/unit/test_serializations.rb",
108
+ "test/unit/test_serialization.rb",
96
109
  "test/unit/test_support.rb",
97
110
  "test/unit/test_time_zones.rb",
98
111
  "test/unit/test_validations.rb"
@@ -118,10 +131,11 @@ Gem::Specification.new do |s|
118
131
  "test/functional/test_dirty.rb",
119
132
  "test/functional/test_document.rb",
120
133
  "test/functional/test_embedded_document.rb",
134
+ "test/functional/test_identity_map.rb",
121
135
  "test/functional/test_logger.rb",
122
136
  "test/functional/test_modifiers.rb",
123
137
  "test/functional/test_pagination.rb",
124
- "test/functional/test_rails_compatibility.rb",
138
+ "test/functional/test_protected.rb",
125
139
  "test/functional/test_string_id_compatibility.rb",
126
140
  "test/functional/test_validations.rb",
127
141
  "test/models.rb",
@@ -131,15 +145,18 @@ Gem::Specification.new do |s|
131
145
  "test/unit/associations/test_base.rb",
132
146
  "test/unit/associations/test_proxy.rb",
133
147
  "test/unit/serializers/test_json_serializer.rb",
148
+ "test/unit/test_descendant_appends.rb",
134
149
  "test/unit/test_document.rb",
135
150
  "test/unit/test_dynamic_finder.rb",
136
151
  "test/unit/test_embedded_document.rb",
137
- "test/unit/test_finder_options.rb",
138
- "test/unit/test_key.rb",
152
+ "test/unit/test_keys.rb",
139
153
  "test/unit/test_mongo_mapper.rb",
140
154
  "test/unit/test_pagination.rb",
155
+ "test/unit/test_plugins.rb",
156
+ "test/unit/test_query.rb",
157
+ "test/unit/test_rails.rb",
141
158
  "test/unit/test_rails_compatibility.rb",
142
- "test/unit/test_serializations.rb",
159
+ "test/unit/test_serialization.rb",
143
160
  "test/unit/test_support.rb",
144
161
  "test/unit/test_time_zones.rb",
145
162
  "test/unit/test_validations.rb"
@@ -151,7 +168,7 @@ Gem::Specification.new do |s|
151
168
 
152
169
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
153
170
  s.add_runtime_dependency(%q<activesupport>, [">= 2.3"])
154
- s.add_runtime_dependency(%q<mongo>, ["= 0.18.2"])
171
+ s.add_runtime_dependency(%q<mongo>, ["= 0.18.3"])
155
172
  s.add_runtime_dependency(%q<jnunemaker-validatable>, ["= 1.8.1"])
156
173
  s.add_development_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
157
174
  s.add_development_dependency(%q<shoulda>, ["= 2.10.2"])
@@ -159,7 +176,7 @@ Gem::Specification.new do |s|
159
176
  s.add_development_dependency(%q<mocha>, ["= 0.9.8"])
160
177
  else
161
178
  s.add_dependency(%q<activesupport>, [">= 2.3"])
162
- s.add_dependency(%q<mongo>, ["= 0.18.2"])
179
+ s.add_dependency(%q<mongo>, ["= 0.18.3"])
163
180
  s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.1"])
164
181
  s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
165
182
  s.add_dependency(%q<shoulda>, ["= 2.10.2"])
@@ -168,7 +185,7 @@ Gem::Specification.new do |s|
168
185
  end
169
186
  else
170
187
  s.add_dependency(%q<activesupport>, [">= 2.3"])
171
- s.add_dependency(%q<mongo>, ["= 0.18.2"])
188
+ s.add_dependency(%q<mongo>, ["= 0.18.3"])
172
189
  s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.1"])
173
190
  s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
174
191
  s.add_dependency(%q<shoulda>, ["= 2.10.2"])