Class: Primer::Octicon::Cache

Inherits:
Object
  • Object
show all
Defined in:
app/lib/primer/octicon/cache.rb

Overview

:nodoc:

Constant Summary collapse

LOOKUP =
{}
PRELOADED_ICONS =

Preload the top 20 used icons.

[:alert, :check, :"chevron-down", :paste, :clock, :"dot-fill", :info, :"kebab-horizontal", :link, :lock, :mail, :pencil, :plus, :question, :repo, :search, :"shield-lock", :star, :trash, :x].freeze

Class Method Summary collapse

Class Method Details

.clear!Object



34
35
36
# File 'app/lib/primer/octicon/cache.rb', line 34

def clear!
  LOOKUP.clear
end

.get_key(symbol:, size:, width: nil, height: nil) ⇒ Object



12
13
14
15
16
# File 'app/lib/primer/octicon/cache.rb', line 12

def get_key(symbol:, size:, width: nil, height: nil)
  attrs = { symbol: symbol, size: size, width: width, height: height }
  attrs.compact!
  attrs.hash
end

.limitObject

Cache size limit.



23
24
25
# File 'app/lib/primer/octicon/cache.rb', line 23

def limit
  500
end

.preload!Object



38
39
40
# File 'app/lib/primer/octicon/cache.rb', line 38

def preload!
  PRELOADED_ICONS.each { |icon| Primer::Beta::Octicon.new(icon: icon) }
end

.read(key) ⇒ Object



18
19
20
# File 'app/lib/primer/octicon/cache.rb', line 18

def read(key)
  LOOKUP[key]
end

.set(key, value) ⇒ Object



27
28
29
30
31
32
# File 'app/lib/primer/octicon/cache.rb', line 27

def set(key, value)
  LOOKUP[key] = value

  # Remove first item when the cache is too large.
  LOOKUP.shift if LOOKUP.size > limit
end