Class: Easymongo::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/easymongo/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, options) ⇒ Query

Set up connection



19
20
21
# File 'lib/easymongo/query.rb', line 19

def initialize(uri, options)
  self.client = Mongo::Client.new(uri, options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Set up collection, stored in the thread



24
25
26
# File 'lib/easymongo/query.rb', line 24

def method_missing(name, *args, &block)
  c!; s[:coll] = name; self
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



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

def client
  @client
end

Instance Method Details

#all(d = {}) ⇒ Object

Get all



85
86
87
# File 'lib/easymongo/query.rb', line 85

def all(d = {})
  g!(d); cursor.to_a.map{|r| ed(r)}.tap{ c!}
end

#count(d = {}) ⇒ Object

Count



90
91
92
# File 'lib/easymongo/query.rb', line 90

def count(d = {})
  g!(d); cursor.count.tap{ c!}
end

#fields(data, d = {}) ⇒ Object

Fields



65
66
67
# File 'lib/easymongo/query.rb', line 65

def fields(data, d = {})
  g!(d); s[:cursor] = cursor.projection(data); self
end

#first(d = {}) ⇒ Object

Get first



75
76
77
# File 'lib/easymongo/query.rb', line 75

def first(d = {})
  g!(d); cursor.first.tap{|r| return ed(r) if r; c!}
end

#get(data = {}) ⇒ Object

Get values, store cursor in thread



50
51
52
# File 'lib/easymongo/query.rb', line 50

def get(data = {})
  s[:cursor] = client[coll].find(ids(data)); self
end

#ids(data) ⇒ Object

Make sure data is optimal



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/easymongo/query.rb', line 108

def ids(data)

  # Just return if nothing to do
  return data if data and data.empty?

  # Support passing id as string
  data = {'_id' => data} if !data or data.is_a?(String)

  # Turn all keys to string
  data = data.stringify_keys

  # Convert id to _id for mongo
  data['_id'] = data.delete('id') if data['id']

  # Convert ids to BSON ObjectId
  data.each do |k, v|
    if v.is_a?(String) and v =~ /^[0-9a-fA-F]{24}$/
      data[k] = oid(v)
    end
  end

  # Return data
  data
end

#last(d = {}) ⇒ Object

Get last



80
81
82
# File 'lib/easymongo/query.rb', line 80

def last(d = {})
  g!(d); cursor.sort(:$natural => -1).first.tap{|r| return ed(r) if r; c!}
end

#limit(n, d = {}) ⇒ Object

Limit



55
56
57
# File 'lib/easymongo/query.rb', line 55

def limit(n, d = {})
  g!(d); s[:cursor] = cursor.limit(n.to_i); self
end

#oid(v = nil) ⇒ Object

Convert to BSON ObjectId or make a new one



134
135
136
# File 'lib/easymongo/query.rb', line 134

def oid(v = nil)
  return BSON::ObjectId.new if v.nil?; BSON::ObjectId.from_string(v) rescue v
end

#rm(data) ⇒ Object

Remove



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/easymongo/query.rb', line 95

def rm(data)

  # Optimize data
  data = ids(data)

  # Delete doc
  result = client[coll].delete_one(data)

  # Return result
  Easymongo::Result.new(result).tap{ c!}
end

#set(*args) ⇒ Object

Set values



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/easymongo/query.rb', line 29

def set(*args)

  # Insert, add oid
  data, values = args.size == 1 ? [{'_id' => oid}, *args] : args

  # Normalize attributes
  data, values = ids(data), ids(values)

  # Using set and unset so we don't store nil in the db
  options = {
    :$set => values.select{|k, v| !v.nil?}, :$unset => values.select{|k, v| v.nil?}
  }.delete_if{|k, v| v.empty?}

  # Update the collection
  result = client[coll].update_one(data, options, :upsert => true)

  # Return result
  Easymongo::Result.new(result)
end

#skip(n, d = {}) ⇒ Object

Skip



60
61
62
# File 'lib/easymongo/query.rb', line 60

def skip(n, d = {})
  g!(d); s[:cursor] = cursor.skip(n.to_i); self
end

#sort(data, d = {}) ⇒ Object

Sort



70
71
72
# File 'lib/easymongo/query.rb', line 70

def sort(data, d = {})
  g!(d); s[:cursor] = cursor.sort(data); self
end