katalyst-html-attributes 1.0.1 → 1.1.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: ebf02bb3d00b36a7de997289b233babcc769526e94fdb2c4794dfa87a72304b5
4
- data.tar.gz: e873c07f7f2afe9723883fa8742274a22b6c1b634335dc5ed43a3c0ff78c7060
3
+ metadata.gz: c544198e07c9d144a3e9aceeb25e313c50363556996eb18676da0c833702e06d
4
+ data.tar.gz: 78b155006b143e651facf97731065ef402151d20a00d1763f5f1228bfe03f5f8
5
5
  SHA512:
6
- metadata.gz: 4f1bc239f47e06a941336e78047ce0b915ad533bf40f54775619f5e3c7797471adc829f8f14e75e8ff6d23962d8491a480b64e40fbac1f602806e763b87030e0
7
- data.tar.gz: 583b0588f9b353b17a0b54537d1d31c1f68f08a8e18df8496fd111253ce263e3cdd0a670fd36a5b23430b24f9a040c4a31132104631adf8ad611db23f0e6ac67
6
+ metadata.gz: 2dfd3e3ddfb9b4dddbc2cad16dac27132b63fc9e824acfd49db14f8c1f2a5aea02d3f989626f3d2debb75100eb4a88372d45b0f0fe222b7a537d7e8341e04b88
7
+ data.tar.gz: e8c1041fbc322d54dae3b89643496d61a229394393dbec9b4cb40c5eedba9e532a529a5c72a1092061558cd5cc72ac16be74d6dc4033c9774cf9a5a76b968fcf
data/README.md CHANGED
@@ -30,3 +30,49 @@ class MyViewComponent < ViewComponent::Base
30
30
  end
31
31
  end
32
32
  ```
33
+
34
+ You can also define your own named attributes:
35
+
36
+ ```ruby
37
+ class MyViewComponent < ViewComponent::Base
38
+ include Katalyst::HtmlAttributes
39
+
40
+ define_html_attributes :link_attributes, :button_attributes
41
+
42
+ def initialize(link:, button:, **html_attributes)
43
+ super(**html_attributes)
44
+
45
+ update_link_attributes(**link) if link
46
+ update_button_attributes(**button) if button
47
+ end
48
+
49
+ def call
50
+ tag.div(**html_attributes) do
51
+ tag.a(**link_attributes) do
52
+ "Link"
53
+ end +
54
+ tag.button(**button_attributes) do
55
+ "Button"
56
+ end
57
+ end
58
+ end
59
+
60
+ def default_html_attributes
61
+ {
62
+ class: "my-class"
63
+ }
64
+ end
65
+
66
+ def default_link_attributes
67
+ {
68
+ class: "my-link"
69
+ }
70
+ end
71
+
72
+ def default_button_attributes
73
+ {
74
+ class: "my-button"
75
+ }
76
+ end
77
+ end
78
+ ```
@@ -6,19 +6,30 @@ module Katalyst
6
6
  module HtmlAttributes
7
7
  extend ActiveSupport::Concern
8
8
 
9
- class_methods do
10
- using HasHtmlAttributes
9
+ using HasHtmlAttributes
10
+
11
+ def self.options_to_html_attributes(options)
12
+ options.slice(:id, :aria, :class, :data).merge_html(options.fetch(:html, {}))
13
+ end
11
14
 
15
+ class_methods do
12
16
  def define_html_attribute_methods(name, default: {})
13
- define_method("default_#{name}") { default }
14
- private("default_#{name}")
17
+ define_method(:"default_#{name}") { default }
18
+ private(:"default_#{name}")
15
19
 
16
20
  define_method(name) do
17
- send("default_#{name}").merge_html(instance_variable_get("@#{name}") || {})
21
+ send(:"default_#{name}").merge_html(instance_variable_get(:"@#{name}") || {})
22
+ end
23
+
24
+ define_method(:"#{name}=") do |options|
25
+ instance_variable_set(:"@#{name}", HtmlAttributes.options_to_html_attributes(options))
18
26
  end
19
27
 
20
- define_method("#{name}=") do |options|
21
- instance_variable_set("@#{name}", options.slice(:id, :aria, :class, :data).merge(options.fetch(:html, {})))
28
+ define_method(:"update_#{name}") do |**options, &block|
29
+ attributes = instance_variable_get(:"@#{name}") || {}
30
+ attributes = attributes.merge_html(HtmlAttributes.options_to_html_attributes(options))
31
+ attributes = yield(attributes) if block
32
+ instance_variable_set(:"@#{name}", attributes)
22
33
  end
23
34
  end
24
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: katalyst-html-attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katalyst Interactive
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-15 00:00:00.000000000 Z
11
+ date: 2024-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -38,7 +38,7 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- description:
41
+ description:
42
42
  email:
43
43
  - developers@katalyst.com.au
44
44
  executables: []
@@ -53,7 +53,7 @@ licenses:
53
53
  - MIT
54
54
  metadata:
55
55
  rubygems_mfa_required: 'true'
56
- post_install_message:
56
+ post_install_message:
57
57
  rdoc_options: []
58
58
  require_paths:
59
59
  - lib
@@ -68,8 +68,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
70
  requirements: []
71
- rubygems_version: 3.4.10
72
- signing_key:
71
+ rubygems_version: 3.5.9
72
+ signing_key:
73
73
  specification_version: 4
74
74
  summary: HTML Attributes utilities for use with ViewComponents
75
75
  test_files: []