stimulus_tag_helper 0.2.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a65ec6e3ca5fbdb0773a81dd380c4ca366e3de6dc30f5e00d45c8d7371c03a25
4
- data.tar.gz: d0593dc01c202aa3de8364b7e48eb97d6cb27a8cd5b1435442756fa7e46683ce
3
+ metadata.gz: 999b55cbd4ceffe9c68cce73f072542ca117191cf8d816e4453f5da3c0373e3d
4
+ data.tar.gz: a9ccac5daddbf14cf5bf472ef17288da765b80d35c83d695d4972a40cf77fa95
5
5
  SHA512:
6
- metadata.gz: 5e15a8b9cbc3f422577d967807f7b22fe1c4f9d9c6b947f26bc0ac678a422e0088b4c584e25bf2c2959ad97e7d549cf235b8c8e54c58a2baf5f445cfd0d7d88b
7
- data.tar.gz: b2837afeee0dac24ad6abe6ff12cebae2c3ba1510bb126f447eaf30383317148d42ddeba86ab707dc6823e0fccf40417547d2cdf6cb054fb5a0083752d6721a4
6
+ metadata.gz: feb8293ffa622314a26e8a54237770768c126316e0f5558d44968abf9b7e209cca7aabba79c5de48971d0423dc68d3387f49716240d0c570768c89485461eb9e
7
+ data.tar.gz: d4cc16a147a1d9b1943b15bedf9ca2b75cd2057b0fa5e18794fbd8e191820c31d59e085675b536ed8e33f9071562f8459cc641fe226a7987155b9e1171e5c5f5
@@ -2,25 +2,34 @@
2
2
 
3
3
  module StimulusTagHelper
4
4
  class StimulusControllerBuilder
5
+ class << self
6
+ delegate :properties, :aliases, :attribute_method_name_for, :property_method_name_for, to: StimulusTagHelper
7
+ end
8
+
5
9
  def initialize(identifier:, template:)
6
10
  @identifier = identifier
7
11
  @template = template
8
12
  end
9
13
 
10
- StimulusTagHelper.property_names.each do |name|
11
- name = name.to_s
12
- attribute, property = name.pluralize == name ? %w[attributes properties] : %w[attribute property]
14
+ properties.each do |name|
15
+ attribute_method_name = attribute_method_name_for(name)
16
+ property_method_name = property_method_name_for(name)
17
+ alias_name = aliases[name]
13
18
 
14
19
  class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
