spark_components 1.2.2 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: adf69719858780a0edee031dde8d9cdd609ace50efb21ffd6aa5b6e1e1e676f5
4
- data.tar.gz: a218f301a64b629b5ef0b7b949f4c2b73c13d7a98cc89928bdef6079d25a1c98
3
+ metadata.gz: adcc859e7bb3c58976280b05b17c030842ee61a93452376c8a0de38b00c5da55
4
+ data.tar.gz: e5b968c9e8bd2317a77edd3883dcf986a1376f821fccaef690d46a380a5c8785
5
5
  SHA512:
6
- metadata.gz: c95e4981f350450f142a55680496b9d53bcb1569993d63a233fc68ff9c57876e65f3cb8c7309c465127b4536f7bb1e7db2f368c0c361c1dfd256fb6c2512fb21
7
- data.tar.gz: 46ee83f856eeab6c5bc6e6b3829850c21e0416a4dae6184ffe05257d5fce40f5809c24320d2a83424853d444609ffd658c37e83eab6c607bf70b337530eeb391
6
+ metadata.gz: 3307a090147aabd5ae4146148903012d685ddc8c092774c8cfc949c691f34fea43077c2f4b19fffe6b92795c0ac669d3ba50163d0cac00cf3b11b0b71cddb2aa
7
+ data.tar.gz: 92da07e9a0c368009f2e06a7149b795901de8dc54e66950a71da1b47af60bc37d4e6a2d82e158dd3146ee39052a4137e3e2d2db5e7a3ec8aca914ec176aeb40e
@@ -5,25 +5,31 @@ module SparkComponents
5
5
  class Hash < Hash
6
6
  def prefix; end
7
7
 
8
- def add(obj = nil)
9
- merge!(obj) unless obj.nil?
8
+ def add(*args)
9
+ args.each do |arg|
10
+ arg.is_a?(::Hash) ? merge!(arg) : self[arg.to_sym] = nil
11
+ end
10
12
  self
11
13
  end
12
14
 
13
- # Output all attributes as [base-]name="value"
15
+ # Output all attributes as [prefix-]name="value"
14
16
  def to_s
15
17
  each_with_object([]) do |(name, value), array|
