Module: Emojidex::Data::CollectionCache

Includes:
CollectionAssetInformation
Included in:
Collection
Defined in:
lib/emojidex/data/collection/cache.rb

Overview

local caching functionality for collections

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CollectionAssetInformation

#generate_checksums, #generate_paths, #get_checksums, #get_combo_checksums, #get_combo_paths, #get_paths, #get_paths?

Instance Attribute Details

#cache_pathObject (readonly)

Returns the value of attribute cache_path.



12
13
14
# File 'lib/emojidex/data/collection/cache.rb', line 12

def cache_path
  @cache_path
end

#download_queueObject (readonly)

Returns the value of attribute download_queue.



12
13
14
# File 'lib/emojidex/data/collection/cache.rb', line 12

def download_queue
  @download_queue
end

#download_threadsObject

Returns the value of attribute download_threads.



13
14
15
# File 'lib/emojidex/data/collection/cache.rb', line 13

def download_threads
  @download_threads
end

#formatsObject

Returns the value of attribute formats.



13
14
15
# File 'lib/emojidex/data/collection/cache.rb', line 13

def formats
  @formats
end

#sizesObject

Returns the value of attribute sizes.



13
14
15
# File 'lib/emojidex/data/collection/cache.rb', line 13

def sizes
  @sizes
end

Instance Method Details

#cache(options = {}) ⇒ Object

Caches emoji to local emoji storage cache Options:

cache_path: manually specify cache location
formats: formats to cache (default is SVG only)
sizes: sizes to cache (default is px32, but this is irrelivant for SVG)


47
48
49
50
# File 'lib/emojidex/data/collection/cache.rb', line 47

def cache(options = {})
  _cache(options)
  cache_index
end

#cache!(options = {}) ⇒ Object

Caches emoji to local emoji storage cache +regenerates checksums and paths Options:

cache_path: manually specify cache location
formats: formats to cache (default is SVG only)
sizes: sizes to cache (default is px32, but this is irrelivant for SVG)


58
59
60
61
62
63
# File 'lib/emojidex/data/collection/cache.rb', line 58

def cache!(options = {})
  _cache(options)
  generate_paths
  generate_checksums
  cache_index
end

#cache_index(destination = nil) ⇒ Object

Updates an index in the specified destination (or the cache path if not specified). This method reads the existing index, combines the contents with this collection, and writes the results.



68
69
70
71
72
73
74
75
# File 'lib/emojidex/data/collection/cache.rb', line 68

def cache_index(destination = nil)
  destination ||= @cache_path
  idx = Emojidex::Data::Collection.new
  idx.r18 = @r18
  idx.load_local_collection(destination) if FileTest.exist? "#{destination}/emoji.json"
  idx.add_emoji @emoji.values
  idx.write_index(destination)
end

#load_cache(path = nil) ⇒ Object



37
38
39
40
# File 'lib/emojidex/data/collection/cache.rb', line 37

def load_cache(path = nil)
  setup_cache(path)
  load_local_collection(@cache_path)
end

#setup_cache(path = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/emojidex/data/collection/cache.rb', line 15

def setup_cache(path = nil)
  @formats = Emojidex::Defaults.selected_formats unless @formats
  @sizes = Emojidex::Defaults.selected_sizes unless @sizes
  @download_queue = []
  @download_threads = 4
  # check if cache dir is already set
  return @cache_path if @cache_path && path.nil?
  # setup cache
  @cache_path =
    File.expand_path((path || Emojidex::Defaults.system_cache_path) + '/emoji')
  # ENV['EMOJI_CACHE'] = @cache_path
  FileUtils.mkdir_p(@cache_path)
  Emojidex::Defaults.sizes.keys.each do |size|
    FileUtils.mkdir_p(@cache_path + "/#{size}")
  end
  # load will expect emoji.json even if it contains no emoji
  unless File.exist? "#{cache_path}/emoji.json"
    File.open("#{@cache_path}/emoji.json", 'w') { |f| f.write '[]' }
  end
  @cache_path
end

#write_index(destination) ⇒ Object

[over]writes a sanitized index to the specified destination. WARNING: This method destroys any index files in the destination.



79
80
81
82
83
84
85
86
87
88
# File 'lib/emojidex/data/collection/cache.rb', line 79

def write_index(destination)
  idx = @emoji.values.to_json
  idx = JSON.parse idx
  idx.each do |moji|
    moji['paths'] = nil
    moji['remote_checksums'] = nil
    moji.delete_if { |_k, v| v.nil? }
  end
  File.open("#{destination}/emoji.json", 'w') { |f| f.write idx.to_json }
end