Class: SuperSettings::Storage::MongoDBStorage
Overview
MongoDB implementation of the SuperSettings::Storage model.
You must define the connection URL to use by setting the ‘url` or `mongodb` attribute on the class.
Defined Under Namespace
Classes: HistoryStorage
Constant Summary
collapse
- DEFAULT_COLLECTION_NAME =
"super_settings"
Class Attribute Summary collapse
#created_at, #description, #key, #raw_value, #updated_at, #value_type
Class Method Summary
collapse
Instance Method Summary
collapse
included, #persisted=, #persisted?, #save!
#==, #create_history, #created_at, #deleted=, #deleted?, #description, #description=, included, #key, #key=, #persisted?, #raw_value, #raw_value=, #save!, #updated_at, #value_type, #value_type=
#deleted=, #deleted?, #initialize, #persisted=, #persisted?
Class Attribute Details
.collection_name ⇒ Object
Returns the value of attribute collection_name.
62
63
64
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 62
def collection_name
@collection_name
end
|
.mongodb ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 64
def mongodb
if @mongodb.nil? || @url_hash != @url.hash
@mutex.synchronize do
unless @url_hash == @url.hash
@url_hash = @url.hash
@mongodb = Mongo::Client.new(@url)
create_indexes!(@mongodb)
end
end
end
@mongodb
end
|
.url=(value) ⇒ Object
61
62
63
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 61
def url=(value)
@url = value
end
|
Class Method Details
.all ⇒ Object
90
91
92
93
94
95
96
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 90
def all
settings_collection.find.projection(history: 0).collect do |attributes|
record = new(attributes)
record.persisted = true
record
end
end
|
.create_history(key:, changed_by:, created_at:, value: nil, deleted: false) ⇒ Object
112
113
114
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 112
def create_history(key:, changed_by:, created_at:, value: nil, deleted: false)
HistoryStorage.create!(key: key, value: value, changed_by: changed_by, created_at: created_at, deleted: deleted)
end
|
.destroy_all ⇒ Object
116
117
118
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 116
def destroy_all
settings_collection.delete_many({})
end
|
.find_by_key(key) ⇒ Object
98
99
100
101
102
103
104
105
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 98
def find_by_key(key)
query = {
key: key,
deleted: false
}
record = settings_collection.find(query).projection(history: 0).first
new(record) if record
end
|
.last_updated_at ⇒ Object
107
108
109
110
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 107
def last_updated_at
last_updated_setting = settings_collection.find.projection(updated_at: 1).sort(updated_at: -1).limit(1).first
last_updated_setting["updated_at"] if last_updated_setting
end
|
.save_all(changes) ⇒ Object
120
121
122
123
124
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 120
def save_all(changes)
upserts = changes.collect { |setting| upsert(setting) }
settings_collection.bulk_write(upserts)
true
end
|
.settings_collection ⇒ Object
77
78
79
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 77
def settings_collection
mongodb[collection_name]
end
|
.updated_since(time) ⇒ Object
81
82
83
84
85
86
87
88
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 81
def updated_since(time)
time = TimePrecision.new(time, :millisecond).time
settings_collection.find(updated_at: {"$gt": time}).projection(history: 0).sort({updated_at: -1}).collect do |attributes|
record = new(attributes)
record.persisted = true
record
end
end
|
Instance Method Details
#as_bson ⇒ Object
227
228
229
230
231
232
233
234
235
236
237
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 227
def as_bson
{
key: key,
raw_value: raw_value,
value_type: value_type,
description: description,
created_at: created_at,
updated_at: updated_at,
deleted: deleted?
}
end
|
#created_at=(val) ⇒ Object
215
216
217
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 215
def created_at=(val)
super(TimePrecision.new(val, :millisecond).time)
end
|
#destroy ⇒ Object
223
224
225
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 223
def destroy
settings_collection.delete_one(key: key)
end
|
#history(limit: nil, offset: 0) ⇒ Object
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 173
def history(limit: nil, offset: 0)
pipeline = [
{
"$match": {key: key}
},
{
"$addFields": {
history: {
"$sortArray": {
input: "$history",
sortBy: {created_at: -1}
}
}
}
}
]
if limit || offset > 0
pipeline << {
"$addFields": {
history: {
"$slice": ["$history", offset, (limit || {"$size": "$history"})]
}
}
}
end
pipeline << {
"$project": {
_id: 0,
history: 1
}
}
record = self.class.settings_collection.aggregate(pipeline).to_a.first
return [] unless record && record["history"].is_a?(Array)
record["history"].collect do |record|
HistoryItem.new(key: key, value: record["value"], changed_by: record["changed_by"], created_at: record["created_at"], deleted: record["deleted"])
end
end
|
#updated_at=(val) ⇒ Object
219
220
221
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 219
def updated_at=(val)
super(TimePrecision.new(val, :millisecond).time)
end
|