16
- name = [prefix, name].compact.join("-")
17
- array << %(#{name.dasherize}="#{value}") unless value.nil?
18
- end.join(" ").html_safe
18
+ if value.is_a?(Attributes::Hash)
19
+ # Flatten nested hashs and inject them unless empty
20
+ value = value.to_s
21
+ array << value unless value.empty?
22
+ else
23
+ name = [prefix, name].compact.join("-").gsub(/[\W_]+/, "-")
24
+ array << %(#{name}="#{value}") unless value.nil?
25
+ end
26
+ end.sort.join(" ")
19
27
  end
20
28
 
21
- def collapse
22
- each_with_object({}) do |(name, value), obj|
23
- name = [prefix, name].compact.join("-")
24
- name = name.downcase.gsub(/[\W_]+/, "-")
25
- obj[name] = value unless value.nil? || value.is_a?(String) && value.empty?
26
- end
29
+ # Easy assess to create a new Attributes::Hash
30
+ def new(*args)
31
+ new_obj = self.class.new
32
+ new_obj.add(*args)
27
33
  end
28
34
  end
29
35
 
@@ -57,7 +63,7 @@ module SparkComponents
57
63
  # Ensure base class is the first element in the classes array.
58
64
  #
59
65
  def base=(klass)
60
- return if klass.blank?
66
+ return if klass.nil? || klass.empty?
61
67
 
62
68
  if @base_set
63
69
  self[0] = klass
@@ -79,11 +85,117 @@ module SparkComponents
79
85
  end
80
86
 
81
87
  def add(*args)
82
- push(*args.uniq.reject { |a| a.nil? || include?(a) })
88
+ push(*args.flatten.uniq.reject { |a| a.nil? || include?(a) })
89
+ self
90
+ end
91
+
92
+ # Easy assess to create a new Attributes::Classname
93
+ def new(*args)
94
+ new_arr = self.class.new
95
+
96
+ unless args.empty?
97
+ new_arr.base = args.shift
98
+ new_arr.add(*args)
99
+ end
100
+
101
+ new_arr
102
+ end
103
+
104
+ def join_class(name, separator = "-")
105
+ raise(SparkComponents::Attributes::Error, "Base class not defined for `join_class(#{name}, …)`") if base.nil?
106
+
107
+ [base, name].join(separator)
108
+ end
109
+
110
+ def to_s
111
+ join(" ")
112
+ end
113
+ end
114
+
115
+ class Tag
116
+ attr_reader :attrs
117
+
118
+ def initialize(obj = {})
119
+ @attrs = Attributes::Hash.new
120
+ merge!(obj)
121
+ end
122
+
123
+ def root(obj = {})
124
+ attrs.add(obj) unless obj.empty?
125
+ attrs
126
+ end
127
+
128
+ def aria(obj = {})
129
+ attrs[:aria] ||= Aria.new
130
+ attrs[:aria].add(obj) unless obj.empty?
131
+ attrs[:aria]
132
+ end
133
+
134
+ def data(obj = {})
135
+ attrs[:data] ||= Data.new
136
+ attrs[:data].add(obj) unless obj.empty?
137
+ attrs[:data]
138
+ end
139
+
140
+ def classnames(*args)
141
+ attrs[:class] ||= Classname.new
142
+ attrs[:class].add(*args) unless args.empty?
143
+ attrs[:class]
144
+ end
145
+
146
+ def add_class(*args)
147
+ classnames.add(*args)
148
+ end
149
+
150
+ def base_class(name)
151
+ classnames.base = name unless name.nil?
152
+ classnames.base
153
+ end
154
+
155
+ def join_class(*args)
156
+ classnames.join_class(*args)
157
+ end
158
+
159
+ def new(obj = {})
160
+ self.class.new(obj)
161
+ end
162
+
163
+ # Ensure each attribute is distinct
164
+ def dup
165
+ new(attrs.each_with_object(Attributes::Hash.new) do |(k, v), obj|
166
+ obj[k] = v.dup
167
+ end)
168
+ end
169
+
170
+ def merge!(obj = {})
171
+ merge_obj(self, obj)
172
+ end
173
+
174
+ def merge(obj = {})
175
+ merge_obj(dup, obj)
176
+ end
177
+
178
+ def merge_obj(tag, obj = {})
179
+ # If merging another Tag, extract attrs to merge
180
+ obj = obj.attrs if obj.is_a?(Tag)
181
+
182
+ obj.each do |key, val|
183
+ if val.is_a?(Classname)
184
+ # preserve object state
185
+ tag.attrs[:class] = val
186
+ else
187
+ case key.to_sym
188
+ when :class then tag.classnames.add(val)
189
+ when :data, :aria then tag.send(key).add(val)
190
+ else; tag.attrs[key] = val
191
+ end
192
+ end
193
+ end
194
+ tag
83
195
  end
84
196
 
85
197
  def to_s
86
- join(" ").html_safe
198
+ attrs.to_s
87
199
  end
88
200
  end
89
201
  end
@@ -15,10 +15,6 @@ module SparkComponents
15
15
  @attributes ||= {}
16
16
  end
17
17
 
18
- def self.themes
19
- @themes ||= {}
20
- end
21
-
22
18
  def self.elements
23
19
  @elements ||= {}
24
20
  end
@@ -45,48 +41,38 @@ module SparkComponents
45
41
  end
46
42
  end
47
43
 
48
- def self.base_class(name)
49
- tag_attributes[:class].base = name
44
+ def self.base_class(name = nil)
45
+ tag_attrs.base_class(name)
50
46
  end
51
47
 
52
48
  def self.add_class(*args)
53
- tag_attributes[:class].add(*args)
54
- end
55
-
56
- def self.add_theme(hash)
57
- themes.merge!(hash)
58
- return if attributes[:theme]
59
-
60
- if hash.key?(:default)
61
- attribute(theme: :default)
62
- else
63
- attribute(:theme)
64
- end
49
+ tag_attrs.add_class(*args)
65
50
  end
66
51
 
67
52
  def self.data_attr(*args)
68
- set_attr(:data, *args)
53
+ tag_attrs.data(attribute(*args))
69
54
  end
70
55
 
71
56
  def self.aria_attr(*args)
72
- set_attr(:aria, *args)
57
+ arg = attribute(*args)
58
+ tag_attrs.aria(arg)
73
59
  end
74
60
 
75
- def self.tag_attr(*args)
76
- set_attr(:tag, *args)
61
+ def self.root_attr(*args)
62
+ tag_attrs.root(attribute(*args))
77
63
  end
78
64
 
79
- def self.set_attr(name, *args)
80
- tag_attributes[name].add(attribute(*args))
65
+ def self.tag_attrs
66
+ @tag_attrs ||= SparkComponents::Attributes::Tag.new
81
67
  end
82
68
 
83
- def self.tag_attributes
84
- @tag_attributes ||= {
85
- class: SparkComponents::Attributes::Classname.new,
86
- data: SparkComponents::Attributes::Data.new,
87
- aria: SparkComponents::Attributes::Aria.new,
88
- tag: SparkComponents::Attributes::Hash.new
89
- }
69
+ def self.validates_choice(name, choices)
70
+ validates(name,
71
+ inclusion: {
72
+ presence: true,
73
+ in: choices,
74
+ message: "\"%{value}\" is not a valid option. Options include: #{choices.join(', ')}"
75
+ })
90
76
  end
91
77
 
92
78
  # rubocop:disable Metrics/AbcSize
@@ -144,22 +130,18 @@ module SparkComponents
144
130
  def self.inherited(subclass)
145
131
  attributes.each { |name, options| subclass.set_attribute(name, options.dup) }
146
132
  elements.each { |name, options| subclass.elements[name] = options.dup }
147
- subclass.themes.merge!(themes)
148
133
 
149
- subclass.tag_attributes.merge!(tag_attributes.each_with_object({}) do |(k, v), obj|
150
- obj[k] = v.dup
151
- end)
134
+ subclass.tag_attrs.merge!(tag_attrs.dup)
152
135
  end
153
136
 
154
137
  def initialize(view, attributes = nil, &block)
155
138
  @view = view
156
139
  attributes ||= {}
157
- initialize_tag_attributes
158
- assign_tag_attributes(attributes)
140
+ initialize_tag_attrs
141
+ assign_tag_attrs(attributes)
159
142
  initialize_attributes(attributes)
160
143
  initialize_elements
161
144
  extend_view_methods
162
- initialize_themes
163
145
  after_init
164
146
  @yield = block_given? ? @view.capture(self, &block) : nil
165
147
  validate!
@@ -177,8 +159,8 @@ module SparkComponents
177
159
  @parents.last
178
160
  end
179
161
 
180
- def classnames
181
- @tag_attributes[:class]
162
+ def classnames(*args)
163
+ @tag_attrs.classnames(*args)
182
164
  end
183
165
 
184
166
  def base_class(name = nil)
@@ -187,33 +169,27 @@ module SparkComponents
187
169
  end
188
170
 
189
171
  def add_class(*args)
190
- classnames.add(*args)
172
+ classnames(*args)
191
173
  end
192
174
 
193
- def join_class(name, separator: "-")
194
- [base_class, name].join(separator) unless base_class.nil?
175
+ def join_class(*args)
176
+ classnames.join_class(*args)
195
177
  end
196
178
 
197
179
  def data_attr(*args)
198
- @tag_attributes[:data].add(*args)
180
+ @tag_attrs.data(*args)
199
181
  end
200
182
 
201
183
  def aria_attr(*args)
202
- @tag_attributes[:aria].add(*args)
184
+ @tag_attrs.aria(*args)
203
185
  end
204
186
 
205
- def tag_attr(*args)
206
- @tag_attributes[:tag].add(*args)
187
+ def root_attr(*args)
188
+ @tag_attrs.root(*args)
207
189
  end
208
190
 
209
- def attrs(add_class: true)
210
- atr = Attributes::Hash.new
211
- # attrtiubte order: id, class, data-, aria-, misc tag attributes
212
- atr[:class] = classnames if add_class
213
- atr.merge!(data_attr.collapse)
214
- atr.merge!(aria_attr.collapse)
215
- atr.merge!(tag_attr)
216
- atr
191
+ def tag_attrs
192
+ @tag_attrs.attrs
217
193
  end
218
194
 
219
195
  def to_s
@@ -230,9 +206,9 @@ module SparkComponents
230
206
  protected
231
207
 
232
208
  # Set tag attribute values from from parameters
233
- def update_attr(name)
234
- %i[aria data tag].each do |el|
235
- @tag_attributes[el][name] = get_instance_variable(name) if @tag_attributes[el].key?(name)
209
+ def update_tag_attr(name)
210
+ %i[aria data root].each do |el|
211
+ @tag_attrs.send(el)[name] = get_instance_variable(name) if @tag_attrs.send(el).key?(name)
236
212
  end
237
213
  end
238
214
 
@@ -240,40 +216,23 @@ module SparkComponents
240
216
  @view.render(partial: file, object: self)
241
217
  end
242
218
 
243
- def initialize_tag_attributes
244
- @tag_attributes = self.class.tag_attributes.each_with_object({}) do |(name, options), obj|
245
- obj[name] = options.dup
246
- end
219
+ def initialize_tag_attrs
220
+ @tag_attrs = self.class.tag_attrs.dup
247
221
  end
248
222
 
249
- def assign_tag_attributes(attributes)
223
+ # Assign tag attributes from arguments
224
+ def assign_tag_attrs(attributes)
250
225
  # support default data, class, and aria attribute names
251
226
  data_attr(attributes.delete(:data)) if attributes[:data]
252
227
  aria_attr(attributes.delete(:aria)) if attributes[:aria]
253
228
  add_class(*attributes.delete(:class)) if attributes[:class]
254
- tag_attr(attributes.delete(:splat)) if attributes[:splat]
229
+ root_attr(attributes.delete(:splat)) if attributes[:splat]
255
230
  end
256
231
 
257
232
  def initialize_attributes(attributes)
258
233
  self.class.attributes.each do |name, options|
259
234
  set_instance_variable(name, attributes[name] || (options[:default] && options[:default].dup))
260
- update_attr(name)
261
- end
262
- end
263
-
264
- # Add a class based on the chosen theme
265
- def initialize_themes
266
- themes = self.class.themes.stringify_keys
267
- return if themes.empty? || @theme.nil?
268
-
269
- theme = @theme.to_s
270
-
271
- if !themes.key?(theme)
272
- theme_list = self.class.themes.keys.map(&:inspect).join(", ")
273
- msg = "Unsupported theme: #{@theme.inspect} is not a valid theme. Try: #{theme_list}."
274
- return raise(SparkComponents::Error, msg)
275
- else
276
- add_class themes[theme]
235
+ update_tag_attr(name)
277
236
  end
278
237
  end
279
238
 
@@ -291,7 +250,7 @@ module SparkComponents
291
250
 
292
251
  # Define common view methods to "alias"
293
252
  def view_methods
294
- %i[tag content_tag image_tag concat content_for link_to component]
253
+ %i[tag content_tag image_tag concat content_for link_to component capture]
295
254
  end
296
255
 
297
256
  def extend_view_methods
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SparkComponents
4
- VERSION = "1.2.2"
4
+ VERSION = "1.3.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spark_components
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Mathis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-26 00:00:00.000000000 Z
11
+ date: 2019-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -77,7 +77,7 @@ files:
77
77
  - lib/spark_components/railtie.rb
78
78
  - lib/spark_components/version.rb
79
79
  - lib/tasks/components_tasks.rake
80
- homepage: https://github.com/imathis/spark_components
80
+ homepage: https://github.com/spark-engine/components
81
81
  licenses:
82
82
  - MIT
83
83
  metadata: {}