Class: SuperSettings::Storage::TestStorage

Inherits:
Object
  • Object
show all
Includes:
SuperSettings::Storage
Defined in:
lib/super_settings/storage/test_storage.rb

Overview

Implementation of the SuperSettings::Storage model for running unit tests.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SuperSettings::Storage

#==, #create_history, included

Constructor Details

#initializeTestStorage

Returns a new instance of TestStorage.



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/super_settings/storage/test_storage.rb', line 84

def initialize(*)
  @original_key = nil
  @raw_value = nil
  @created_at = nil
  @updated_at = nil
  @description = nil
  @value_type = nil
  @deleted = false
  @persisted = false
  super
end

Class Attribute Details

.settingsObject (readonly)

Returns the value of attribute settings.



18
19
20
# File 'lib/super_settings/storage/test_storage.rb', line 18

def settings
  @settings
end

Instance Attribute Details

#changed_byObject

Returns the value of attribute changed_by.



12
13
14
# File 'lib/super_settings/storage/test_storage.rb', line 12

def changed_by
  @changed_by
end

#created_atObject

Returns the value of attribute created_at.



11
12
13
# File 'lib/super_settings/storage/test_storage.rb', line 11

def created_at
  @created_at
end

#descriptionObject

Returns the value of attribute description.



11
12
13
# File 'lib/super_settings/storage/test_storage.rb', line 11

def description
  @description
end

#keyObject

Returns the value of attribute key.



11
12
13
# File 'lib/super_settings/storage/test_storage.rb', line 11

def key
  @key
end

#raw_valueObject

Returns the value of attribute raw_value.



11
12
13
# File 'lib/super_settings/storage/test_storage.rb', line 11

def raw_value
  @raw_value
end

#updated_atObject

Returns the value of attribute updated_at.



11
12
13
# File 'lib/super_settings/storage/test_storage.rb', line 11

def updated_at
  @updated_at
end

#value_typeObject

Returns the value of attribute value_type.



11
12
13
# File 'lib/super_settings/storage/test_storage.rb', line 11

def value_type
  @value_type
end

Class Method Details

.allObject



34
35
36
37
38
39
40
# File 'lib/super_settings/storage/test_storage.rb', line 34

def all
  settings.values.collect do |attributes|
    setting = new(attributes)
    setting.send(:set_persisted!)
    setting
  end
end

.create_history(key:, changed_by:, created_at:, value: nil, deleted: false) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/super_settings/storage/test_storage.rb', line 63

def create_history(key:, changed_by:, created_at:, value: nil, deleted: false)
  history = @history[key]
  unless history
    history = []
    @history[key] = history
  end

  created_at = SuperSettings::TimePrecision.new(created_at).time if created_at
  item = {key: key, value: value, deleted: deleted, changed_by: changed_by, created_at: created_at}
  history.unshift(item)

  item
end

.destroy_allObject



29
30
31
32
# File 'lib/super_settings/storage/test_storage.rb', line 29

def destroy_all
  @settings = {}
  @history = {}
end

.find_by_key(key) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/super_settings/storage/test_storage.rb', line 50

def find_by_key(key)
  attributes = settings[key]
  return nil unless attributes

  setting = new(attributes)
  setting.send(:set_persisted!)
  setting unless setting.deleted?
end

.history(key) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/super_settings/storage/test_storage.rb', line 20

def history(key)
  items = @history[key]
  unless items
    items = []
    @history[key] = items
  end
  items
end

.last_updated_atObject



59
60
61
# File 'lib/super_settings/storage/test_storage.rb', line 59

def last_updated_at
  settings.values.collect { |attributes| attributes[:updated_at] }.max
end

.updated_since(time) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/super_settings/storage/test_storage.rb', line 42

def updated_since(time)
  settings.values.select { |attributes| attributes[:updated_at].to_f > time.to_f }.collect do |attributes|
    setting = new(attributes)
    setting.send(:set_persisted!)
    setting
  end
end

Instance Method Details

#deleted=(value) ⇒ Object



131
132
133
# File 'lib/super_settings/storage/test_storage.rb', line 131

def deleted=(value)
  @deleted = Coerce.boolean(value)
end

#deleted?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/super_settings/storage/test_storage.rb', line 147

def deleted?
  !!@deleted
end

#history(limit: nil, offset: 0) ⇒ Object



96
97
98
99
100
101
# File 'lib/super_settings/storage/test_storage.rb', line 96

def history(limit: nil, offset: 0)
  items = self.class.history(key)
  items[offset, limit || items.length].collect do |attributes|
    HistoryItem.new(attributes)
  end
end

#persisted?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/super_settings/storage/test_storage.rb', line 151

def persisted?
  !!@persisted
end

#save!Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/super_settings/storage/test_storage.rb', line 103

def save!
  self.updated_at ||= Time.now
  self.created_at ||= updated_at
  if defined?(@original_key) && @original_key
    self.class.settings.delete(@original_key)
  end
  self.class.settings[key] = attributes
  set_persisted!
  true
end