mongo_odm 0.2.11 → 0.2.13

Sign up to get free protection for your applications and to get access to all the features.
data/CONTRIBUTORS CHANGED
@@ -1,5 +1,15 @@
1
- Steve Sloan
1
+ Steve Sloan (CodeMonkeySteve)
2
2
 
3
3
  * Config class, railtie to load config from YAML file
4
4
  * Timestamps module
5
- * BSON::DBRef ideas
5
+ * BSON::DBRef ideas
6
+
7
+ Dusty Doris (dusty)
8
+
9
+ * Bug fix
10
+ * before_validate callback
11
+
12
+ Nikita Baksalyar (nbaksalyar)
13
+
14
+ * Bug fixes
15
+ * orm_adapter compatibility
@@ -0,0 +1,14 @@
1
+ class Hash
2
+ def rmerge!(other_hash)
3
+ merge!(other_hash) do |key, old_value, new_value|
4
+ old_value.class == self.class ? old_value.rmerge!(new_value) : new_value
5
+ end
6
+ end
7
+
8
+ def rmerge(other_hash)
9
+ result = {}
10
+ merge(other_hash) do |key, old_value, new_value|
11
+ result[key] = old_value.class == self.class ? old_value.rmerge(new_value) : new_value
12
+ end
13
+ end
14
+ end
@@ -1,65 +1,56 @@
1
1
  # encoding: utf-8
2
+ require 'mongo_odm/core_ext/hash_recursive_merge'
3
+
2
4
  module MongoODM
3
5
 
4
6
  class Criteria
5
7
 
6
8
  delegate :inspect, :to_xml, :to_yaml, :to_json, :include?, :to => :to_a
7
9
 
8
- def initialize(klass, options = {})
9
- @klass = klass
10
- @selector = options[:selector] || {}
11
- @opts = options[:opts] || {}
12
- @sort = options[:sort] || []
13
- @limit = options[:limit] || nil
14
- @skip = options[:skip] || nil
10
+ def initialize(klass, selector = {}, opts = {})
11
+ @_klass = klass
12
+ @_selector = selector
13
+ @_opts = opts
15
14
  _set_cursor
16
15
  end
16
+ attr_reader :_klass, :_selector, :_opts
17
17
 
18
18
  def ==(other)
19
19
  case other
20
20
  when Criteria
21
- other.instance_variable_get(:@selector) == @selector &&
22
- other.instance_variable_get(:@opts) == @opts &&
23
- other.instance_variable_get(:@sort) == @sort &&
24
- other.instance_variable_get(:@limit) == @limit &&
25
- other.instance_variable_get(:@skip) == @skip
21
+ other._klass == @_klass &&
22
+ other._selector == @_selector &&
23
+ other._opts == @_opts
26
24
  else
27
25
  to_a == other.to_a
28
26
  end
29
27
  end
30
28
 
31
29
  def to_a
32
- @cursor.to_a
30
+ @_result ||= @_cursor.rewind! && @_cursor.to_a
33
31
  end
34
32
 
35
33
  def to_cursor
36
- @cursor
34
+ @_cursor
37
35
  end
38
36
 
39
37
  def _set_cursor
40
- @cursor = @klass.collection.find(@selector, @opts)
41
- @cursor = @cursor.sort(@sort) unless @sort.blank?
42
- @cursor = @cursor.limit(@limit) unless @limit.blank?
43
- @cursor = @cursor.skip(@skip) unless @skip.blank?
44
- @cursor
38
+ @_cursor ||= @_klass.collection.find(@_selector, @_opts.dup)
45
39
  end
46
40
 
47
41
  def _merge_criteria(criteria)
