has_stimulus_attrs 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e702f304344f1da5c028901abba68b02aec77c575cef53dedc213aa5b684c68d
4
+ data.tar.gz: 15d9bd70b5bf3a905a8b7a4a32f685763e0f57a0a7c38f340d989aaf6191f28a
5
+ SHA512:
6
+ metadata.gz: 7fb5adfb5f24e1434260cb128ed8055238498e93a21ae97d94a4ebfffab95dab23646db78cadec1598482ab3f5b5db7a2e9c59181cbaeab7a8f95dd3d6deb1bd
7
+ data.tar.gz: 598f5ad7b233c3ba368d897fa8fa4a57ca9601f790c9afe843e5df625b432c1d0bfeb8a371843f1c760fdacaae600b1fbc84b6de1ac8816ca83b4327c7d9452f
data/.rubocop.yml ADDED
@@ -0,0 +1,6 @@
1
+ inherit_gem:
2
+ rubocop-rails_config:
3
+ - config/rails.yml
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 3.1
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in has_stimulus_attrs.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Tomáš Celizna
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # HasStimulusAttrs
2
+
3
+ [![HasStimulusAttrs](https://github.com/tomasc/has_stimulus_attrs/actions/workflows/ruby.yml/badge.svg)](https://github.com/tomasc/has_stimulus_attrs/actions/workflows/ruby.yml)
4
+
5
+ Helper methods for dealing with [stimulus](https://stimulus.hotwired.dev/) data attributes.
6
+
7
+ Relies on [`has_dom_attrs`](github.com/tomasc/has_dom_attrs) and [`stimulus_helpers`](github.com/tomasc/stimulus_helpers).
8
+
9
+ ## Installation
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add has_stimulus_attrs
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install has_stimulus_attrs
18
+
19
+ ## Usage
20
+
21
+ Include `HasStimulusAttrs` in your class and define a `controller_name` class method:
22
+
23
+ ```ruby
24
+ class ModalComponent
25
+ include HasStimulusAttrs
26
+
27
+ def self.controller_name
28
+ "modal-component"
29
+ end
30
+ end
31
+ ```
32
+
33
+ `controller_name` can also be defined in a base class and dynamically resolved:
34
+
35
+ ```ruby
36
+ class ApplicationComponent
37
+ include HasStimulusAttrs
38
+
39
+ def self.controller_name
40
+ self.name.underscore.dasherize
41
+ end
42
+ end
43
+
44
+ class DetailsComponent < ApplicationComponent
45
+ end
46
+
47
+ DetailsComponent.new.controller_name
48
+ # => "details-component"
49
+ ```
50
+
51
+ You can then use the included class methods to easily set stimulus attributes on
52
+ your class:
53
+
54
+ ```ruby
55
+ class ModalComponent < ApplicationComponent
56
+ has_stimulus_controller # sets the "controller" data attribute using :controller_name by default
57
+ has_stimulus_controller "click-outside"
58
+ has_stimulus_controller "scroll-lock", if: :open? # conditionally add the controller
59
+ has_stimulus_controller "scroll-lock", unless: :closed? # conditionally add the controller
60
+
61
+ has_stimulus_action "click", "onClick"
62
+ has_stimulus_action "click", "onClick", if: :open?
63
+
64
+ has_stimulus_class "open", "modal--open"
65
+ has_stimulus_class "width", -> { "modal--#{width}" } # resolve the class name dynamically
66
+
67
+ has_stimulus_outlet "outlet", ".selector"
68
+
69
+ has_stimulus_param id: 123
70
+ has_stimulus_param id: -> { id }
71
+
72
+ has_stimulus_target "target"
73
+ has_stimulus_target "target", controller: "other-controller"
74
+
75
+ has_stimulus_value "id", 123
76
+ has_stimulus_value "id", -> { id }
77
+ has_stimulus_value "id", -> { id }, controller: "other-controller"
78
+ end
79
+ ```
80
+
81
+ ## Development
82
+
83
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
84
+
85
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
86
+
87
+ ## Contributing
88
+
89
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/has_stimulus_attrs.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ task default: :test
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/has_stimulus_attrs/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "has_stimulus_attrs"
7
+ spec.version = HasStimulusAttrs::VERSION
8
+ spec.authors = ["Tomas Celizna", "Asger Behncke Jacobsen"]
9
+ spec.email = ["tomas.celizna@gmail.com", "a@asgerbehnckejacobsen.dk"]
10
+
11
+ spec.summary = "Helper methods for dealing with stimulus attributes."
12
+ spec.description = "Helper methods for dealing with stimulus attributes."
13
+ spec.homepage = "https://github.com/tomasc/has_stimulus_attrs"
14
+ spec.required_ruby_version = ">= 3.1.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+ spec.metadata["changelog_uri"] = "https://github.com/tomasc/has_stimulus_attrs/CHANGELOG.md"
19
+
20
+ spec.files = Dir.chdir(__dir__) do
21
+ `git ls-files -z`.split("\x0").reject do |f|
22
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
23
+ end
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_dependency "stimulus_helpers", "~> 0.1.0"
30
+ spec.add_dependency "has_dom_attrs", "~> 0.1.0"
31
+
32
+ spec.add_development_dependency "bundler"
33
+ spec.add_development_dependency "rake"
34
+ spec.add_development_dependency "minitest", "~> 5.0"
35
+
36
+ spec.add_development_dependency "lefthook"
37
+ spec.add_development_dependency "rubocop-rails_config"
38
+ end
data/lefthook.yml ADDED
@@ -0,0 +1,5 @@
1
+ pre-commit:
2
+ parallel: true
3
+ commands:
4
+ rubocop:
5
+ run: bundle exec rubocop {staged_files} -A --display-cop-names --extra-details --force-exclusion
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HasStimulusAttrs
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "has_stimulus_attrs/version"
4
+
5
+ require "has_dom_attrs"
6
+ require "stimulus_helpers"
7
+
8
+ module HasStimulusAttrs
9
+ include HasDomAttrs
10
+ include StimulusHelpers
11
+
12
+ class << self
13
+ def included(base)
14
+ base.extend ClassMethods
15
+ end
16
+ end
17
+
18
+ module ClassMethods
19
+ def controller_name
20
+ raise NotImplementedError
21
+ end
22
+
23
+ def has_stimulus_controller(name = controller_name, **options)
24
+ key = :controller
25
+ val = name
26
+
27
+ prepend___has_stimulus___method(key, val, **options)
28
+ end
29
+
30
+ def has_stimulus_action(event, action, controller: nil, **options)
31
+ key = :action
32
+ val = -> { stimulus_action((controller || controller_name), event, action).values.first }
33
+
34
+ prepend___has_stimulus___method(key, val, **options)
35
+ end
36
+
37
+ def has_stimulus_class(name, value, controller: nil, **options)
38
+ key = -> { stimulus_class((controller || controller_name), name, "N/A").keys.first }
39
+ val = -> {
40
+ v = case value
41
+ when Proc then instance_exec(&value)
42
+ else value.to_s
43
+ end
44
+ stimulus_class((controller || controller_name), name, v).values.first
45
+ }
46
+
47
+ prepend___has_stimulus___method(key, val, **options)
48
+ end
49
+
50
+ def has_stimulus_outlet(name, value, controller: nil, **options)
51
+ key = -> { stimulus_outlet((controller || controller_name), name, "N/A").keys.first }
52
+ val = -> {
53
+ v = case value
54
+ when Proc then instance_exec(&value)
55
+ when Symbol then send(value)
56
+ else value
57
+ end
58
+ stimulus_outlet((controller || controller_name), name, v).values.first
59
+ }
60
+
61
+ prepend___has_stimulus___method(key, val, **options)
62
+ end
63
+
64
+ def has_stimulus_param(name, value, controller: nil, **options)
65
+ key = -> { stimulus_param((controller || controller_name), name, "N/A").keys.first }
66
+ val = -> {
67
+ v = case value
68
+ when Proc then instance_exec(&value)
69
+ when Symbol then send(value)
70
+ else value
71
+ end
72
+ stimulus_param((controller || controller_name), name, v).values.first
73
+ }
74
+
75
+ prepend___has_stimulus___method(key, val, **options)
76
+ end
77
+
78
+ def has_stimulus_target(name, controller: nil, **options)
79
+ key = -> { stimulus_target((controller || controller_name), name).keys.first }
80
+ val = -> { stimulus_target((controller || controller_name), name).values.first }
81
+
82
+ prepend___has_stimulus___method(key, val, **options)
83
+ end
84
+
85
+ def has_stimulus_value(name, value = nil, controller: nil, **options)
86
+ key = -> { stimulus_value((controller || controller_name), name, "N/A").keys.first }
87
+ val = -> {
88
+ v = case value
89
+ when Proc then instance_exec(&value)
90
+ when Symbol then send(value)
91
+ when NilClass then send(name)
92
+ else value
93
+ end
94
+ stimulus_value((controller || controller_name), name, v).values.first
95
+ }
96
+
97
+ prepend___has_stimulus___method(key, val, **options)
98
+ end
99
+
100
+ private
101
+ def prepend___has_stimulus___method(key, value, **options)
102
+ prepend(
103
+ Module.new do
104
+ define_method :dom_data do
105
+ cond = options[:if] || options[:unless]
106
+ cond_value = case cond
107
+ when Proc then instance_exec(&cond)
108
+ when Symbol, String then send(cond)
109
+ end
110
+
111
+ if cond && options.key?(:if)
112
+ return super() unless cond_value
113
+ end
114
+
115
+ if cond && options.key?(:unless)
116
+ return super() if cond_value
117
+ end
118
+
119
+ k = case key
120
+ when Proc then instance_exec(&key)
121
+ else key
122
+ end
123
+
124
+ v = case value
125
+ when Proc then instance_exec(&value)
126
+ else value
127
+ end
128
+
129
+ super().tap do |data|
130
+ data[k] = [data[k], v].reject(&:blank?).uniq.join(" ")
131
+ end
132
+ end
133
+ end
134
+ )
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,4 @@
1
+ module HasStimulusAttrs
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_stimulus_attrs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tomas Celizna
8
+ - Asger Behncke Jacobsen
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2023-03-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: stimulus_helpers
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 0.1.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 0.1.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: has_dom_attrs
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: 0.1.0
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: 0.1.0
42
+ - !ruby/object:Gem::Dependency
43
+ name: bundler
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rake
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: minitest
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '5.0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '5.0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: lefthook
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: rubocop-rails_config
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ description: Helper methods for dealing with stimulus attributes.
113
+ email:
114
+ - tomas.celizna@gmail.com
115
+ - a@asgerbehnckejacobsen.dk
116
+ executables: []
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".rubocop.yml"
121
+ - ".ruby-version"
122
+ - Gemfile
123
+ - LICENSE
124
+ - README.md
125
+ - Rakefile
126
+ - has_stimulus_attrs.gemspec
127
+ - lefthook.yml
128
+ - lib/has_stimulus_attrs.rb
129
+ - lib/has_stimulus_attrs/version.rb
130
+ - sig/has_stimulus_attrs.rbs
131
+ homepage: https://github.com/tomasc/has_stimulus_attrs
132
+ licenses: []
133
+ metadata:
134
+ homepage_uri: https://github.com/tomasc/has_stimulus_attrs
135
+ source_code_uri: https://github.com/tomasc/has_stimulus_attrs
136
+ changelog_uri: https://github.com/tomasc/has_stimulus_attrs/CHANGELOG.md
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: 3.1.0
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubygems_version: 3.3.7
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: Helper methods for dealing with stimulus attributes.
156
+ test_files: []