stimulus_tag_helper 0.2.0 → 0.3.1

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: a65ec6e3ca5fbdb0773a81dd380c4ca366e3de6dc30f5e00d45c8d7371c03a25
4
- data.tar.gz: d0593dc01c202aa3de8364b7e48eb97d6cb27a8cd5b1435442756fa7e46683ce
3
+ metadata.gz: 8ff2c2c234e2e424625b92771a01e0d91a0e9022f197c95a2db0f567fa030b40
4
+ data.tar.gz: 346032a31e1beb29637638ebac298294b06e5ff8e6e0e82df1245a3da1c73036
5
5
  SHA512:
6
- metadata.gz: 5e15a8b9cbc3f422577d967807f7b22fe1c4f9d9c6b947f26bc0ac678a422e0088b4c584e25bf2c2959ad97e7d549cf235b8c8e54c58a2baf5f445cfd0d7d88b
7
- data.tar.gz: b2837afeee0dac24ad6abe6ff12cebae2c3ba1510bb126f447eaf30383317148d42ddeba86ab707dc6823e0fccf40417547d2cdf6cb054fb5a0083752d6721a4
6
+ metadata.gz: 28d2e85611dafc3d1f2e299f9db2662fa14a178d11a0b8f96930a7e50b037a022c468da97a98ced7320e234f3e129d0a4bdc43de9f741bc93406283f6919f6a4
7
+ data.tar.gz: 9e213aa8a8789b0dd92f05a6e248500f019c34273b7de5397547b5493c2a9656ca288ab8e5a37608c3e80c49c289deca6af8eb1ab05bef99c58280e0b41ae61b
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2021 Anton Topchii
3
+ Copyright (c) 2022 Anton Topchii
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -2,30 +2,56 @@
2
2
 
3
3
  module StimulusTagHelper
4
4
  class StimulusControllerBuilder
5
- def initialize(identifier:, template:)
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
- StimulusTagHelper.property_names.each do |name|
11
- name = name.to_s
12
- attribute, property = name.pluralize == name ? %w[attributes properties] : %w[attribute property]
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}_#{attribute}(...) # def value_attribute(...)
16
- { data: #{name}_#{property}(...) } # { data: value_property(...) }
17
- end # 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
- alias #{name} #{name}_#{attribute} # alias value value_attribute
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 #{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
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
- @template.stimulus_attributes(@identifier, **attributes)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StimulusTagHelper
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.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)
@@ -35,16 +44,10 @@ module StimulusTagHelper
35
44
  STRING
36
45
  end
37
46
 
38
- def stimulus_controller(identifier, tag: nil, **args, &block)
39
- raise(ArgumentError, "Missing block") unless block
40
-
41
- builder = StimulusControllerBuilder.new(identifier: identifier, template: self)
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.property_names - %i[class])
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.property_names.each do |name|
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.methods_map[StimulusTagHelper.aliases[name] || name], *args, **kwargs)
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: stimulus_controllers_properties(...)}
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: Array.wrap(identifiers).compact.join(" ")}
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.2.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 00:00:00.000000000 Z
11
+ date: 2022-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails