bun_bun_bundle 0.3.7 → 0.4.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: 98a722ea47a9c3376749b1db05818efb82f8cba273785d4c44e84a250b7a2b31
4
- data.tar.gz: f6c2344f88eaf01de284d045dc5b8c3c9558c96ebf8b34f87de9c0accab8aef4
3
+ metadata.gz: 192332541b28477a4d791698cb2469d96b6b8ed30243c8edeb5636c7a131360b
4
+ data.tar.gz: 6b5a34372a29e6b53cc0e27b5193db02359f43a508bcbbf451940a438a281b15
5
5
  SHA512:
6
- metadata.gz: f4069cbc3d7fb25acba0545c9966d465a3763e472065c34eed0bf2216e75792a2430389ae7a421c1d4caae2fbdb5f10cd07c90fc58b2bb955d99af7cf3418597
7
- data.tar.gz: 48d6a7b3a6bf8628497586a8a985292ea913387974990d8cb10d90e48791e687197d52be8f5c2e6e2de77e59e31139b97b33ff9880478de4cffec65b4cc7c2cb
6
+ metadata.gz: db8d44d6fdbe8e3ca9513250bdfbeb1bb03b38cf23e7bc01e917effe292300f311e46a7e979ef995cab1fb170bc46f93ab4f514519d85bff9bf3f77f454a0cac
7
+ data.tar.gz: e0808d67347e5728e6bc0d76c5cefe313b260f728fe2da44e8519a5c092b3109f050c1bc23d9810db375b9eafe2bc6e53caf9d353ed03ff25133e325050edca1
data/README.md CHANGED
@@ -152,6 +152,15 @@ All tag helpers accept additional HTML attributes:
152
152
  <%= bun_img_tag('images/logo.png', alt: 'My App', class: 'logo') %>
153
153
  ```
154
154
 
155
+ Data and aria attributes can be passed as nested hashes (Rails-style) or with
156
+ underscores (Lucky-style). Underscores are converted to hyphens automatically:
157
+
158
+ ```erb
159
+ <%= bun_js_tag('js/app.js', data: { turbo_track: "reload" }) %>
160
+ <%= bun_js_tag('js/app.js', data_turbo_track: "reload") %>
161
+ <%# Both render: data-turbo-track="reload" %>
162
+ ```
163
+
155
164
  ## CLI
156
165
 
157
166
  Build your assets using the bundled CLI (`bbb` is available as a shorter
@@ -35,7 +35,7 @@ module BunBunBundle
35
35
  def bun_js_tag(source, **options)
36
36
  src = bun_asset(source)
37
37
  attrs = { type: 'text/javascript' }.merge(options).merge(src: src)
38
- _bun_safe(%(<script #{_bun_html_attrs(attrs)}></script>))
38
+ bun_safe(%(<script #{bun_html_attrs(attrs)}></script>))
39
39
  end
40
40
 
41
41
  # Generates a <link> tag for a CSS entry point.
@@ -46,7 +46,7 @@ module BunBunBundle
46
46
  def bun_css_tag(source, **options)
47
47
  href = bun_asset(source)
48
48
  attrs = { type: 'text/css', rel: 'stylesheet' }.merge(options).merge(href: href)
49
- _bun_safe(%(<link #{_bun_html_attrs(attrs)}>))
49
+ bun_safe(%(<link #{bun_html_attrs(attrs)}>))
50
50
  end
51
51
 
52
52
  # Generates an <img> tag for an image asset.
@@ -58,18 +58,30 @@ module BunBunBundle
58
58
  src = bun_asset(source)
59
59
  alt = options.delete(:alt) || File.basename(source, '.*').tr('-_', ' ').capitalize
60
60
  attrs = { alt: alt }.merge(options).merge(src: src)
61
- _bun_safe(%(<img #{_bun_html_attrs(attrs)}>))
61
+ bun_safe(%(<img #{bun_html_attrs(attrs)}>))
62
62
  end
63
63
 
64
64
  private
65
65
 
66
- def _bun_html_attrs(hash)
67
- hash.compact.map do |k, v|
68
- v == true ? k.to_s : %(#{k}="#{_bun_escape_attr(v)}")
66
+ def bun_html_attrs(hash)
67
+ bun_flatten_attrs(hash).compact.map do |k, v|
68
+ k = k.to_s.tr('_', '-')
69
+ v == true ? k : %(#{k}="#{bun_escape_attr(v)}")
69
70
  end.join(' ')
70
71
  end
71
72
 
72
- def _bun_escape_attr(value)
73
+ def bun_flatten_attrs(hash, prefix: nil)
74
+ hash.each_with_object({}) do |(k, v), flat|
75
+ key = prefix ? :"#{prefix}_#{k}" : k
76
+ if v.is_a?(Hash)
77
+ flat.merge!(bun_flatten_attrs(v, prefix: key))
78
+ else
79
+ flat[key] = v
80
+ end
81
+ end
82
+ end
83
+
84
+ def bun_escape_attr(value)
73
85
  value.to_s.gsub('&', '&amp;').gsub('"', '&quot;').gsub('<', '&lt;').gsub('>', '&gt;')
74
86
  end
75
87
  end
@@ -58,7 +58,7 @@ module BunBunBundle
58
58
  })()
59
59
  </script>
60
60
  HTML
61
- _bun_safe(html)
61
+ bun_safe(html)
62
62
  end
63
63
  end
64
64
  end
@@ -5,9 +5,9 @@ module BunBunBundle
5
5
  private
6
6
 
7
7
  if String.method_defined?(:html_safe)
8
- def _bun_safe(html) = html.html_safe
8
+ def bun_safe(html) = html.html_safe
9
9
  else
10
- def _bun_safe(html) = html
10
+ def bun_safe(html) = html
11
11
  end
12
12
  end
13
13
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BunBunBundle
4
- VERSION = '0.3.7'
4
+ VERSION = '0.4.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bun_bun_bundle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.7
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wout Fierens