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 +12 -2
- data/lib/mongo_odm/core_ext/hash_recursive_merge.rb +14 -0
- data/lib/mongo_odm/criteria.rb +19 -28
- data/lib/mongo_odm/document/persistence.rb +6 -6
- data/lib/mongo_odm/version.rb +1 -1
- metadata +3 -2
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
|
data/lib/mongo_odm/criteria.rb
CHANGED
@@ -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,
|
9
|
-
@
|
10
|
-
@
|
11
|
-
@
|
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.
|
22
|
-
other.
|
23
|
-
other.
|
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
|
-
@
|
30
|
+
@_result ||= @_cursor.rewind! && @_cursor.to_a
|
33
31
|
end
|
34
32
|
|
35
33
|
def to_cursor
|
36
|
-
@
|
34
|
+
@_cursor
|
37
35
|
end
|
38
36
|
|
39
37
|
def _set_cursor
|
40
|
-
@
|
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
|
-
@
|
49
|
-
@
|
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 @
|
59
|
-
result = @
|
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 @
|
62
|
-
@
|
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,
|
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,
|
95
|
+
MongoODM::Criteria.new(self, {:_id => selector})
|
96
96
|
else
|
97
|
-
MongoODM::Criteria.new(self,
|
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
|
data/lib/mongo_odm/version.rb
CHANGED
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.
|
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-
|
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
|