has_dom_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: 803cfad840b103564000efceae798dc50b5cbbf444e2ae63e84a09a1c311a4eb
4
+ data.tar.gz: '0093ccd0bebf0f0bd4c584304327bf5c67cec5bf76cdc89c41daaa915e2000c1'
5
+ SHA512:
6
+ metadata.gz: d23685a4f676cd252678c90be4e972f9a6235d87f52f8ff1b48551c24407a2975a483c97802df3e201e8a569dd70c23d9083ee2467d815695c539829748b199b
7
+ data.tar.gz: 896d9e5bd69c15ee1dee4986539bebe6e1ef4e512bd8828d47a715ce653abb3622669b8883c80ad3283bb356e1b246a716fdb35b1ce919ccc8a82c0b5c1f896e
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,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in has_dom_attrs.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
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,91 @@
1
+ # HasDomAttrs
2
+
3
+ [![HasDomAttrs](https://github.com/tomasc/has_dom_attrs/actions/workflows/ruby.yml/badge.svg)](https://github.com/tomasc/has_dom_attrs/actions/workflows/ruby.yml)
4
+
5
+ Helper methods for dealing with html element attributes.
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add has_dom_attrs
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install has_dom_attrs
16
+
17
+ ## Usage
18
+
19
+ Include `HasDomAttrs` in your class:
20
+
21
+ ```ruby
22
+ class Component
23
+ include HasDomAttrs
24
+ end
25
+ ```
26
+
27
+ This makes your component respond to `dom_attrs`, which returns a hash of class, data,
28
+ aria, and style attributes, that can be passed to Rails tag helpers.
29
+
30
+ ```ruby
31
+ component = Component.new
32
+ tag.div "Hello world", **component.dom_attrs
33
+ ```
34
+
35
+ You can define attributes using class methods:
36
+
37
+ ```ruby
38
+ class DetailsComponent
39
+ include HasDomAttrs
40
+
41
+ has_dom_attr :open
42
+
43
+ attr_reader :open
44
+
45
+ def initialize(open: false)
46
+ @open = open
47
+ end
48
+ end
49
+ ```
50
+
51
+ ```ruby
52
+ component = Component.new(open: true)
53
+ component.dom_attrs
54
+ # => { open: "true" }
55
+ ```
56
+
57
+ Likewise you can set classes, data, and aria attributes:
58
+
59
+ ```ruby
60
+ class ModalComponent
61
+ include HasDomAttrs
62
+
63
+ has_dom_attr :open
64
+ has_dom_class -> { "modal--width_#{width}" }
65
+ has_dom_aria :aria_modal, if: :open
66
+ has_dom_data :width
67
+
68
+ attr_reader :open
69
+
70
+ def initialize(open: false, width: :m)
71
+ @open = open
72
+ @width = width
73
+ end
74
+ end
75
+ ```
76
+
77
+ ```ruby
78
+ component = ModalComponent.new(open: true, width: :l)
79
+ component.dom_attrs
80
+ # => { open: "true", class: "modal--width_l", aria: { modal: true }, data: { width: "l" } }
81
+ ```
82
+
83
+ ## Development
84
+
85
+ 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.
86
+
87
+ 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).
88
+
89
+ ## Contributing
90
+
91
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/has_dom_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,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/has_dom_attrs/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "has_dom_attrs"
7
+ spec.version = HasDomAttrs::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 html element attributes."
12
+ spec.description = "Helper methods for dealing with html element attributes."
13
+ spec.homepage = "https://github.com/tomasc/has_dom_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_dom_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 "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
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 HasDomAttrs
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "has_dom_attrs/version"
4
+
5
+ require "active_support/core_ext/hash/keys"
6
+ require "active_support/core_ext/string/inflections"
7
+
8
+ module HasDomAttrs
9
+ class << self
10
+ def included(base)
11
+ base.extend ClassMethods
12
+ end
13
+ end
14
+
15
+ module ClassMethods
16
+ def has_dom_attr(name, value = nil, **options)
17
+ prepend___has_dom___method(:dom_attrs, name, value, **options)
18
+ end
19
+
20
+ def has_dom_aria(name, value = nil, **options)
21
+ prepend___has_dom___method(:dom_aria, name, value, **options)
22
+ end
23
+
24
+ def has_dom_data(name, value = nil, **options)
25
+ prepend___has_dom___method(:dom_data, name, value, **options)
26
+ end
27
+
28
+ def has_dom_class(value, **options)
29
+ prepend(
30
+ Module.new do
31
+ define_method :dom_classes do
32
+ cond = options[:if] || options[:unless]
33
+ cond_value = case cond
34
+ when Proc then instance_exec(&cond)
35
+ when Symbol, String then send(cond)
36
+ end
37
+
38
+ if cond && options.key?(:if)
39
+ return super() unless cond_value
40
+ end
41
+
42
+ if cond && options.key?(:unless)
43
+ return super() if cond_value
44
+ end
45
+
46
+ super().tap do |classes|
47
+ classes << case value
48
+ when Proc then instance_exec(&value)
49
+ when Symbol then send(value)
50
+ else value
51
+ end
52
+ end
53
+ end
54
+ end
55
+ )
56
+ end
57
+
58
+ private
59
+ def prepend___has_dom___method(method_name, name, value = nil, **options)
60
+ prepend(
61
+ Module.new do
62
+ define_method method_name do
63
+ cond = options[:if] || options[:unless]
64
+ cond_value = case cond
65
+ when Proc then instance_exec(&cond)
66
+ when Symbol, String then send(cond)
67
+ end
68
+
69
+ if cond && options.key?(:if)
70
+ return super() unless cond_value
71
+ end
72
+
73
+ if cond && options.key?(:unless)
74
+ return super() if cond_value
75
+ end
76
+
77
+ super().tap do |data|
78
+ data[name] = case value
79
+ when Proc then instance_exec(&value)
80
+ when Symbol, String then send(value)
81
+ else send(name)
82
+ end
83
+ end
84
+ end
85
+ end
86
+ )
87
+ end
88
+ end
89
+
90
+ def dom_attrs
91
+ {
92
+ aria: dom_aria,
93
+ class: dom_classes,
94
+ data: dom_data,
95
+ style: dom_style
96
+ }.reject { |_, v| v.nil? || v.empty? }
97
+ .deep_stringify_keys
98
+ .deep_transform_keys(&:dasherize)
99
+ end
100
+
101
+ def dom_classes
102
+ []
103
+ end
104
+
105
+ def dom_aria
106
+ {}
107
+ end
108
+
109
+ def dom_data
110
+ {}
111
+ end
112
+
113
+ def dom_style
114
+ nil
115
+ end
116
+ end
@@ -0,0 +1,4 @@
1
+ module HasDomAttrs
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_dom_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: 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 dealing with html element attributes.
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
+ - Gemfile
109
+ - LICENSE
110
+ - README.md
111
+ - Rakefile
112
+ - has_dom_attrs.gemspec
113
+ - lefthook.yml
114
+ - lib/has_dom_attrs.rb
115
+ - lib/has_dom_attrs/version.rb
116
+ - sig/has_dom_attrs.rbs
117
+ homepage: https://github.com/tomasc/has_dom_attrs
118
+ licenses: []
119
+ metadata:
120
+ homepage_uri: https://github.com/tomasc/has_dom_attrs
121
+ source_code_uri: https://github.com/tomasc/has_dom_attrs
122
+ changelog_uri: https://github.com/tomasc/has_dom_attrs/CHANGELOG.md
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: 3.1.0
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubygems_version: 3.3.7
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Helper methods for dealing with html element attributes.
142
+ test_files: []