stimulus_helpers 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: dd7023b93ba3d485fbea28c5eee63938fab133d3a142d40494e0bf37b7216ab1
4
+ data.tar.gz: 61bd7b0845434ceffeece2f56560207c80f220646622abf207dde1fb79434cf8
5
+ SHA512:
6
+ metadata.gz: 2d5a7e5abb4d4263004243f8967f9d1f04cc1b360acd68ca13359286a79928f6ce8407dc08962325d96e2bf146dece12193187bd7dcc9aa83f6bc42916723ab0
7
+ data.tar.gz: 22852bd7e2403fe4acc820cd7626a22caf3ca834f310a17a9751f9c003f7d91b497ad8065b988781cdcdc77f06c127b882904c021831dfb556f5762bc665cf23
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/CHANGELOG.md ADDED
File without changes
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 stimulus_helpers.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,81 @@
1
+ # StimulusHelpers
2
+
3
+ [![StimulusHelpers](https://github.com/tomasc/stimulus_helpers/actions/workflows/ruby.yml/badge.svg)](https://github.com/tomasc/stimulus_helpers/actions/workflows/ruby.yml)
4
+
5
+ Helpers to build stimulus controller attributes for use in views and components.
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add stimulus_helpers
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install stimulus_helpers
16
+
17
+ ## Usage
18
+
19
+ Simply include the `StimulusHelpers` module:
20
+
21
+ ```ruby
22
+ class MyClass
23
+ include StimulusHelpers
24
+ end
25
+
26
+ # or
27
+
28
+ ActionView::Base.send :include, StimulusHelpers
29
+
30
+ # etc.
31
+ ```
32
+
33
+ This will add the following helpers:
34
+
35
+ ```ruby
36
+ stimulus_controller("component")
37
+ # => { "controller" => "component" }
38
+
39
+ stimulus_action("component", "click", "open")
40
+ # => { "action" => "click->component#open" }
41
+
42
+ stimulus_actions("component", click: "open", blur: "close")
43
+ # => { "action" => "click->component#open blur->component#close" }
44
+
45
+ stimulus_class("component", "open", "component--open")
46
+ # => { "component-open-class" => "component--open" }
47
+
48
+ stimulus_classes("component", open: "component--open", closed: "component--closed")
49
+ # => { "component-open-class" => "component--open", "component-closed-class" => "component--closed" }
50
+
51
+ stimulus_value("component", "open", true)
52
+ # => { "component-open-value" => "true" }
53
+
54
+ stimulus_values("component", user: { name: "Jens" }, names: ["foo", "bar"])
55
+ # => { "component-user-value" => "{\"name\":\"Jens\"}", "component-names-value" => "[\"foo\",\"bar\"]" }
56
+
57
+ stimulus_target("component", :input)
58
+ # => { "component-target" => "input" }
59
+
60
+ stimulus_param("component", "id", 123)
61
+ # => { "component-id-param" => "123" }
62
+
63
+ stimulus_params("component", id: 123, name: "Jens")
64
+ # => { "component-id-param" => "123", "component-name-param" => "Jens" }
65
+
66
+ stimulus_outlet("component", "result", ".result")
67
+ # => { "component-result-outlet" => ".result" }
68
+
69
+ stimulus_outlets("component", result: ".result", output: ".output")
70
+ # => { "component-result-outlet" => ".result", "component-output-outlet" => ".output" }
71
+ ```
72
+
73
+ ## Development
74
+
75
+ 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.
76
+
77
+ 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).
78
+
79
+ ## Contributing
80
+
81
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/stimulus_helpers.
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
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 StimulusHelpers
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "stimulus_helpers/version"
4
+ require "active_support/core_ext/string/inflections"
5
+ require "json"
6
+
7
+ module StimulusHelpers
8
+ # @see https://blog.saeloun.com/2021/09/28/ruby-allow-value-omission-in-hash-literals
9
+ def stimulus_controller(controller)
10
+ { controller: }
11
+ end
12
+
13
+ def stimulus_action(controller, action, listener)
14
+ build_stimulus_action(controller:, actions: { action => listener })
15
+ end
16
+
17
+ def stimulus_actions(controller, actions = {})
18
+ build_stimulus_action(controller:, actions:)
19
+ end
20
+
21
+ def stimulus_class(controller, name, value)
22
+ build_stimulus_attribute(
23
+ controller:,
24
+ attributes: { name => value },
25
+ type: "class"
26
+ )
27
+ end
28
+
29
+ def stimulus_classes(controller, classes = {})
30
+ build_stimulus_attribute(
31
+ controller:,
32
+ attributes: classes,
33
+ type: "class"
34
+ )
35
+ end
36
+
37
+ def stimulus_value(controller, name, value)
38
+ build_stimulus_attribute(
39
+ controller:,
40
+ attributes: { name => value },
41
+ type: "value"
42
+ )
43
+ end
44
+
45
+ def stimulus_values(controller, values = {})
46
+ build_stimulus_attribute(
47
+ controller:,
48
+ attributes: values,
49
+ type: "value"
50
+ )
51
+ end
52
+
53
+ def stimulus_target(controller, target)
54
+ build_stimulus_attribute(
55
+ controller:,
56
+ attributes: { target: }
57
+ )
58
+ end
59
+
60
+ def stimulus_param(controller, name, value)
61
+ build_stimulus_attribute(
62
+ controller:,
63
+ attributes: { name => value },
64
+ type: "param"
65
+ )
66
+ end
67
+
68
+ def stimulus_params(controller, params = {})
69
+ build_stimulus_attribute(
70
+ controller:,
71
+ attributes: params,
72
+ type: "param"
73
+ )
74
+ end
75
+
76
+ def stimulus_outlet(controller, name, value)
77
+ build_stimulus_attribute(
78
+ controller:,
79
+ attributes: { name => value },
80
+ type: "outlet"
81
+ )
82
+ end
83
+
84
+ def stimulus_outlets(controller, outlets = {})
85
+ build_stimulus_attribute(
86
+ controller:,
87
+ attributes: outlets,
88
+ type: "outlet"
89
+ )
90
+ end
91
+
92
+ private
93
+ def build_stimulus_action(controller:, actions: {})
94
+ action = actions.map do |action, listeners|
95
+ Array(listeners).map do |listener|
96
+ "#{action}->#{controller}##{listener}"
97
+ end
98
+ end.join(" ")
99
+ { action: }
100
+ end
101
+
102
+ def build_stimulus_attribute(controller:, type: nil, attributes: {})
103
+ attributes.transform_keys! { |key| "#{key}-#{type}" } if type
104
+ attributes.each_with_object({}) do |(key, value), res|
105
+ value = value.to_json if value.is_a?(Array) || value.is_a?(Hash)
106
+ res["#{controller}-#{key.to_s.dasherize}"] = value.to_s
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,4 @@
1
+ module StimulusHelpers
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/stimulus_helpers/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "stimulus_helpers"
7
+ spec.version = StimulusHelpers::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 stimulus controllers."
12
+ spec.description = "Helper methods for stimulus controllers."
13
+ spec.homepage = "https://github.com/tomasc/stimulus_helpers"
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/stimulus_helpers/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 "activesupport"
30
+
31
+ spec.add_development_dependency "bundler"
32
+ spec.add_development_dependency "rake"
33
+ spec.add_development_dependency "minitest", "~> 5.0"
34
+
35
+ spec.add_development_dependency "lefthook"
36
+ spec.add_development_dependency "rubocop-rails_config"
37
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stimulus_helpers
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: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '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'
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
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: minitest
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '5.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '5.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: lefthook
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rubocop-rails_config
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
+ description: Helper methods for stimulus controllers.
99
+ email:
100
+ - tomas.celizna@gmail.com
101
+ - a@asgerbehnckejacobsen.dk
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".rubocop.yml"
107
+ - ".ruby-version"
108
+ - CHANGELOG.md
109
+ - Gemfile
110
+ - LICENSE
111
+ - README.md
112
+ - Rakefile
113
+ - lefthook.yml
114
+ - lib/stimulus_helpers.rb
115
+ - lib/stimulus_helpers/version.rb
116
+ - sig/stimulus_helpers.rbs
117
+ - stimulus_helpers.gemspec
118
+ homepage: https://github.com/tomasc/stimulus_helpers
119
+ licenses: []
120
+ metadata:
121
+ homepage_uri: https://github.com/tomasc/stimulus_helpers
122
+ source_code_uri: https://github.com/tomasc/stimulus_helpers
123
+ changelog_uri: https://github.com/tomasc/stimulus_helpers/CHANGELOG.md
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: 3.1.0
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubygems_version: 3.3.7
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Helper methods for stimulus controllers.
143
+ test_files: []