48
- @selector.merge!(criteria.instance_variable_get(:@selector))
49
- @opts.merge!(criteria.instance_variable_get(:@opts))
50
- @sort << criteria.instance_variable_get(:@sort) unless criteria.instance_variable_get(:@sort).blank?
51
- @limit = criteria.instance_variable_get(:@limit) unless criteria.instance_variable_get(:@limit).blank?
52
- @skip = criteria.instance_variable_get(:@skip) unless criteria.instance_variable_get(:@skip).blank?
42
+ @_selector.rmerge!(criteria._selector)
43
+ @_opts.rmerge!(criteria._opts)
53
44
  _set_cursor
54
45
  self
55
46
  end
56
47
 
57
48
  def method_missing(method_name, *args, &block)
58
- if @klass.respond_to?(method_name)
59
- result = @klass.send(method_name, *args, &block)
49
+ if @_klass.respond_to?(method_name)
50
+ result = @_klass.send(method_name, *args, &block)
60
51
  result.is_a?(Criteria) ? _merge_criteria(result) : result
61
- elsif @cursor.respond_to?(method_name)
62
- @cursor.send(method_name, *args, &block)
52
+ elsif @_cursor.respond_to?(method_name)
53
+ @_cursor.send(method_name, *args, &block)
63
54
  elsif [].respond_to?(method_name)
64
55
  to_a.send(method_name, *args, &block)
65
56
  else
@@ -90,25 +90,25 @@ module MongoODM
90
90
 
91
91
  def find(selector = {}, opts = {})
92
92
  if opts.blank? && selector.is_a?(String)
93
- MongoODM::Criteria.new(self, :selector => {:_id => BSON::ObjectId.from_string(selector)})
93
+ MongoODM::Criteria.new(self, {:_id => BSON::ObjectId.from_string(selector)})
94
94
  elsif opts.blank? && selector.is_a?(BSON::ObjectId)
95
- MongoODM::Criteria.new(self, :selector => {:_id => selector})
95
+ MongoODM::Criteria.new(self, {:_id => selector})
96
96
  else
97
- MongoODM::Criteria.new(self, :selector => selector, :opts => opts)
97
+ MongoODM::Criteria.new(self, selector, opts)
98
98
  end
99
99
  end
100
100
 
101
101
  def sort(key_or_list, direction = nil)
102
102
  order = key_or_list.is_a?(Array) ? key_or_list : direction.nil? ? [key_or_list, :asc] : [key_or_list, direction]
103
- MongoODM::Criteria.new(self, :sort => order)
103
+ MongoODM::Criteria.new(self, {}, {:sort => order})
104
104
  end
105
105
 
106
106
  def skip(number_to_skip = nil)
107
- MongoODM::Criteria.new(self, :skip => number_to_skip)
107
+ MongoODM::Criteria.new(self, {}, {:skip => number_to_skip})
108
108
  end
109
109
 
110
110
  def limit(number_to_return = nil)
111
- MongoODM::Criteria.new(self, :limit => number_to_return)
111
+ MongoODM::Criteria.new(self, {}, {:limit => number_to_return})
112
112
  end
113
113
 
114
114
  def cursor
@@ -2,6 +2,6 @@
2
2
  module MongoODM
3
3
  VERSION_MAJOR = "0"
4
4
  VERSION_MINOR = "2"
5
- VERSION_BUILD = "11"
5
+ VERSION_BUILD = "13"
6
6
  VERSION = "#{VERSION_MAJOR}.#{VERSION_MINOR}.#{VERSION_BUILD}"
7
7
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mongo_odm
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.11
5
+ version: 0.2.13
6
6
  platform: ruby
7
7
  authors:
8
8
  - Carlos Paramio
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-14 00:00:00 +01:00
13
+ date: 2011-03-16 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -253,6 +253,7 @@ files:
253
253
  - lib/mongo_odm/collection.rb
254
254
  - lib/mongo_odm/config.rb
255
255
  - lib/mongo_odm/core_ext/conversions.rb
256
+ - lib/mongo_odm/core_ext/hash_recursive_merge.rb
256
257
  - lib/mongo_odm/criteria.rb
257
258
  - lib/mongo_odm/cursor.rb
258
259
  - lib/mongo_odm/document.rb