plucky 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/plucky/criteria_hash.rb +15 -1
- data/lib/plucky/options_hash.rb +9 -0
- data/lib/plucky/query.rb +21 -5
- data/lib/plucky/version.rb +1 -1
- metadata +3 -3
data/lib/plucky/criteria_hash.rb
CHANGED
@@ -1,13 +1,19 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
module Plucky
|
3
3
|
class CriteriaHash
|
4
|
-
attr_reader :source
|
4
|
+
attr_reader :source, :options
|
5
5
|
|
6
6
|
def initialize(hash={}, options={})
|
7
7
|
@source, @options = {}, options
|
8
8
|
hash.each { |key, value| self[key] = value }
|
9
9
|
end
|
10
10
|
|
11
|
+
def initialize_copy(source)
|
12
|
+
super
|
13
|
+
@options = @options.dup
|
14
|
+
@source = @source.dup
|
15
|
+
end
|
16
|
+
|
11
17
|
def []=(key, value)
|
12
18
|
normalized_key = normalized_key(key)
|
13
19
|
if key.is_a?(SymbolOperator)
|
@@ -77,6 +83,14 @@ module Plucky
|
|
77
83
|
@options[:object_ids] = value.flatten
|
78
84
|
end
|
79
85
|
|
86
|
+
# The definition of simple is querying by only _id or _id and _type.
|
87
|
+
# If this is the case, you can use IdentityMap in library to not perform
|
88
|
+
# query and instead just return from map.
|
89
|
+
def simple?
|
90
|
+
key_set = keys.to_set
|
91
|
+
key_set == [:_id].to_set || key_set == [:_id, :_type].to_set
|
92
|
+
end
|
93
|
+
|
80
94
|
private
|
81
95
|
def method_missing(method, *args, &block)
|
82
96
|
@source.send(method, *args, &block)
|
data/lib/plucky/options_hash.rb
CHANGED
@@ -8,6 +8,11 @@ module Plucky
|
|
8
8
|
hash.each { |key, value| self[key] = value }
|
9
9
|
end
|
10
10
|
|
11
|
+
def initialize_copy(source)
|
12
|
+
super
|
13
|
+
@source = @source.dup
|
14
|
+
end
|
15
|
+
|
11
16
|
def []=(key, value)
|
12
17
|
key = normalized_key(key)
|
13
18
|
source[key] = normalized_value(key, value)
|
@@ -21,6 +26,10 @@ module Plucky
|
|
21
26
|
source
|
22
27
|
end
|
23
28
|
|
29
|
+
def fields?
|
30
|
+
!self[:fields].nil?
|
31
|
+
end
|
32
|
+
|
24
33
|
private
|
25
34
|
def method_missing(method, *args, &block)
|
26
35
|
@source.send(method, *args, &block)
|
data/lib/plucky/query.rb
CHANGED
@@ -1,18 +1,29 @@
|
|
1
1
|
# encoding: UTF-8
|
2
|
+
require 'forwardable'
|
2
3
|
module Plucky
|
3
4
|
class Query
|
5
|
+
extend Forwardable
|
6
|
+
|
4
7
|
OptionKeys = [
|
5
8
|
:select, :offset, :order, # MM
|
6
9
|
:fields, :skip, :limit, :sort, :hint, :snapshot, :batch_size, :timeout # Ruby Driver
|
7
10
|
]
|
8
11
|
|
9
|
-
attr_reader
|
12
|
+
attr_reader :criteria, :options, :collection
|
13
|
+
def_delegator :criteria, :simple?
|
14
|
+
def_delegator :options, :fields?
|
10
15
|
|
11
16
|
def initialize(collection, opts={})
|
12
17
|
@collection, @options, @criteria = collection, OptionsHash.new, CriteriaHash.new
|
13
18
|
opts.each { |key, value| self[key] = value }
|
14
19
|
end
|
15
20
|
|
21
|
+
def initialize_copy(source)
|
22
|
+
super
|
23
|
+
@criteria = @criteria.dup
|
24
|
+
@options = @options.dup
|
25
|
+
end
|
26
|
+
|
16
27
|
def object_ids(*keys)
|
17
28
|
return criteria.object_ids if keys.empty?
|
18
29
|
criteria.object_ids = *keys
|
@@ -28,9 +39,7 @@ module Plucky
|
|
28
39
|
end
|
29
40
|
|
30
41
|
def all(opts={})
|
31
|
-
|
32
|
-
update(opts).find(to_hash).each { |doc| docs << doc }
|
33
|
-
end
|
42
|
+
update(opts).find(to_hash).to_a
|
34
43
|
end
|
35
44
|
|
36
45
|
def first(opts={})
|
@@ -65,7 +74,7 @@ module Plucky
|
|
65
74
|
end
|
66
75
|
|
67
76
|
def reverse
|
68
|
-
self[:sort].map! { |s| [s[0], -s[1]] }
|
77
|
+
self[:sort].map! { |s| [s[0], -s[1]] } unless self[:sort].nil?
|
69
78
|
self
|
70
79
|
end
|
71
80
|
|
@@ -110,5 +119,12 @@ module Plucky
|
|
110
119
|
def to_hash
|
111
120
|
criteria.to_hash.merge(options.to_hash)
|
112
121
|
end
|
122
|
+
|
123
|
+
def inspect
|
124
|
+
as_nice_string = to_hash.collect do |key, value|
|
125
|
+
" #{key}: #{value.inspect}"
|
126
|
+
end.sort.join(",")
|
127
|
+
"#<#{self.class}#{as_nice_string}>"
|
128
|
+
end
|
113
129
|
end
|
114
130
|
end
|
data/lib/plucky/version.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 3
|
9
|
+
version: 0.1.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- John Nunemaker
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-05-
|
17
|
+
date: 2010-05-23 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|