spark-component 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +4 -0
- data/.rubocop.yml +2 -2
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +4 -4
- data/lib/spark/component/attr.rb +2 -2
- data/lib/spark/component/attribute.rb +103 -13
- data/lib/spark/component/element.rb +6 -3
- data/lib/spark/component/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1be4ebc3a6ecf66978f87558cbc83f349fcf41e15b4aff6aa9e5609d88b12a5f
|
4
|
+
data.tar.gz: 4a94a2bf74570d0f9cc7b531bb4d815c2e068c92162390d9da08e0885f3e6576
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 699b6ed45fd2c908648bdd7a78c09dcb916e1289e414181dbc0efef84b766eae81dd1694511e3ef499e0ae1b9ccb500e3e0154d9ffbd5695847436bd8e59557e
|
7
|
+
data.tar.gz: f588d817b0a446b3dff251c0b8687c6c712131aa50f9d652e0a63199b4a2a1f08ecc06d34678fc6072c28d7c8b966758e30e01568f72b54637376b6f8e826cf3
|
data/.github/workflows/ruby.yml
CHANGED
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
# v1.1.0 - 2020-01-10
|
2
|
+
|
3
|
+
- New: Class method `attribute_default_group` makes it easy to set defaults for multiple attributes based on a single component argument. It's great for theming, where setting `theme: :notice` can set attributes for color, layout, etc.
|
4
|
+
- New: Class methods `tag_attribute`, `data_attribute`, and `aria_attribute` make it easy to sync component arguments to tag_attrs object.
|
5
|
+
|
1
6
|
# v1.0.1 - 2020-01-08
|
2
7
|
|
3
8
|
- Fix: added `deep_compact` to Attr and TagAttr to ensure that `nil` or `empty?` objects are ignored.
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
spark-component (1.0
|
4
|
+
spark-component (1.1.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -91,7 +91,7 @@ GEM
|
|
91
91
|
nokogiri (1.10.7)
|
92
92
|
mini_portile2 (~> 2.4.0)
|
93
93
|
parallel (1.19.1)
|
94
|
-
parser (2.
|
94
|
+
parser (2.7.0.2)
|
95
95
|
ast (~> 2.4.0)
|
96
96
|
pry (0.12.2)
|
97
97
|
coderay (~> 1.1.0)
|
@@ -130,10 +130,10 @@ GEM
|
|
130
130
|
thor (>= 0.20.3, < 2.0)
|
131
131
|
rainbow (3.0.0)
|
132
132
|
rake (10.5.0)
|
133
|
-
rubocop (0.
|
133
|
+
rubocop (0.79.0)
|
134
134
|
jaro_winkler (~> 1.5.1)
|
135
135
|
parallel (~> 1.10)
|
136
|
-
parser (>= 2.
|
136
|
+
parser (>= 2.7.0.1)
|
137
137
|
rainbow (>= 2.2.2, < 4.0)
|
138
138
|
ruby-progressbar (~> 1.7)
|
139
139
|
unicode-display_width (>= 1.4.0, < 1.7)
|
data/lib/spark/component/attr.rb
CHANGED
@@ -38,9 +38,9 @@ module Spark
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def deep_compact(hash)
|
41
|
-
hash.
|
41
|
+
hash.reject do |_key, val|
|
42
42
|
val = deep_compact(val) if val.is_a?(Hash)
|
43
|
-
|
43
|
+
(val.nil? || val.respond_to?(:empty?) && val.empty?)
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
@@ -43,13 +43,7 @@ require "spark/component/tag_attr"
|
|
43
43
|
#
|
44
44
|
module Spark
|
45
45
|
module Component
|
46
|
-
BASE_ATTRIBUTES =
|
47
|
-
id: nil,
|
48
|
-
class: nil,
|
49
|
-
data: {},
|
50
|
-
aria: {},
|
51
|
-
html: {}
|
52
|
-
}.freeze
|
46
|
+
BASE_ATTRIBUTES = %i[id class data aria html].freeze
|
53
47
|
|
54
48
|
module Attribute
|
55
49
|
# All components and elements will support these attributes
|
@@ -66,6 +60,11 @@ module Spark
|
|
66
60
|
def initialize_attributes(attrs = nil)
|
67
61
|
attrs ||= {}
|
68
62
|
|
63
|
+
# Filter out attributes which aren't defined by class method
|
64
|
+
attrs.select! { |key, _value| self.class.attributes.keys.include?(key) }
|
65
|
+
|
66
|
+
initialize_attribute_default_groups(attrs)
|
67
|
+
|
69
68
|
self.class.attributes.each do |name, default|
|
70
69
|
default = (!default.nil? ? default : nil)
|
71
70
|
value = attrs[name].nil? ? default : attrs[name]
|
@@ -76,6 +75,28 @@ module Spark
|
|
76
75
|
end
|
77
76
|
end
|
78
77
|
|
78
|
+
def initialize_attribute_default_groups(attrs)
|
79
|
+
self.class.attribute_default_groups.each do |group, group_options|
|
80
|
+
# Determine what group name is set for this attribute.
|
81
|
+
name = attrs[group] || self.class.attributes[group]
|
82
|
+
|
83
|
+
# Get defaults to set from group name
|
84
|
+
defaults = group_options[name]
|
85
|
+
|
86
|
+
next unless defaults
|
87
|
+
unless defaults.is_a?(Hash)
|
88
|
+
raise("In argument group `:#{name}`, value `#{defaults}` must be a hash.")
|
89
|
+
end
|
90
|
+
|
91
|
+
defaults.each do |key, value|
|
92
|
+
if attrs[key].nil?
|
93
|
+
attrs[key] = value
|
94
|
+
instance_variable_set(:"@#{key}", value)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
79
100
|
def attribute(name)
|
80
101
|
attributes[name]
|
81
102
|
end
|
@@ -108,12 +129,17 @@ module Spark
|
|
108
129
|
end
|
109
130
|
end
|
110
131
|
|
111
|
-
# Initialize tag attributes
|
132
|
+
# Initialize tag attributes
|
112
133
|
#
|
113
|
-
# If a component or element
|
114
|
-
#
|
134
|
+
# If a component or element defines tag_attributes, aria_attributes, or data_attributes
|
135
|
+
# Automatically assign add arguments to tag_attrs
|
115
136
|
def tag_attrs
|
116
|
-
@tag_attrs
|
137
|
+
return @tag_attrs if @tag_attrs
|
138
|
+
|
139
|
+
@tag_attrs = TagAttr.new.add(attr_hash(*self.class.tag_attributes))
|
140
|
+
@tag_attrs.add(aria: attr_hash(*self.class.aria_attributes))
|
141
|
+
@tag_attrs.add(data: attr_hash(*self.class.data_attributes))
|
142
|
+
@tag_attrs
|
117
143
|
end
|
118
144
|
|
119
145
|
# Easy reference a tag's classname
|
@@ -142,7 +168,10 @@ module Spark
|
|
142
168
|
|
143
169
|
module ClassMethods
|
144
170
|
def attributes
|
145
|
-
|
171
|
+
# Set attributes default to a hash using keys defined by BASE_ATTRIBUTES
|
172
|
+
@attributes ||= Spark::Component::BASE_ATTRIBUTES.each_with_object({}) do |val, obj|
|
173
|
+
obj[val] = nil
|
174
|
+
end
|
146
175
|
end
|
147
176
|
|
148
177
|
# Sets attributes, accepts an array of keys, pass a hash to set default values
|
@@ -177,7 +206,9 @@ module Spark
|
|
177
206
|
name = :"attribute_#{name}"
|
178
207
|
|
179
208
|
if (choices = options.delete(:choices))
|
180
|
-
supported_choices = choices.map
|
209
|
+
supported_choices = choices.map do |c|
|
210
|
+
c.is_a?(String) ? c.to_sym : c.to_s
|
211
|
+
end.concat(choices)
|
181
212
|
|
182
213
|
choices = choices.map(&:inspect).to_sentence(last_word_connector: ", or ")
|
183
214
|
message = "\"%<value>s\" is not valid. Options include: #{choices}."
|
@@ -188,6 +219,57 @@ module Spark
|
|
188
219
|
validates(name, options)
|
189
220
|
end
|
190
221
|
|
222
|
+
# Store attributes to be added to tag_attrs
|
223
|
+
def tag_attributes
|
224
|
+
@tag_attributes ||= BASE_ATTRIBUTES.dup
|
225
|
+
end
|
226
|
+
|
227
|
+
# Store attributes to be added to tag_attrs aria
|
228
|
+
def aria_attributes
|
229
|
+
@aria_attributes ||= []
|
230
|
+
end
|
231
|
+
|
232
|
+
# Store attributes to be added to tag_attrs data
|
233
|
+
def data_attributes
|
234
|
+
@data_attributes ||= []
|
235
|
+
end
|
236
|
+
|
237
|
+
# Add attribute(s) and automatically add to tag_attr
|
238
|
+
def tag_attribute(*args)
|
239
|
+
attr_object = hash_from_args(*args)
|
240
|
+
|
241
|
+
if (aria_object = attr_object.delete(:aria))
|
242
|
+
attribute(aria_object)
|
243
|
+
aria_attributes.concat(aria_object.keys)
|
244
|
+
end
|
245
|
+
|
246
|
+
if (data_object = attr_object.delete(:data))
|
247
|
+
attribute(data_object)
|
248
|
+
data_attributes.concat(data_object.keys)
|
249
|
+
end
|
250
|
+
|
251
|
+
attribute(attr_object)
|
252
|
+
tag_attributes.concat(attr_object.keys)
|
253
|
+
end
|
254
|
+
|
255
|
+
# Add attribute(s) and automatically add to tag_attr's aria hash
|
256
|
+
def aria_attribute(*args)
|
257
|
+
tag_attribute(aria: hash_from_args(*args))
|
258
|
+
end
|
259
|
+
|
260
|
+
# Add attribute(s) and automatically add to tag_attr's data hash
|
261
|
+
def data_attribute(*args)
|
262
|
+
tag_attribute(data: hash_from_args(*args))
|
263
|
+
end
|
264
|
+
|
265
|
+
def attribute_default_group(object)
|
266
|
+
attribute_default_groups.merge!(object)
|
267
|
+
end
|
268
|
+
|
269
|
+
def attribute_default_groups
|
270
|
+
@attribute_default_groups ||= {}
|
271
|
+
end
|
272
|
+
|
191
273
|
private
|
192
274
|
|
193
275
|
# Store attributes and define methods for validation
|
@@ -200,6 +282,14 @@ module Spark
|
|
200
282
|
instance_variable_get(:"@#{name}")
|
201
283
|
end
|
202
284
|
end
|
285
|
+
|
286
|
+
# Convert mixed arguments to a hash
|
287
|
+
# Example: (:a, :b, c: true) => { a: nil, b: nil, c: true }
|
288
|
+
def hash_from_args(*args)
|
289
|
+
args.each_with_object({}) do |arg, obj|
|
290
|
+
arg.is_a?(Hash) ? obj.merge!(arg) : obj[arg] = nil
|
291
|
+
end
|
292
|
+
end
|
203
293
|
end
|
204
294
|
end
|
205
295
|
end
|
@@ -200,7 +200,7 @@ module Spark
|
|
200
200
|
|
201
201
|
private
|
202
202
|
|
203
|
-
# If an element extends a component, extend that
|
203
|
+
# If an element extends a component, extend that class and include necessary modules
|
204
204
|
def extend_class(component, &config)
|
205
205
|
base = Class.new(component || Spark::Component::Element::Base, &config)
|
206
206
|
define_model_name(base) if defined?(ActiveModel)
|
@@ -211,12 +211,15 @@ module Spark
|
|
211
211
|
base.define_singleton_method(:source_component) { component }
|
212
212
|
|
213
213
|
# Override component when used as an element
|
214
|
-
|
214
|
+
if defined?(Spark::Component::Integration)
|
215
|
+
base.include(Spark::Component::Integration::Element)
|
216
|
+
end
|
215
217
|
|
216
218
|
base
|
217
219
|
end
|
218
220
|
|
219
|
-
# ActiveModel validations require a model_name.
|
221
|
+
# ActiveModel validations require a model_name.
|
222
|
+
# This connects new classes to their proper model names.
|
220
223
|
def define_model_name(klass)
|
221
224
|
klass.define_singleton_method(:model_name) do
|
222
225
|
# try the current class, the parent class, or default to Spark::Component
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spark-component
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandon Mathis
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-01-
|
11
|
+
date: 2020-01-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionview-component
|