Class: Turnkey::Cache

Inherits:
Object
  • Object
show all
Extended by:
Sanitizers
Defined in:
lib/turnkey/cache.rb

Constant Summary collapse

CACHE_KEY =
'tk-cache'

Class Method Summary collapse

Methods included from Sanitizers

reader_sig_for, writer_sig_for

Class Method Details

.attributesForClass(klass) ⇒ Object



39
40
41
# File 'lib/turnkey/cache.rb', line 39

def self.attributesForClass(klass)
  self.cache[klass.to_s]
end

.cacheObject



10
11
12
# File 'lib/turnkey/cache.rb', line 10

def self.cache
  NSUserDefaults.standardUserDefaults[CACHE_KEY] || {}
end

.classesObject



47
48
49
# File 'lib/turnkey/cache.rb', line 47

def self.classes
  self.cache.keys.map { |klass_name| Object.const_get(klass_name) }
end

.clear_cacheObject



43
44
45
# File 'lib/turnkey/cache.rb', line 43

def self.clear_cache
  NSUserDefaults.standardUserDefaults[CACHE_KEY] = nil
end

.update(instance_or_array) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/turnkey/cache.rb', line 14

def self.update(instance_or_array)
  if instance_or_array.is_a?(Array)
    instance_or_array.each { |item| update(item) }
  else

    @@objs << instance_or_array.object_id

    update_hash = {}
    update_hash[instance_or_array.class.to_s] = instance_or_array.instance_variables.map do |ivar|
      ivar.to_s
    end
    update_hash.delete_if { |klass, attrs| attrs.empty? }
    self.add(update_hash)
    update_references(instance_or_array)
  end
end

.update_references(instance) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/turnkey/cache.rb', line 31

def self.update_references(instance)
  instance.instance_variables.each do |prop|
    value = instance.send(reader_sig_for(prop))
    next if @@objs.include?(value.object_id)
    update(value)
  end
end