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
-
#cache_path ⇒ Object
readonly
Returns the value of attribute cache_path.
-
#download_queue ⇒ Object
readonly
Returns the value of attribute download_queue.
-
#download_threads ⇒ Object
Returns the value of attribute download_threads.
Instance Method Summary collapse
-
#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).
-
#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).
-
#cache_index(destination = nil) ⇒ Object
Updates an index in the specified destination (or the cache path if not specified).
- #load_cache(path = nil) ⇒ Object
- #setup_cache(path = nil) ⇒ Object
-
#write_index(destination) ⇒ Object
[over]writes a sanitized index to the specified destination.
Methods included from CollectionAssetInformation
#generate_checksums, #generate_paths, #get_checksums, #get_paths, #get_paths?
Instance Attribute Details
#cache_path ⇒ Object (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_queue ⇒ Object (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_threads ⇒ Object
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 |
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)
45 46 47 48 |
# File 'lib/emojidex/data/collection/cache.rb', line 45 def cache( = {}) _cache() 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)
56 57 58 59 60 61 |
# File 'lib/emojidex/data/collection/cache.rb', line 56 def cache!( = {}) _cache() 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.
66 67 68 69 70 71 72 73 |
# File 'lib/emojidex/data/collection/cache.rb', line 66 def cache_index(destination = nil) destination ||= @cache_path idx = Emojidex::Data::Collection.new idx.load_local_collection(destination) if FileTest.exist? "#{destination}/emoji.json" idx.add_emoji @emoji.values #File.open("#{destination}/emoji.json", 'w') { |f| f.write idx.emoji.values.to_json } write_index(destination) end |
#load_cache(path = nil) ⇒ Object
35 36 37 38 |
# File 'lib/emojidex/data/collection/cache.rb', line 35 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 |
# File 'lib/emojidex/data/collection/cache.rb', line 15 def setup_cache(path = nil) @download_queue = [] @download_threads = 8 # check if cache dir is already set return @cache_path if @cache_path && path.nil? # setup cache @cache_path = File.((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.
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/emojidex/data/collection/cache.rb', line 77 def write_index(destination) idx = @emoji.values.to_json idx = JSON.parse idx idx.each do |moji| moji['paths'] = nil moji['local_checksums'] = nil moji.delete_if { |_k, v| v.nil? } end File.open("#{destination}/emoji.json", 'w') { |f| f.write idx.to_json } end |