Class: ZendeskAppsTools::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/zendesk_apps_tools/cache.rb

Constant Summary collapse

CACHE_FILE_NAME =
'.zat'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Cache

Returns a new instance of Cache.



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

def initialize(options)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/zendesk_apps_tools/cache.rb', line 8

def options
  @options
end

Instance Method Details

#clearObject



26
27
28
# File 'lib/zendesk_apps_tools/cache.rb', line 26

def clear
  File.delete local_cache_path if options[:clean] && File.exist?(local_cache_path)
end

#fetch(key, subdomain = nil) ⇒ Object



21
22
23
24
# File 'lib/zendesk_apps_tools/cache.rb', line 21

def fetch(key, subdomain = nil)
  # drop the default_proc and replace with Hash#dig if older Ruby versions are unsupported
  local_cache[key] || global_cache[subdomain][key] || global_cache['default'][key]
end

#save(hash) ⇒ Object



14
15
16
17
18
19
# File 'lib/zendesk_apps_tools/cache.rb', line 14

def save(hash)
  return if options[:zipfile]

  local_cache.update(hash)
  File.open(local_cache_path, 'w') { |f| f.write JSON.pretty_generate(local_cache) }
end