class Turnkey::Cache

Constants

CACHE_KEY

Public Class Methods

attributesForClass(klass) click to toggle source
# File lib/turnkey/cache.rb, line 39
def self.attributesForClass(klass)
  self.cache[klass.to_s]
end
cache() click to toggle source
# File lib/turnkey/cache.rb, line 10
def self.cache
  NSUserDefaults.standardUserDefaults[CACHE_KEY] || {}
end
classes() click to toggle source
# File lib/turnkey/cache.rb, line 47
def self.classes
  self.cache.keys.map { |klass_name| Object.const_get(klass_name) }
end
clear_cache() click to toggle source
# File lib/turnkey/cache.rb, line 43
def self.clear_cache
  NSUserDefaults.standardUserDefaults[CACHE_KEY] = nil
end
update(instance_or_array) click to toggle source
# 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) click to toggle source
# 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

Private Class Methods

add(to_add) click to toggle source
# File lib/turnkey/cache.rb, line 57
def self.add(to_add)
  updated = self.cache.mutableCopy.merge!(to_add) { |key, orig_val, new_val|
    orig_val | new_val
  }
  setCache(updated)
end
setCache(new_cache) click to toggle source
# File lib/turnkey/cache.rb, line 53
def self.setCache(new_cache)
  NSUserDefaults.standardUserDefaults[CACHE_KEY] = new_cache
end