Class: Plucky::Query

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/plucky/query.rb

Constant Summary collapse

OptionKeys =
[
  :select, :offset, :order,                                              # MM
  :fields, :skip, :limit, :sort, :hint, :snapshot, :batch_size, :timeout # Ruby Driver
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, opts = {}) ⇒ Query

Returns a new instance of Query.



16
17
18
19
# File 'lib/plucky/query.rb', line 16

def initialize(collection, opts={})
  @collection, @options, @criteria = collection, OptionsHash.new, CriteriaHash.new
  opts.each { |key, value| self[key] = value }
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



12
13
14
# File 'lib/plucky/query.rb', line 12

def collection
  @collection
end

#criteriaObject (readonly)

Returns the value of attribute criteria.



12
13
14
# File 'lib/plucky/query.rb', line 12

def criteria
  @criteria
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/plucky/query.rb', line 12

def options
  @options
end

Instance Method Details

#[](key) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/plucky/query.rb', line 96

def [](key)
  key = key.to_sym if key.respond_to?(:to_sym)
  if OptionKeys.include?(key)
    @options[key]
  else
    @criteria[key]
  end
end

#[]=(key, value) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/plucky/query.rb', line 105

def []=(key, value)
  key = key.to_sym if key.respond_to?(:to_sym)
  if OptionKeys.include?(key)
    @options[key] = value
  else
    @criteria[key] = value
  end
end

#all(opts = {}) ⇒ Object



41
42
43
# File 'lib/plucky/query.rb', line 41

def all(opts={})
  update(opts).find(to_hash).to_a
end

#count(opts = {}) ⇒ Object



57
58
59
# File 'lib/plucky/query.rb', line 57

def count(opts={})
  update(opts).find(to_hash).count
end

#fields(*args) ⇒ Object



66
67
68
69
# File 'lib/plucky/query.rb', line 66

def fields(*args)
  self[:fields] = args
  self
end

#find(opts = {}) ⇒ Object



33
34
35
# File 'lib/plucky/query.rb', line 33

def find(opts={})
  update(opts).collection.find(criteria.to_hash, options.to_hash)
end

#find_one(opts = {}) ⇒ Object



37
38
39
# File 'lib/plucky/query.rb', line 37

def find_one(opts={})
  update(opts).collection.find_one(criteria.to_hash, options.to_hash)
end

#first(opts = {}) ⇒ Object



45
46
47
# File 'lib/plucky/query.rb', line 45

def first(opts={})
  update(opts).find_one(to_hash)
end

#initialize_copy(source) ⇒ Object



21
22
23
24
25
# File 'lib/plucky/query.rb', line 21

def initialize_copy(source)
  super
  @criteria = @criteria.dup
  @options  = @options.dup
end

#inspectObject



123
124
125
126
127
128
# File 'lib/plucky/query.rb', line 123

def inspect
  as_nice_string = to_hash.collect do |key, value|
    " #{key}: #{value.inspect}"
  end.sort.join(",")
  "#<#{self.class}#{as_nice_string}>"
end

#last(opts = {}) ⇒ Object



49
50
51
# File 'lib/plucky/query.rb', line 49

def last(opts={})
  update(opts).reverse.find_one(to_hash)
end

#limit(count = nil) ⇒ Object



71
72
73
74
# File 'lib/plucky/query.rb', line 71

def limit(count=nil)
  self[:limit] = count
  self
end

#merge(other) ⇒ Object



114
115
116
117
# File 'lib/plucky/query.rb', line 114

def merge(other)
  merged = criteria.merge(other.criteria).to_hash.merge(options.to_hash.merge(other.options.to_hash))
  clone.update(merged)
end

#object_ids(*keys) ⇒ Object



27
28
29
30
31
# File 'lib/plucky/query.rb', line 27

def object_ids(*keys)
  return criteria.object_ids if keys.empty?
  criteria.object_ids = *keys
  self
end

#remove(opts = {}) ⇒ Object



53
54
55
# File 'lib/plucky/query.rb', line 53

def remove(opts={})
  update(opts).collection.remove(criteria.to_hash)
end

#reverseObject



76
77
78
79
# File 'lib/plucky/query.rb', line 76

def reverse
  self[:sort].map! { |s| [s[0], -s[1]] } unless self[:sort].nil?
  self
end

#skip(count = nil) ⇒ Object



81
82
83
84
# File 'lib/plucky/query.rb', line 81

def skip(count=nil)
  self[:skip] = count
  self
end

#sort(*args) ⇒ Object



86
87
88
89
# File 'lib/plucky/query.rb', line 86

def sort(*args)
  self[:sort] = *args
  self
end

#to_hashObject



119
120
121
# File 'lib/plucky/query.rb', line 119

def to_hash
  criteria.to_hash.merge(options.to_hash)
end

#update(opts = {}) ⇒ Object



61
62
63
64
# File 'lib/plucky/query.rb', line 61

def update(opts={})
  opts.each { |key, value| self[key] = value }
  self
end

#where(hash = {}) ⇒ Object



91
92
93
94
# File 'lib/plucky/query.rb', line 91

def where(hash={})
  criteria.merge(CriteriaHash.new(hash)).to_hash.each { |key, value| self[key] = value }
  self
end