Class: SuperSettings::Storage::JSONStorage
- Inherits:
-
StorageAttributes
- Object
- StorageAttributes
- SuperSettings::Storage::JSONStorage
- Includes:
- Transaction
- Defined in:
- lib/super_settings/storage/json_storage.rb
Overview
This is an abstract storage class that provides support for storing settings in a JSON file. The settings are stored in JSON as an array of hashes with each hash representing a setting.
Setting history should be stored in separate JSON files per key and are loaded separately from the main settings file.
This class can be used as the base for any storage class where the settings are all stored together in a single JSON payload.
Writes are serialized within a process, but there is no coordination between processes. If multiple processes write settings at the same time, the last write wins and can overwrite changes made by another process. Storage backends based on this class are best suited for setups where settings are updated from a single process at a time.
Subclasses must implement the following methods:
- self.all
- self.last_updated_at
- save!
Direct Known Subclasses
Defined Under Namespace
Classes: HistoryStorage
Instance Attribute Summary
Attributes inherited from StorageAttributes
#created_at, #description, #key, #raw_value, #updated_at, #value_type
Class Method Summary collapse
- .all ⇒ Object
- .create_history(key:, changed_by:, created_at:, value: nil, deleted: false) ⇒ Object
- .find_by_key(key) ⇒ Object
-
.parse_settings(json) ⇒ Array<SuperSettings::Storage::JSONStorage>
Helper method to load settings from a JSON string.
- .save_all(changes) ⇒ Object
- .updated_since(timestamp) ⇒ Object
Instance Method Summary collapse
- #as_json ⇒ Object
- #created_at=(val) ⇒ Object
- #history(limit: nil, offset: 0) ⇒ Object
- #updated_at=(val) ⇒ Object
Methods included from Transaction
included, #persisted=, #persisted?, #save!
Methods inherited from StorageAttributes
#deleted=, #deleted?, #initialize, #persisted=, #persisted?
Methods included from SuperSettings::Storage
#==, #create_history, #created_at, #deleted=, #deleted?, #description, #description=, included, #key, #key=, #persisted?, #raw_value, #raw_value=, #save!, #updated_at, #value_type, #value_type=
Constructor Details
This class inherits a constructor from SuperSettings::Storage::StorageAttributes
Class Method Details
.all ⇒ Object
62 63 64 |
# File 'lib/super_settings/storage/json_storage.rb', line 62 def all parse_settings(settings_json_payload) end |
.create_history(key:, changed_by:, created_at:, value: nil, deleted: false) ⇒ Object
74 75 76 |
# File 'lib/super_settings/storage/json_storage.rb', line 74 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, storage: self) end |
.find_by_key(key) ⇒ Object
70 71 72 |
# File 'lib/super_settings/storage/json_storage.rb', line 70 def find_by_key(key) active.detect { |setting| setting.key == key } end |
.parse_settings(json) ⇒ Array<SuperSettings::Storage::JSONStorage>
Helper method to load settings from a JSON string.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/super_settings/storage/json_storage.rb', line 132 def parse_settings(json) return [] if Coerce.blank?(json) JSON.parse(json).collect do |attributes| setting = new( key: attributes["key"], raw_value: attributes["value"], description: attributes["description"], value_type: attributes["value_type"], updated_at: Time.parse(attributes["updated_at"]), created_at: Time.parse(attributes["created_at"]), deleted: attributes["deleted"] ) setting.persisted = true setting end end |
.save_all(changes) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/super_settings/storage/json_storage.rb', line 78 def save_all(changes) SAVE_MUTEX.synchronize do existing = {} parse_settings(settings_json_payload).each do |setting| existing[setting.key] = setting end history_items = [] changes.each do |record| if record.is_a?(HistoryStorage) history_items << record else existing[record.key] = record end end settings = existing.values.sort_by(&:key) changed_histories = {} history_items.each do |history_item| setting = existing[history_item.key] next unless setting history = changed_histories[history_item.key] unless history history = setting.history.dup changed_histories[history_item.key] = history end history.unshift(history_item) end settings_json = JSON.dump(settings.collect(&:as_json)) save_settings_json(settings_json) changed_histories.each do |setting_key, setting_history| ordered_history = setting_history.sort_by { |history_item| history_item.created_at }.reverse payload = ordered_history.collect do |history_item| { value: history_item.value, changed_by: history_item.changed_by, created_at: history_item.created_at.iso8601(6), deleted: history_item.deleted? } end history_json = JSON.dump(payload) save_history_json(setting_key, history_json) end end end |
.updated_since(timestamp) ⇒ Object
66 67 68 |
# File 'lib/super_settings/storage/json_storage.rb', line 66 def updated_since() all.select { |setting| setting.updated_at > } end |
Instance Method Details
#as_json ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/super_settings/storage/json_storage.rb', line 201 def as_json { key: key, value: raw_value, value_type: value_type, description: description, created_at: created_at&.iso8601(6), updated_at: updated_at&.iso8601(6), deleted: deleted? } end |
#created_at=(val) ⇒ Object
185 186 187 |
# File 'lib/super_settings/storage/json_storage.rb', line 185 def created_at=(val) super(TimePrecision.new(val).time) end |
#history(limit: nil, offset: 0) ⇒ Object
193 194 195 196 197 198 199 |
# File 'lib/super_settings/storage/json_storage.rb', line 193 def history(limit: nil, offset: 0) history = fetch_history limit ||= history.length history[offset, limit].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
189 190 191 |
# File 'lib/super_settings/storage/json_storage.rb', line 189 def updated_at=(val) super(TimePrecision.new(val).time) end |