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: 31c234dcff3bfb134e7f02ed13f084dd712f787ae855ef4f02ba2d2e58b0eed7
4
+ data.tar.gz: afe2cc728ae77855ed37487d00d46b96a9819f582bbac00b439e1e5eb2c47d11
5
+ SHA512:
6
+ metadata.gz: 9e0f166fc3375af1f43102f8ef1fee6bd21401b0c8d001e78acbda9b9371f9c4e784e7f0315e56443b8e9f44e554e89e945c6c09e34d5bd348c3c4501535f626
7
+ data.tar.gz: 3627534f617998ce45d75559d7635d3001c5d24532810d7ccdca0f903096dc202a8d568d7196a5300b42728a70488695ccf27e1d3d43a42cea678a9dd5bc7760
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 2.6
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in stimulus_attrs.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+
10
+ gem 'minitest', '~> 5.0'
11
+
12
+ group :test do
13
+ gem 'actionview', '>= 6.0.0'
14
+ gem 'activesupport', '>= 6.0.0'
15
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,70 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ stimulus_attrs (0.1.0)
5
+ activesupport (>= 6.0.0)
6
+ html_attrs (>= 1.1.2)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionview (7.1.3.2)
12
+ activesupport (= 7.1.3.2)
13
+ builder (~> 3.1)
14
+ erubi (~> 1.11)
15
+ rails-dom-testing (~> 2.2)
16
+ rails-html-sanitizer (~> 1.6)
17
+ activesupport (7.1.3.2)
18
+ base64
19
+ bigdecimal
20
+ concurrent-ruby (~> 1.0, >= 1.0.2)
21
+ connection_pool (>= 2.2.5)
22
+ drb
23
+ i18n (>= 1.6, < 2)
24
+ minitest (>= 5.1)
25
+ mutex_m
26
+ tzinfo (~> 2.0)
27
+ base64 (0.2.0)
28
+ bigdecimal (3.1.7)
29
+ builder (3.2.4)
30
+ concurrent-ruby (1.2.3)
31
+ connection_pool (2.4.1)
32
+ crass (1.0.6)
33
+ drb (2.2.1)
34
+ erubi (1.12.0)
35
+ html_attrs (1.1.2)
36
+ actionview (>= 6.0)
37
+ activesupport (>= 6.0)
38
+ i18n (1.14.4)
39
+ concurrent-ruby (~> 1.0)
40
+ loofah (2.22.0)
41
+ crass (~> 1.0.2)
42
+ nokogiri (>= 1.12.0)
43
+ minitest (5.22.3)
44
+ mutex_m (0.2.0)
45
+ nokogiri (1.16.3-arm64-darwin)
46
+ racc (~> 1.4)
47
+ racc (1.7.3)
48
+ rails-dom-testing (2.2.0)
49
+ activesupport (>= 5.0.0)
50
+ minitest
51
+ nokogiri (>= 1.6)
52
+ rails-html-sanitizer (1.6.0)
53
+ loofah (~> 2.21)
54
+ nokogiri (~> 1.14)
55
+ rake (13.1.0)
56
+ tzinfo (2.0.6)
57
+ concurrent-ruby (~> 1.0)
58
+
59
+ PLATFORMS
60
+ arm64-darwin-22
61
+
62
+ DEPENDENCIES
63
+ actionview (>= 6.0.0)
64
+ activesupport (>= 6.0.0)
65
+ minitest (~> 5.0)
66
+ rake (~> 13.0)
67
+ stimulus_attrs!
68
+
69
+ BUNDLED WITH
70
+ 2.4.13
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Owais
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # StimulusAttrs
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/stimulus_attrs`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ 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).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/owaiswiz/stimulus_attrs.
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ require "standard/rake"
13
+
14
+ task default: %i[test standard]
@@ -0,0 +1,18 @@
1
+ module StimulusAttrs::DefaultIdentifier
2
+ extend ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+ def identifier_for_stimulus_attrs
6
+ # Convert things like 'Admin::UserAvatarComponent' to 'admin--user-avatar'
7
+ @identifier_for_stimulus_attrs ||= name.gsub('::Component', '').gsub(/Component$/, '')
8
+ .underscore.gsub('/', '--').gsub('_', '-')
9
+ end
10
+ end
11
+
12
+ def identifier_for_stimulus_attrs
13
+ self.class.identifier_for_stimulus_attrs
14
+ end
15
+
16
+ ViewComponent::Base.include(self) if defined?(ViewComponent::Base)
17
+ Phlex::HTML.include(self) if defined?(Phlex::HTML)
18
+ end
@@ -0,0 +1,17 @@
1
+ module StimulusAttrs::Helpers
2
+ %i[controller target values classes actions params outlets].each do |method|
3
+ define_method("stimulus_#{method}") do |*args, **kwargs|
4
+ kwargs[:identifier] ||= identifier_for_stimulus_attrs if respond_to?(:identifier_for_stimulus_attrs)
5
+ if self.class.respond_to?(:identifier_for_stimulus_attrs)
6
+ kwargs[:identifier] ||= self.class.identifier_for_stimulus_attrs
7
+ end
8
+ StimulusAttrs.public_send(method, *args, **kwargs)
9
+ end
10
+ end
11
+
12
+ ActiveSupport.on_load(:action_view) do
13
+ include StimulusAttrs::Helpers
14
+ end
15
+ ViewComponent::Base.include(self) if defined?(ViewComponent::Base)
16
+ Phlex::HTML.include(self) if defined?(Phlex::HTML)
17
+ end
@@ -0,0 +1,13 @@
1
+ class StimulusAttrs::IdentifierScope
2
+ def initialize(identifier)
3
+ @identifier = identifier
4
+ end
5
+
6
+ def method_missing(*args, **kwargs, &block)
7
+ StimulusAttrs.send(*args, **kwargs, identifier: @identifier, &block)
8
+ end
9
+
10
+ def respond_to_missing?(*args)
11
+ StimulusAttrs.respond_to?(*args)
12
+ end
13
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support'
4
+ require 'active_support/notifications'
5
+ require 'active_support/concern'
6
+ require 'active_support/core_ext/string/inflections'
7
+ require 'html_attrs'
8
+
9
+ class StimulusAttrs < HtmlAttrs # rubocop:disable Style/Documentation
10
+ VERSION = '0.1.0'
11
+
12
+ require_relative 'stimulus_attrs/identifier_scope'
13
+ require_relative 'stimulus_attrs/helpers'
14
+ require_relative 'stimulus_attrs/default_identifier'
15
+
16
+ alias merge smart_merge
17
+
18
+ def self.controller(identifier_as_arg = nil, values: nil, classes: nil, actions: nil, params: nil, outlets: nil, identifier: nil)
19
+ identifier ||= identifier_as_arg
20
+ raise ArgumentError, 'identifier is required' unless identifier
21
+ controller_referenced(identifier)
22
+
23
+ result = new(data: { controller: identifier })
24
+ result = result.merge(self.values(**values, identifier: identifier)) if values
25
+ result = result.merge(self.classes(**classes, identifier: identifier)) if classes
26
+ result = result.merge(self.actions(**actions, identifier: identifier)) if actions
27
+ result = result.merge(self.params(**params, identifier: identifier)) if params
28
+ result = result.merge(self.outlets(**outlets, identifier: identifier)) if outlets
29
+
30
+ result
31
+ end
32
+
33
+ def self.target(target_name, identifier:)
34
+ new(data: { "#{identifier}-target" => target_name.to_s.underscore.camelcase(:lower) })
35
+ end
36
+
37
+ def self.values(identifier:, **values)
38
+ new(data: values.transform_keys { |k| "#{identifier}-#{k.to_s.underscore.dasherize}-value" })
39
+ end
40
+
41
+ def self.classes(identifier:, **classes)
42
+ new(data: classes.transform_keys { |k| "#{identifier}-#{k.to_s.underscore.dasherize}-class" })
43
+ end
44
+
45
+ # `descriptor` is what stimulus calls things that go inside data-action: https://stimulus.hotwired.dev/reference/actions#descriptors
46
+ def self.actions(descriptor = nil, identifier: nil, params: nil, **descriptors)
47
+ if descriptor
48
+ # Keep the default descriptors near the start of the action list
49
+ descriptors = { nil => [descriptor] + Array(descriptors.delete(nil)) }.merge(descriptors)
50
+ end
51
+
52
+ action = descriptors.map do |event, methods|
53
+ Array(methods).map do |method|
54
+ method = method.to_s
55
+ prepend_identifier = !method.include?('#')
56
+ raise ArgumentError, 'identifier is required' if prepend_identifier && !identifier
57
+
58
+ method = method.to_s.underscore.camelcase(:lower) if prepend_identifier
59
+ method = "#{identifier}##{method}" if prepend_identifier
60
+ event ? "#{event}->#{method}" : method
61
+ end
62
+ end.flatten.join(' ')
63
+
64
+ result = new(data: { action: action })
65
+ result = result.merge(self.params(**params, identifier: identifier)) if params.present?
66
+ result
67
+ end
68
+
69
+ def self.params(identifier:, **params)
70
+ new(data: params.transform_keys { |k| "#{identifier}-#{k.to_s.underscore.dasherize}-param" })
71
+ end
72
+
73
+ def self.outlets(identifier:, **outlets)
74
+ new(data: outlets.transform_keys { |k| "#{identifier}-#{k.to_s.underscore.dasherize}-outlet" })
75
+ end
76
+
77
+ def self.with_identifier(identifier, &block)
78
+ scope = IdentifierScope.new(identifier)
79
+ yield scope if block
80
+ scope
81
+ end
82
+
83
+ def self.controller_referenced(identifier)
84
+ return unless instrumentation_enabled?
85
+ ActiveSupport::Notifications.instrument('controller_referenced.stimulus_attrs', identifier: identifier)
86
+ end
87
+
88
+ def self.instrumentation_enabled?
89
+ !!@instrumentation_enabled
90
+ end
91
+
92
+ def self.instrumentation_enabled=(value)
93
+ @instrumentation_enabled = value
94
+ end
95
+ end
@@ -0,0 +1,4 @@
1
+ module StimulusAttrs
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stimulus_attrs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Owais
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-03-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 6.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 6.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: html_attrs
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.1.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.1.2
41
+ description: Helpers to simplify rendering Stimulus data attributes
42
+ email:
43
+ - owaiswiz@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".standard.yml"
49
+ - Gemfile
50
+ - Gemfile.lock
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - lib/stimulus_attrs.rb
55
+ - lib/stimulus_attrs/default_identifier.rb
56
+ - lib/stimulus_attrs/helpers.rb
57
+ - lib/stimulus_attrs/identifier_scope.rb
58
+ - sig/stimulus_attrs.rbs
59
+ homepage: https://github.com/owaiswiz/stimulus_attrs
60
+ licenses:
61
+ - MIT
62
+ metadata:
63
+ homepage_uri: https://github.com/owaiswiz/stimulus_attrs
64
+ source_code_uri: https://github.com/owaiswiz/stimulus_attrs
65
+ changelog_uri: https://github.com/owaiswiz/stimulus_attrs/releases
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 2.6.0
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubygems_version: 3.4.10
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Helpers to simplify rendering Stimulus data attributes
85
+ test_files: []