15
- def #{name}_#{attribute}(...) # def value_attribute(...)
16
- { data: #{name}_#{property}(...) } # { data: value_property(...) }
17
- end # end
20
+ def #{name}(*args, **kwargs) # def values(*args, **kwargs)
21
+ @template.stimulus_#{attribute_method_name}(*args.unshift(@identifier), **kwargs) # @template.stimulus_values_attributes(*args.unshift(@identifier), **kwargs)
22
+ end # end
23
+
24
+ alias_method :#{alias_name}, :#{name} # alias_method :value, :values
25
+ alias_method :#{attribute_method_name}, :#{name} # alias_method :values_attributes, :values
26
+ alias_method :#{alias_name}_attribute, :#{name} # alias_method :value_attribute, :values
18
27
 
19
- alias #{name} #{name}_#{attribute} # alias value value_attribute
28
+ def #{property_method_name}(*args, **kwargs) # def values_properties(*args, **kwargs)
29
+ @template.stimulus_#{property_method_name}(*args.unshift(@identifier), **kwargs) # @template.stimulus_values_properties(*args.unshift(@identifier), **kwargs)
30
+ end # end
20
31
 
21
- def #{name}_#{property}(*args, **kwargs) # def value_property(*args, **kwargs)
22
- @template.stimulus_#{name}_#{property}(*args.unshift(@identifier), **kwargs) # stimulus_value_property(@identifier, *args, **kwargs)
23
- end # end
32
+ alias_method :#{alias_name}_property, :#{property_method_name} # alias_method :value_property, :values_properties
24
33
  RUBY
25
34
  end
26
35
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StimulusTagHelper
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.1"
5
5
  end
@@ -5,25 +5,34 @@ require "zeitwerk"
5
5
  Zeitwerk::Loader.for_gem.setup
6
6
 
7
7
  module StimulusTagHelper
8
- def self.base_properties
9
- %i[controller values classes targets target actions].freeze
8
+ def self.properties
9
+ %i[controllers values classes targets actions].freeze
10
10
  end
11
11
 
12
12
  def self.aliases
13
- {controller: :controllers, value: :values, class: :classes, target: :targets, action: :actions}.freeze
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.methods_map
17
- {controllers: :stimulus_controllers_property, values: :stimulus_values_properties,
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.alias_properties
22
- @alias_properties ||= aliases.keys
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.property_names
26
- @property_names ||= base_properties + alias_properties
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)
@@ -56,29 +65,28 @@ module StimulusTagHelper
56
65
  data.merge!(stimulus_properties(
57
66
  identifier,
58
67
  controller: data[:controller] || controller, # nil is allowed, because the args will be prepended by the identifier
59
- **args.extract!(*StimulusTagHelper.property_names - %i[class])
68
+ **args.extract!(*StimulusTagHelper.all_possible_properties_names - %i[class])
60
69
  ))
61
70
  tag_builder.tag_string(tag, **args.merge(data: data), &block)
62
71
  end
63
72
 
64
- # Does not includes the controller attribute
65
73
  def stimulus_attributes(...)
66
74
  {data: stimulus_properties(...)}
67
75
  end
68
76
 
69
77
  alias_method :stimulus_attribute, :stimulus_attributes
70
78
 
71
- # Does not includes the controller property
72
79
  def stimulus_properties(identifier, **props)
80
+ props = props.symbolize_keys
73
81
  {}.tap do |data|
74
- StimulusTagHelper.property_names.each do |name|
82
+ StimulusTagHelper.all_possible_properties_names.each do |name|
75
83
  next unless props.key?(name)
76
84
 
77
85
  args = Array.wrap(props[name]).unshift(identifier)
78
86
  kwargs = args.last.is_a?(Hash) ? args.pop : {}
79
87
  StimulusTagHelper.prevent_duplicates(props, name)
80
88
  data.merge!(
81
- public_send(StimulusTagHelper.methods_map[StimulusTagHelper.aliases[name] || name], *args, **kwargs)
89
+ public_send("stimulus_#{StimulusTagHelper.property_method_name_for(name)}", *args, **kwargs)
82
90
  )
83
91
  end
84
92
  end
@@ -91,7 +99,7 @@ module StimulusTagHelper
91
99
  alias_method :stimulus_controller_attribute, :stimulus_controllers_attribute
92
100
 
93
101
  def stimulus_controllers_property(*identifiers)
94
- {controller: Array.wrap(identifiers).compact.join(" ")}
102
+ {controller: identifiers.flatten.compact.uniq.join(" ")}
95
103
  end
96
104
 
97
105
  alias_method :stimulus_controller_property, :stimulus_controllers_property
@@ -142,12 +150,18 @@ module StimulusTagHelper
142
150
 
143
151
  def stimulus_actions_property(identifier, *actions_params)
144
152
  {
145
- action: actions_params.map { |action_params| stimulus_action_value(identifier, action_params) }.join(" ").html_safe
153
+ action: actions_params.flatten.map { |action_params| stimulus_action_value(identifier, action_params) }.join(" ").html_safe
146
154
  }
147
155
  end
148
156
 
149
157
  alias_method :stimulus_action_property, :stimulus_actions_property
150
158
 
159
+ def stimulus_actions_attribute(...)
160
+ {data: stimulus_actions_property(...)}
161
+ end
162
+
163
+ alias_method :stimulus_action_attribute, :stimulus_actions_attribute
164
+
151
165
  def stimulus_action_value(identifier, args_or_string) # :nodoc:
152
166
  return StimulusAction.parse(args_or_string, identifier: identifier) if args_or_string.is_a?(String)
153
167
 
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.2.0
4
+ version: 0.2.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 00:00:00.000000000 Z
11
+ date: 2022-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails