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.
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>
Heper 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
51 52 53 |
# File 'lib/super_settings/storage/json_storage.rb', line 51 def all parse_settings(settings_json_payload) end |
.create_history(key:, changed_by:, created_at:, value: nil, deleted: false) ⇒ Object
63 64 65 |
# File 'lib/super_settings/storage/json_storage.rb', line 63 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
59 60 61 |
# File 'lib/super_settings/storage/json_storage.rb', line 59 def find_by_key(key) active.detect { |setting| setting.key == key } end |
.parse_settings(json) ⇒ Array<SuperSettings::Storage::JSONStorage>
Heper method to load settings from a JSON string.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/super_settings/storage/json_storage.rb', line 119 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
67 68 69 70 71 72 73 74 75 76 77 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 |
# File 'lib/super_settings/storage/json_storage.rb', line 67 def save_all(changes) 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 |
.updated_since(timestamp) ⇒ Object
55 56 57 |
# File 'lib/super_settings/storage/json_storage.rb', line 55 def updated_since() all.select { |setting| setting.updated_at > } end |
Instance Method Details
#as_json ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/super_settings/storage/json_storage.rb', line 188 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
172 173 174 |
# File 'lib/super_settings/storage/json_storage.rb', line 172 def created_at=(val) super(TimePrecision.new(val).time) end |
#history(limit: nil, offset: 0) ⇒ Object
180 181 182 183 184 185 186 |
# File 'lib/super_settings/storage/json_storage.rb', line 180 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
176 177 178 |
# File 'lib/super_settings/storage/json_storage.rb', line 176 def updated_at=(val) super(TimePrecision.new(val).time) end |