Class: Perpetuity::MongoDB
- Inherits:
-
Object
- Object
- Perpetuity::MongoDB
show all
- Defined in:
- lib/perpetuity/mongodb.rb,
lib/perpetuity/mongodb/index.rb,
lib/perpetuity/mongodb/query.rb,
lib/perpetuity/mongodb/query_union.rb,
lib/perpetuity/mongodb/query_attribute.rb,
lib/perpetuity/mongodb/query_expression.rb,
lib/perpetuity/mongodb/query_intersection.rb
Defined Under Namespace
Classes: Index, Query, QueryAttribute, QueryExpression, QueryIntersection, QueryUnion
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(options) ⇒ MongoDB
Returns a new instance of MongoDB.
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/perpetuity/mongodb.rb', line 9
def initialize options
@host = options.fetch(:host, 'localhost')
@port = options.fetch(:port, 27017)
@db = options.fetch(:db)
@pool_size = options.fetch(:pool_size, 5)
@username = options[:username]
@password = options[:password]
@connection = nil
@indexes = Hash.new { |hash, key| hash[key] = active_indexes(key) }
end
|
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
7
8
9
|
# File 'lib/perpetuity/mongodb.rb', line 7
def connection
@connection
end
|
#db ⇒ Object
Returns the value of attribute db.
7
8
9
|
# File 'lib/perpetuity/mongodb.rb', line 7
def db
@db
end
|
#host ⇒ Object
Returns the value of attribute host.
7
8
9
|
# File 'lib/perpetuity/mongodb.rb', line 7
def host
@host
end
|
#password ⇒ Object
Returns the value of attribute password.
7
8
9
|
# File 'lib/perpetuity/mongodb.rb', line 7
def password
@password
end
|
#pool_size ⇒ Object
Returns the value of attribute pool_size.
7
8
9
|
# File 'lib/perpetuity/mongodb.rb', line 7
def pool_size
@pool_size
end
|
#port ⇒ Object
Returns the value of attribute port.
7
8
9
|
# File 'lib/perpetuity/mongodb.rb', line 7
def port
@port
end
|
#username ⇒ Object
Returns the value of attribute username.
7
8
9
|
# File 'lib/perpetuity/mongodb.rb', line 7
def username
@username
end
|
Instance Method Details
#activate_index!(index) ⇒ Object
138
139
140
141
142
143
144
145
|
# File 'lib/perpetuity/mongodb.rb', line 138
def activate_index! index
attribute = index.attribute.to_s
order = index.order == :ascending ? 1 : -1
unique = index.unique?
collection(index.collection).create_index [[attribute, order]], unique: unique
index.activate!
end
|
#active_indexes(klass) ⇒ Object
128
129
130
131
132
133
134
135
136
|
# File 'lib/perpetuity/mongodb.rb', line 128
def active_indexes klass
indexes = collection(klass).index_information
indexes.map do |name, index|
key = index['key'].keys.first
direction = index['key'][key]
unique = index['unique']
Index.new(klass, key, order: Index::KEY_ORDERS[direction], unique: unique)
end.to_set
end
|
#all(klass) ⇒ Object
94
95
96
|
# File 'lib/perpetuity/mongodb.rb', line 94
def all klass
retrieve klass, {}, {}
end
|
#can_serialize?(value) ⇒ Boolean
108
109
110
|
# File 'lib/perpetuity/mongodb.rb', line 108
def can_serialize? value
serializable_types.include? value.class
end
|
#collection(klass) ⇒ Object
34
35
36
|
# File 'lib/perpetuity/mongodb.rb', line 34
def collection klass
database.collection(klass.to_s)
end
|
#connect ⇒ Object
20
21
22
23
|
# File 'lib/perpetuity/mongodb.rb', line 20
def connect
database.authenticate(@username, @password) if @username and @password
@connection ||= Mongo::MongoClient.new @host, @port, pool_size: @pool_size
end
|
#connected? ⇒ Boolean
25
26
27
|
# File 'lib/perpetuity/mongodb.rb', line 25
def connected?
!!@connection
end
|
#count(klass) ⇒ Object
47
48
49
|
# File 'lib/perpetuity/mongodb.rb', line 47
def count klass
collection(klass).count
end
|
#database ⇒ Object
29
30
31
32
|
# File 'lib/perpetuity/mongodb.rb', line 29
def database
connect unless connected?
@connection.db(@db)
end
|
#delete(object, klass = nil) ⇒ Object
98
99
100
101
102
|
# File 'lib/perpetuity/mongodb.rb', line 98
def delete object, klass=nil
id = object.respond_to?(:id) ? object.id : object
klass ||= object.class
collection(klass.to_s).remove "_id" => id
end
|
#delete_all(klass) ⇒ Object
51
52
53
|
# File 'lib/perpetuity/mongodb.rb', line 51
def delete_all klass
database.collection(klass.to_s).remove
end
|
#drop_collection(to_be_dropped) ⇒ Object
112
113
114
|
# File 'lib/perpetuity/mongodb.rb', line 112
def drop_collection to_be_dropped
collection(to_be_dropped).drop
end
|
#first(klass) ⇒ Object
55
56
57
58
59
60
|
# File 'lib/perpetuity/mongodb.rb', line 55
def first klass
document = database.collection(klass.to_s).find_one
document[:id] = document.delete("_id")
document
end
|
#index(klass, attribute, options = {}) ⇒ Object
116
117
118
119
120
121
122
|
# File 'lib/perpetuity/mongodb.rb', line 116
def index klass, attribute, options={}
@indexes[klass] ||= Set.new
index = Index.new(klass, attribute, options)
@indexes[klass] << index
index
end
|
#indexes(klass) ⇒ Object
124
125
126
|
# File 'lib/perpetuity/mongodb.rb', line 124
def indexes klass
@indexes[klass]
end
|
#insert(klass, attributes) ⇒ Object
38
39
40
41
42
43
44
45
|
# File 'lib/perpetuity/mongodb.rb', line 38
def insert klass, attributes
if attributes.has_key? :id
attributes[:_id] = attributes[:id]
attributes.delete :id
end
collection(klass).insert attributes
end
|
#remove_index(index) ⇒ Object
147
148
149
150
151
152
153
154
155
|
# File 'lib/perpetuity/mongodb.rb', line 147
def remove_index index
coll = collection(index.collection)
db_indexes = coll.index_information.select do |name, info|
name =~ /#{index.attribute}/
end
if db_indexes.any?
collection(index.collection).drop_index db_indexes.first.first
end
end
|
#retrieve(klass, criteria, options = {}) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/perpetuity/mongodb.rb', line 62
def retrieve klass, criteria, options = {}
if criteria.has_key?(:id)
if criteria[:id].is_a? String
criteria = { _id: (BSON::ObjectId.from_string(criteria[:id].to_s) rescue criteria[:id]) }
else
criteria[:_id] = criteria.delete(:id)
end
end
other_options = { limit: options[:limit] }
if options[:page]
other_options = other_options.merge skip: (options[:page] - 1) * options[:limit]
end
cursor = database.collection(klass.to_s).find(criteria, other_options)
sort_cursor(cursor, options).map do |document|
document[:id] = document.delete("_id")
document
end
end
|
#sort_cursor(cursor, options) ⇒ Object
84
85
86
87
88
89
90
91
92
|
# File 'lib/perpetuity/mongodb.rb', line 84
def sort_cursor cursor, options
return cursor unless options.has_key?(:attribute) &&
options.has_key?(:direction)
sort_field = options[:attribute]
sort_direction = options[:direction]
sort_criteria = [[sort_field, sort_direction]]
cursor.sort(sort_criteria)
end
|
#update(klass, id, new_data) ⇒ Object
104
105
106
|
# File 'lib/perpetuity/mongodb.rb', line 104
def update klass, id, new_data
collection(klass).update({ _id: id }, new_data)
end
|