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



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

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



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

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

Instance Attribute Details

#clientObject

Returns the value of attribute client.



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

def client
  @client
end

Instance Method Details

#all(d = {}) ⇒ Object

Get all



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

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

#count(d = {}) ⇒ Object

Count



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

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

#first(d = {}) ⇒ Object

Get first



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

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



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

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

#ids(data) ⇒ Object

Make sure data is optimal



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/easymongo/query.rb', line 99

def ids(data)

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

  # Support passing id as string
  data = {'_id' => data} if 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{|k, v| data[k] = oid(v) if v.is_a?(String) and v =~ /^[0-9a-fA-F]{24}$/}

  # Return data
  data
end

#last(d = {}) ⇒ Object

Get last



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

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

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

Limit



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

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



121
122
123
# File 'lib/easymongo/query.rb', line 121

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

#rm(data) ⇒ Object

Remove



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/easymongo/query.rb', line 86

def rm(data)

  # Optimize data
  data = ids(data)

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

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

#set(*args) ⇒ Object

Set values



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

def set(*args)

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

  # 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?}

  # Normalize data
  data = ids(data)

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

  # Return result
  Easymongo::Result.new(result, data, values, options)
end

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

Sort



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

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