Class: SuperSettings::Storage::RedisStorage::HistoryStorage

Inherits:
HistoryAttributes show all
Defined in:
lib/super_settings/storage/redis_storage.rb

Constant Summary collapse

HISTORY_KEY_PREFIX =
"SuperSettings.history"

Instance Attribute Summary

Attributes inherited from HistoryAttributes

#changed_by, #deleted, #key, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from HistoryAttributes

#deleted?, #initialize

Methods included from Attributes

#attributes=, #initialize

Constructor Details

This class inherits a constructor from SuperSettings::Storage::HistoryAttributes

Class Method Details

.create!(attributes) ⇒ Object



47
48
49
50
51
# File 'lib/super_settings/storage/redis_storage.rb', line 47

def create!(attributes)
  record = new(attributes)
  record.save!
  record
end

.destroy_all_by_key(key, redis) ⇒ Object



53
54
55
# File 'lib/super_settings/storage/redis_storage.rb', line 53

def destroy_all_by_key(key, redis)
  redis.del("#{HISTORY_KEY_PREFIX}.#{key}")
end

.find_all_by_key(key:, offset: 0, limit: nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/super_settings/storage/redis_storage.rb', line 35

def find_all_by_key(key:, offset: 0, limit: nil)
  end_index = (limit.nil? ? -1 : offset + limit - 1)
  return [] unless end_index >= -1

  payloads = RedisStorage.with_redis { |redis| redis.lrange("#{HISTORY_KEY_PREFIX}.#{key}", offset, end_index) }
  payloads.collect do |json|
    record = new(JSON.parse(json))
    record.key = key.to_s
    record
  end
end

.redis_key(key) ⇒ Object



57
58
59
# File 'lib/super_settings/storage/redis_storage.rb', line 57

def redis_key(key)
  "#{HISTORY_KEY_PREFIX}.#{key}"
end

Instance Method Details

#created_atObject



74
75
76
# File 'lib/super_settings/storage/redis_storage.rb', line 74

def created_at
  SuperSettings::Storage::RedisStorage.time_at_microseconds(super)
end

#save!Object

Raises:

  • (ArgumentError)


62
63
64
65
66
67
68
# File 'lib/super_settings/storage/redis_storage.rb', line 62

def save!
  raise ArgumentError.new("Missing key") if Coerce.blank?(key)

  RedisStorage.transaction do |changes|
    changes << self
  end
end

#save_to_redis(redis) ⇒ Object



70
71
72
# File 'lib/super_settings/storage/redis_storage.rb', line 70

def save_to_redis(redis)
  redis.lpush(self.class.redis_key(key), payload_json.to_json)
end