heroicons_helper 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 668ee3c4c63423a55043c27767e157662c42eae1b79dd983f2112482eee0f661
4
- data.tar.gz: a73d673d85a739017cf0b98b383629418a6c8730b1dc98fd9367cb34b574e01f
3
+ metadata.gz: 95ec724478d383801f98ffeeb2bd8d0f5f14fea4c6eee777b59b9bc5e0337d78
4
+ data.tar.gz: 18e9be3121ca73f11a472a36ed35d31b2c157a12a4d177395e82ea7d5dacf993
5
5
  SHA512:
6
- metadata.gz: 43c906d2e65a78ce00fc56c164788930d7e4a6c0c12edaf7c416357a927a5e50e2c8edb8ba210b6f033717761f3ddf6aa4fafb171158a4b81ee893c4971e2d40
7
- data.tar.gz: 19adfe1de1ee6e9ca95cf225afd6d69b9ff9667e2307fb8080b5ca4b7547026800fcab3dbf374a231e9475e5e97aeebfefe61f5216755e0cd56313c1078e67a6
6
+ metadata.gz: 5ab5881814e673ef1d7ee9e24f454a05796c4558baaa780a3c0d7a1365dec92e67858c535b41e046b3cf548b9265a9446fbdb38fc10631e3e1475566f074a742
7
+ data.tar.gz: 4fee60bcdd51cb3109fc48acde1693a702f965ab3475f9f9ebce6ed9e26110f5cc668febde294e4d20f30cbe6de64d07aae1e686ec7f62d742987a3ca98aa111
data/Gemfile CHANGED
@@ -6,10 +6,13 @@ source "https://rubygems.org"
6
6
  gemspec
7
7
 
8
8
  group :development, :test do
9
+ gem "amazing_print"
10
+ gem "debug"
9
11
  gem "rake", "~> 13.0"
10
12
  gem "rubocop-standard", "~> 7.0"
11
13
  end
12
14
 
13
15
  group :test do
14
16
  gem "minitest", "~> 5.15"
17
+ gem "minitest-focus", "~> 1.3"
15
18
  end
data/README.md CHANGED
@@ -44,6 +44,32 @@ puts outline_icon.to_svg
44
44
  => <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12"></path></svg>
45
45
  ```
46
46
 
47
+ ## Cache
48
+
49
+ This gem also comes with a simple caching system, which can be useful to preload icons. It works like this:
50
+
51
+ ``` ruby
52
+ icons_to_preload = [{
53
+ name: "thumb-down",
54
+ variant: "outline",
55
+ }, {
56
+ name: "refresh",
57
+ variant: "solid",
58
+ },]
59
+
60
+ HeroiconsHelper::Cache.preload!(icons_to_preload) do |found, icon|
61
+ # An instance of `FakeClass` is stored in the cache
62
+ FakeClass.new(icon) unless found
63
+ end
64
+ ```
65
+
66
+ `HeroiconsHelper::Cache.preload!` does one of two things:
67
+
68
+ * If, given the `icons_to_preload` array, an item is located in the cache, `found` is true and `icon` is the cached item
69
+ * Otherwise, `found` is false, and `icon` is the element currently being iterated. Also, the last line of the block sets the cache
70
+
71
+ The Hash elements within `icons_to_preload` can also take `height` and `width` keys.
72
+
47
73
  ## Development
48
74
 
49
75
  To update the Heroicons set:
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HeroiconsHelper
4
+ # :nodoc:
5
+ class Cache
6
+ LOOKUP = {}
7
+
8
+ class << self
9
+ def get_key(name:, variant:, width: nil, height: nil)
10
+ attrs = { name: name, variant: variant, width: width, height: height }
11
+ attrs.compact!
12
+ attrs.hash
13
+ end
14
+
15
+ def read(key)
16
+ LOOKUP[key]
17
+ end
18
+
19
+ # Cache size limit.
20
+ def limit
21
+ 500
22
+ end
23
+
24
+ def set(key, value)
25
+ LOOKUP[key] = value
26
+
27
+ # Remove first item when the cache is too large.
28
+ LOOKUP.shift if LOOKUP.size > limit
29
+ end
30
+
31
+ def clear!
32
+ LOOKUP.clear
33
+ end
34
+
35
+ # @param icons_to_preload [Array<Hash>] List of icons to render, in the format { name: :icon_name, variant: "variant" }.
36
+ def preload!(icons_to_preload, &block)
37
+ raise ArgumentError, "icons_to_preload must be an Array; it's #{icons_to_preload.class}" unless icons_to_preload.is_a?(Array)
38
+ raise ArgumentError, "icons_to_preload must have between 1 and 20 items; you have #{preload.count}" unless (1..20).cover?(icons_to_preload.count)
39
+
40
+ icons_to_preload.each do |icon|
41
+ height = icon["height"] || nil
42
+ width = icon["width"] || nil
43
+
44
+ # Don't allow sizes under 16px
45
+ if !height.nil? && height.to_i < 16
46
+ height = nil
47
+ end
48
+ if !width.nil? && width.to_i < 16
49
+ width = nil
50
+ end
51
+
52
+ cache_key = HeroiconsHelper::Cache.get_key(
53
+ name: icon["name"],
54
+ variant: icon["variant"],
55
+ height: height,
56
+ width: width
57
+ )
58
+
59
+ cache_icon = HeroiconsHelper::Cache.read(cache_key)
60
+ found = !cache_icon.nil?
61
+
62
+ result = yield found, cache_icon || icon
63
+
64
+ HeroiconsHelper::Cache.set(cache_key, result) unless found
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end