stimulus_tag_helper 0.2.0 → 0.3.1
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 +4 -4
- data/LICENSE.txt +1 -1
- data/lib/stimulus_tag_helper/stimulus_controller_builder.rb +38 -12
- data/lib/stimulus_tag_helper/version.rb +1 -1
- data/lib/stimulus_tag_helper.rb +36 -28
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8ff2c2c234e2e424625b92771a01e0d91a0e9022f197c95a2db0f567fa030b40
|
4
|
+
data.tar.gz: 346032a31e1beb29637638ebac298294b06e5ff8e6e0e82df1245a3da1c73036
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 28d2e85611dafc3d1f2e299f9db2662fa14a178d11a0b8f96930a7e50b037a022c468da97a98ced7320e234f3e129d0a4bdc43de9f741bc93406283f6919f6a4
|
7
|
+
data.tar.gz: 9e213aa8a8789b0dd92f05a6e248500f019c34273b7de5397547b5493c2a9656ca288ab8e5a37608c3e80c49c289deca6af8eb1ab05bef99c58280e0b41ae61b
|
data/LICENSE.txt
CHANGED
@@ -2,30 +2,56 @@
|
|
2
2
|
|
3
3
|
module StimulusTagHelper
|
4
4
|
class StimulusControllerBuilder
|
5
|
-
|
5
|
+
class << self
|
6
|
+
delegate :properties, :aliases, :attribute_method_name_for, :property_method_name_for, to: StimulusTagHelper
|
7
|
+
end
|
8
|
+
|
9
|
+
attr_reader :identifier, :template, :tag, :tag_options
|
10
|
+
|
11
|
+
# all the options are tag options of now
|
12
|
+
def initialize(identifier:, template:, tag: nil, **tag_options)
|
6
13
|
@identifier = identifier
|
7
14
|
@template = template
|
15
|
+
@tag = tag
|
16
|
+
@tag_options = tag_options
|
8
17
|
end
|
9
18
|
|
10
|
-
|
11
|
-
|
12
|
-
|
19
|
+
properties.each do |name|
|
20
|
+
attribute_method_name = attribute_method_name_for(name)
|
21
|
+
property_method_name = property_method_name_for(name)
|
22
|
+
alias_name = aliases[name]
|
13
23
|
|
14
24
|
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
15
|
-
def #{name}
|
16
|
-
|
17
|
-
end
|
25
|
+
def #{name}(*args, **kwargs) # def values(*args, **kwargs)
|
26
|
+
template.stimulus_#{attribute_method_name}(*args.unshift(identifier), **kwargs) # template.stimulus_values_attributes(*args.unshift(identifier), **kwargs)
|
27
|
+
end # end
|
18
28
|
|
19
|
-
|
29
|
+
alias_method :#{alias_name}, :#{name} # alias_method :value, :values
|
30
|
+
alias_method :#{attribute_method_name}, :#{name} # alias_method :values_attributes, :values
|
31
|
+
alias_method :#{alias_name}_attribute, :#{name} # alias_method :value_attribute, :values
|
20
32
|
|
21
|
-
def #{
|
22
|
-
|
23
|
-
end
|
33
|
+
def #{property_method_name}(*args, **kwargs) # def values_properties(*args, **kwargs)
|
34
|
+
template.stimulus_#{property_method_name}(*args.unshift(identifier), **kwargs) # template.stimulus_values_properties(*args.unshift(identifier), **kwargs)
|
35
|
+
end # end
|
36
|
+
|
37
|
+
alias_method :#{alias_name}_property, :#{property_method_name} # alias_method :value_property, :values_properties
|
24
38
|
RUBY
|
25
39
|
end
|
26
40
|
|
27
41
|
def attributes(**attributes)
|
28
|
-
|
42
|
+
template.stimulus_attributes(identifier, **attributes)
|
43
|
+
end
|
44
|
+
|
45
|
+
def properties(**properties)
|
46
|
+
template.stimulus_properties(identifier, **properties)
|
47
|
+
end
|
48
|
+
|
49
|
+
def capture(&block)
|
50
|
+
return template.capture(self, &block) unless tag
|
51
|
+
|
52
|
+
template.stimulus_controller_tag(identifier, tag: tag, **tag_options) do
|
53
|
+
template.capture(self, &block)
|
54
|
+
end
|
29
55
|
end
|
30
56
|
end
|
31
57
|
end
|
data/lib/stimulus_tag_helper.rb
CHANGED
@@ -5,25 +5,34 @@ require "zeitwerk"
|
|
5
5
|
Zeitwerk::Loader.for_gem.setup
|
6
6
|
|
7
7
|
module StimulusTagHelper
|
8
|
-
def self.
|
9
|
-
%i[
|
8
|
+
def self.properties
|
9
|
+
%i[controllers values classes targets actions].freeze
|
10
10
|
end
|
11
11
|
|
12
12
|
def self.aliases
|
13
|
-
|
13
|
+
@aliases ||=
|
14
|
+
properties.map { |property| [property.to_s.singularize.to_sym, property] }.to_h.tap do |aliases|
|
15
|
+
aliases.dup.each { |key, value| aliases[value] = key }
|
16
|
+
end.freeze
|
14
17
|
end
|
15
18
|
|
16
|
-
def self.
|
17
|
-
|
18
|
-
classes: :stimulus_classes_properties, targets: :stimulus_targets_properties, actions: :stimulus_actions_property}.freeze
|
19
|
+
def self.all_possible_properties_names
|
20
|
+
@all_possible_properties_names ||= aliases.keys
|
19
21
|
end
|
20
22
|
|
21
|
-
def self.
|
22
|
-
|
23
|
+
def self.property_method_name_for(property)
|
24
|
+
property = aliases[property] if properties.exclude?(property)
|
25
|
+
"#{property}_#{rendered_as(property) == :one ? "property" : "properties"}"
|
23
26
|
end
|
24
27
|
|
25
|
-
def self.
|
26
|
-
|
28
|
+
def self.attribute_method_name_for(property)
|
29
|
+
property = aliases[property] if properties.exclude?(property)
|
30
|
+
"#{property}_#{rendered_as(property) == :one ? "attribute" : "attributes"}"
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.rendered_as(property)
|
34
|
+
{controllers: :one, values: :many, classes: :many, targets: :many, actions: :one} \
|
35
|
+
[property] || raise(ArgumentError, "Unknown property: #{property.inspect}")
|
27
36
|
end
|
28
37
|
|
29
38
|
def self.prevent_duplicates(properties_set, name)
|
@@ -35,16 +44,10 @@ module StimulusTagHelper
|
|
35
44
|
STRING
|
36
45
|
end
|
37
46
|
|
38
|
-
def stimulus_controller(identifier, tag: nil, **
|
39
|
-
|
40
|
-
|
41
|
-
builder
|
42
|
-
|
43
|
-
return capture(builder, &block) unless tag
|
44
|
-
|
45
|
-
stimulus_controller_tag(identifier, tag: tag, **args) do
|
46
|
-
capture(builder, &block)
|
47
|
-
end
|
47
|
+
def stimulus_controller(identifier, tag: nil, **options, &block)
|
48
|
+
builder = StimulusControllerBuilder.new(identifier: identifier, tag: tag, template: self, **options)
|
49
|
+
return builder unless block
|
50
|
+
builder.capture(&block)
|
48
51
|
end
|
49
52
|
|
50
53
|
# controller arg is used to add additional stimulus controllers to the element
|
@@ -56,42 +59,41 @@ module StimulusTagHelper
|
|
56
59
|
data.merge!(stimulus_properties(
|
57
60
|
identifier,
|
58
61
|
controller: data[:controller] || controller, # nil is allowed, because the args will be prepended by the identifier
|
59
|
-
**args.extract!(*StimulusTagHelper.
|
62
|
+
**args.extract!(*StimulusTagHelper.all_possible_properties_names - %i[class])
|
60
63
|
))
|
61
64
|
tag_builder.tag_string(tag, **args.merge(data: data), &block)
|
62
65
|
end
|
63
66
|
|
64
|
-
# Does not includes the controller attribute
|
65
67
|
def stimulus_attributes(...)
|
66
68
|
{data: stimulus_properties(...)}
|
67
69
|
end
|
68
70
|
|
69
71
|
alias_method :stimulus_attribute, :stimulus_attributes
|
70
72
|
|
71
|
-
# Does not includes the controller property
|
72
73
|
def stimulus_properties(identifier, **props)
|
74
|
+
props = props.symbolize_keys
|
73
75
|
{}.tap do |data|
|
74
|
-
StimulusTagHelper.
|
76
|
+
StimulusTagHelper.all_possible_properties_names.each do |name|
|
75
77
|
next unless props.key?(name)
|
76
78
|
|
77
79
|
args = Array.wrap(props[name]).unshift(identifier)
|
78
80
|
kwargs = args.last.is_a?(Hash) ? args.pop : {}
|
79
81
|
StimulusTagHelper.prevent_duplicates(props, name)
|
80
82
|
data.merge!(
|
81
|
-
public_send(StimulusTagHelper.
|
83
|
+
public_send("stimulus_#{StimulusTagHelper.property_method_name_for(name)}", *args, **kwargs)
|
82
84
|
)
|
83
85
|
end
|
84
86
|
end
|
85
87
|
end
|
86
88
|
|
87
89
|
def stimulus_controllers_attribute(...)
|
88
|
-
{data:
|
90
|
+
{data: stimulus_controllers_property(...)}
|
89
91
|
end
|
90
92
|
|
91
93
|
alias_method :stimulus_controller_attribute, :stimulus_controllers_attribute
|
92
94
|
|
93
95
|
def stimulus_controllers_property(*identifiers)
|
94
|
-
{controller:
|
96
|
+
{controller: identifiers.flatten.compact.uniq.join(" ")}
|
95
97
|
end
|
96
98
|
|
97
99
|
alias_method :stimulus_controller_property, :stimulus_controllers_property
|
@@ -142,12 +144,18 @@ module StimulusTagHelper
|
|
142
144
|
|
143
145
|
def stimulus_actions_property(identifier, *actions_params)
|
144
146
|
{
|
145
|
-
action: actions_params.map { |action_params| stimulus_action_value(identifier, action_params) }.join(" ").html_safe
|
147
|
+
action: actions_params.flatten.map { |action_params| stimulus_action_value(identifier, action_params) }.join(" ").html_safe
|
146
148
|
}
|
147
149
|
end
|
148
150
|
|
149
151
|
alias_method :stimulus_action_property, :stimulus_actions_property
|
150
152
|
|
153
|
+
def stimulus_actions_attribute(...)
|
154
|
+
{data: stimulus_actions_property(...)}
|
155
|
+
end
|
156
|
+
|
157
|
+
alias_method :stimulus_action_attribute, :stimulus_actions_attribute
|
158
|
+
|
151
159
|
def stimulus_action_value(identifier, args_or_string) # :nodoc:
|
152
160
|
return StimulusAction.parse(args_or_string, identifier: identifier) if args_or_string.is_a?(String)
|
153
161
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stimulus_tag_helper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anton Topchii
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-07-
|
11
|
+
date: 2022-07-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|