heroicons_helper 0.1.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 668ee3c4c63423a55043c27767e157662c42eae1b79dd983f2112482eee0f661
4
- data.tar.gz: a73d673d85a739017cf0b98b383629418a6c8730b1dc98fd9367cb34b574e01f
3
+ metadata.gz: 1567311352d143cb38d0ebf41388ab02cffa75dde06c9aa02694d06dfde7e3ef
4
+ data.tar.gz: b865c9123848efdf54afda2336491a15f7289a9e2f91a2a005731436e248c01a
5
5
  SHA512:
6
- metadata.gz: 43c906d2e65a78ce00fc56c164788930d7e4a6c0c12edaf7c416357a927a5e50e2c8edb8ba210b6f033717761f3ddf6aa4fafb171158a4b81ee893c4971e2d40
7
- data.tar.gz: 19adfe1de1ee6e9ca95cf225afd6d69b9ff9667e2307fb8080b5ca4b7547026800fcab3dbf374a231e9475e5e97aeebfefe61f5216755e0cd56313c1078e67a6
6
+ metadata.gz: 9461351e92db53399116d07f77cbcde4094f50201f550b02d8dcaa125e9671a90b84300c831610be743c6be72d7f578c1069f9ec6e505af484481797225f9c91
7
+ data.tar.gz: 67d4d42f881e0d435494eaa79890b55022d6187d2f99b26dea652d5a0517203ffcc4f531da01a64ed04b8a15f405f9e080539b25999ca823804b49148c7164aa
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