Class: EmeraldODM::Collection
Instance Attribute Summary
#_document
Class Method Summary
collapse
-
.collection ⇒ Mongo::Collection
-
.collection_name ⇒ Symbol?
-
.db_name ⇒ Symbol?
-
.find(filter, projection: {}, sort: {}, limit: 0) ⇒ Object
-
.is_valid_fields?(query_fields) ⇒ Boolean
-
.update(type, filter, set: {}, unset: {}) ⇒ Object
-
.update_many(filter, set: {}, unset: {}) ⇒ Object
-
.update_one(filter, set: {}, unset: {}) ⇒ Object
-
.validate_common_stages(*stages) ⇒ Object
-
.validate_dollar_stages(*stages) ⇒ Object
-
.validate_fields_from_stages(*stages) ⇒ Object
Instance Method Summary
collapse
array_to_h, #empty?, fields, #initialize, #nil?, #to_h
Class Method Details
.collection ⇒ Mongo::Collection
114
115
116
|
# File 'lib/emerald_odm.rb', line 114
def self.collection
Connector.database(db_name&.to_sym)[collection_name&.to_sym]
end
|
.collection_name ⇒ Symbol?
109
110
111
|
# File 'lib/emerald_odm.rb', line 109
def self.collection_name
nil
end
|
.db_name ⇒ Symbol?
104
105
106
|
# File 'lib/emerald_odm.rb', line 104
def self.db_name
nil
end
|
.find(filter, projection: {}, sort: {}, limit: 0) ⇒ Object
141
142
143
|
# File 'lib/emerald_odm.rb', line 141
def self.find(filter, projection: {}, sort: {}, limit: 0)
new({}).find(filter, projection: projection, sort: sort, limit: limit)
end
|
.is_valid_fields?(query_fields) ⇒ Boolean
118
119
120
|
# File 'lib/emerald_odm.rb', line 118
def self.is_valid_fields?(query_fields)
(query_fields - fields).count == 0
end
|
.update(type, filter, set: {}, unset: {}) ⇒ Object
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/emerald_odm.rb', line 145
def self.update(type, filter, set: {}, unset: {})
validate_fields_from_stages(filter, set, unset)
update = {}
update[:'$set'] = set unless set.empty?
update[:'$unset'] = unset unless unset.empty?
if update.empty?
raise 'Update without set or unset'
elsif filter.empty?
raise 'Update without filter'
end
if type == :one
update_response = collection.update_one(filter, update).to_a
elsif type == :many
update_response = collection.update_many(filter, update).to_a
else
raise 'Invalid type'
end
update_response.first
end
|
.update_many(filter, set: {}, unset: {}) ⇒ Object
172
173
174
|
# File 'lib/emerald_odm.rb', line 172
def self.update_many(filter, set: {}, unset: {})
update(:many, filter, set: set, unset: unset)
end
|
.update_one(filter, set: {}, unset: {}) ⇒ Object
168
169
170
|
# File 'lib/emerald_odm.rb', line 168
def self.update_one(filter, set: {}, unset: {})
update(:one, filter, set: set, unset: unset)
end
|
.validate_common_stages(*stages) ⇒ Object
189
190
191
192
193
194
195
|
# File 'lib/emerald_odm.rb', line 189
def self.validate_common_stages(*stages)
formatted_stages = stages.map { |stage| stage.keys }.flatten.uniq.map{|k| k.to_s}.reject{|k| k.start_with?('$')}
formatted_stages = formatted_stages.map{|f| f.to_s.split('.').first}.uniq
unless is_valid_fields?(formatted_stages)
raise "Invalid fields: #{(formatted_stages - self.fields)}"
end
end
|
.validate_dollar_stages(*stages) ⇒ Object
181
182
183
184
185
186
187
|
# File 'lib/emerald_odm.rb', line 181
def self.validate_dollar_stages(*stages)
known_dollar_stages = %w[$and $or $in]
formatted_stages = stages.map { |stage| stage.keys }.flatten.uniq.map{|k| k.to_s}.select{|k| k.start_with?('$')}
unless (formatted_stages - known_dollar_stages).empty?
raise "Invalid dollar stages: #{(formatted_stages - known_dollar_stages)}"
end
end
|
.validate_fields_from_stages(*stages) ⇒ Object
176
177
178
179
|
# File 'lib/emerald_odm.rb', line 176
def self.validate_fields_from_stages(*stages)
validate_dollar_stages(*stages)
validate_common_stages(*stages)
end
|
Instance Method Details
#find(filter, projection: {}, sort: {}, limit: 0) ⇒ Array<self>
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/emerald_odm.rb', line 127
def find(filter, projection: {}, sort: {}, limit: 0)
self.class.validate_fields_from_stages(filter, projection, sort)
if projection.empty?
projection = self.class.fields.map { |field| [field, 1] }.to_h
end
query = self.class.collection.find(filter).projection(projection).sort(sort)
if limit > 0
query = query.limit(limit)
end
query.to_a.map { |document| self.class.new(document) }
end
|