class HabiticaCli::Cache

Extremely simplistic layer on top of PStore Responsible for caching habit responses

Public Class Methods

new(path = nil) click to toggle source
# File lib/habitica_cli/cache.rb, line 5
def initialize(path = nil)
  @path = path || File.join(Dir.home, '.habitca-cli-cache')
  create_store(@path)
end

Public Instance Methods

create_store(path) click to toggle source
# File lib/habitica_cli/cache.rb, line 10
def create_store(path)
  @store = PStore.new(path)
end
destroy!() click to toggle source
# File lib/habitica_cli/cache.rb, line 18
def destroy!
  File.delete(@store.path)
  create_store(@path)
end
get(key, default = nil) click to toggle source
# File lib/habitica_cli/cache.rb, line 14
def get(key, default = nil)
  @store.transaction { @store.fetch(key, default) }
end
store_tasks(items) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/habitica_cli/cache.rb, line 24
def store_tasks(items)
  shortened = nil
  count = get('count', 0)
  @store.transaction do
    shortened = items.map do |item|
      count += 1
      short_id = "#{item['id'][0]}#{count}"

      @store[short_id] = item

      item['cid'] = short_id
      item
    end
    @store[:count] = count
  end
  shortened
end