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
76
77
78
|
# 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
client = Mongo::Client.new(@url)
create_indexes!(client)
@mongodb = client
@url_hash = @url.hash
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
93
94
95
96
97
98
99
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 93
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
119
120
121
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 119
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
123
124
125
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 123
def destroy_all
settings_collection.delete_many({})
end
|
.find_by_key(key) ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 101
def find_by_key(key)
query = {
key: key,
deleted: false
}
attributes = settings_collection.find(query).projection(history: 0).first
if attributes
record = new(attributes)
record.persisted = true
record
end
end
|
.last_updated_at ⇒ Object
114
115
116
117
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 114
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
127
128
129
130
131
132
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 127
def save_all(changes)
upserts = changes.collect { |setting| upsert(setting) }
return true if upserts.empty?
settings_collection.bulk_write(upserts)
true
end
|
.settings_collection ⇒ Object
80
81
82
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 80
def settings_collection
mongodb[collection_name]
end
|
.updated_since(time) ⇒ Object
84
85
86
87
88
89
90
91
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 84
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
235
236
237
238
239
240
241
242
243
244
245
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 235
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
223
224
225
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 223
def created_at=(val)
super(TimePrecision.new(val, :millisecond).time)
end
|
#destroy ⇒ Object
231
232
233
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 231
def destroy
settings_collection.delete_one(key: key)
end
|
#history(limit: nil, offset: 0) ⇒ Object
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
214
215
216
217
218
219
220
221
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 181
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
227
228
229
|
# File 'lib/super_settings/storage/mongodb_storage.rb', line 227
def updated_at=(val)
super(TimePrecision.new(val, :millisecond).time)
end
|