Class: Glib::JsonUi::JsonUiElement
- Inherits:
-
Object
- Object
- Glib::JsonUi::JsonUiElement
show all
- Defined in:
- app/helpers/glib/json_ui/abstract_builder.rb
Constant Summary
collapse
- @@_required_properties =
{}
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of JsonUiElement.
53
54
55
56
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 53
def initialize(json, page)
@json = json
@page = page
end
|
Instance Attribute Details
#json ⇒ Object
Returns the value of attribute json.
49
50
51
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 49
def json
@json
end
|
#page ⇒ Object
Returns the value of attribute page.
49
50
51
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 49
def page
@page
end
|
Class Method Details
.action(propName) ⇒ Object
286
287
288
289
290
291
292
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 286
def self.action(propName)
define_method(propName) do |value|
if value json.set!(propName) { value.call(page.action_builder) }
end
end
end
|
.any(propName) ⇒ Object
82
83
84
85
86
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 82
def self.any(propName)
define_method(propName) do |value|
json.set! propName, value
end
end
|
.array(propName) ⇒ Object
300
301
302
303
304
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 300
def self.array(propName)
define_method(propName) do |value|
json.set! propName, value&.to_a
end
end
|
.badgeable ⇒ Object
277
278
279
280
281
282
283
284
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 277
def self.badgeable
define_method(:badge) do |value|
json.badge do
json.text to_string(value[:text])
json.backgroundColor to_color(value[:backgroundColor])
end
end
end
|
.bool(propName, options = {}) ⇒ Object
204
205
206
207
208
209
210
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 204
def self.bool(propName, options = {})
define_method(propName) do |value|
bool = value == true
instance_variable_set("@#{propName}", bool) if options[:cache]
json.set! propName, bool
end
end
|
.color(propName) ⇒ Object
130
131
132
133
134
135
136
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 130
def self.color(propName)
define_method(propName) do |value|
if (value = to_color(value))
json.set! propName, value
end
end
end
|
.component_name ⇒ Object
78
79
80
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 78
def self.component_name
@component_name ||= self.name.sub('Glib::JsonUi::ActionBuilder::', '')
end
|
.date(propName) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 88
def self.date(propName)
define_method(propName) do |value|
json.set! propName, value&.to_date
end
end
|
.date_time(propName) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 102
def self.date_time(propName)
define_method(propName) do |value|
json.set! propName, value&.to_datetime
end
end
|
.enum(propName, options = {}) ⇒ Object
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 165
def self.enum(propName, options = {})
define_method(propName) do |value|
str = to_string(value)
if !Rails.env.production?
allowed_options = options[:options] || []
if allowed_options.present? && !allowed_options.map(&:to_s).include?(str)
raise "Invalid value for #{propName}: '#{str}'. Allowed values: #{allowed_options.map(&:inspect).join(', ')}"
end
end
instance_variable_set("@#{propName}", str) if options[:cache]
json.set! propName, str
end
end
|
.float(propName) ⇒ Object
198
199
200
201
202
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 198
def self.float(propName)
define_method(propName) do |value|
json.set! propName, value&.to_f
end
end
|
.hash(propName, **options) ⇒ Object
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 306
def self.hash(propName, **options)
define_method(propName) do |value|
return if value.nil?
value = ActiveSupport::HashWithIndifferentAccess.new(value)
required = options[:required] || []
optional = options[:optional] || []
required.each do |req|
raise ArgumentError, "Hash property: '#{req}' is required" if value[req.to_s].nil?
end
if optional.present?
value.except(*required).each do |k, v|
raise ArgumentError, "Unknown hash property: '#{k}'" if !optional.include?(k.to_sym)
end
end
json.set! propName, value
end
end
|
.icon(propName, options = {}) ⇒ Object
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 212
def self.icon(propName, options = {})
define_method(propName) do |value|
if value.is_a?(Hash)
data = value
name = data[:name]
else
data = {}
name = value
end
json.set!(propName) do
if (color = data[:color])
json.color color
end
if (style_classes = data[:styleClasses])
json.styleClasses style_classes
end
if name
data[:material] ||= {}
data[:material][:name] = name
end
if (material = data[:material])
json.material do
json.name material[:name]
json.size material[:size] if material[:size]
end
end
if (fa = data[:fa])
json.fa do
json.name fa[:name]
json.size fa[:size] if fa[:size]
end
end
if (custom = data[:custom])
json.custom do
json.name custom[:name]
json.size custom[:size] if custom[:size]
end
end
if (badge = data[:badge])
if badge.is_a?(Hash)
json.badge badge
else
json.badge do
json.text badge
end
end
end
end
end
end
|
.int(propName) ⇒ Object
192
193
194
195
196
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 192
def self.int(propName)
define_method(propName) do |value|
json.set! propName, value&.to_i
end
end
|
.length(propName) ⇒ Object
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 138
def self.length(propName)
define_method(propName) do |value|
if value
if !Rails.env.production?
case value
when 'matchParent'
when 'wrapContent'
else
if !value.is_a? Integer
raise "Invalid length: #{value}"
end
end
end
json.set! propName, value
end
end
end
|
329
330
331
332
333
334
335
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 329
def self.(propName)
define_method(propName) do |value|
json.set! propName do
value.call(page.)
end
end
end
|
.panels_builder(propName, *panelNames) ⇒ Object
345
346
347
348
349
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 345
def self.panels_builder(propName, *panelNames)
define_method(propName) do |value|
value.call(page.content_builder(panelNames))
end
end
|
.required(*prop_names) ⇒ Object
351
352
353
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 351
def self.required(*prop_names)
@@_required_properties[self.component_name] = prop_names
end
|
.singleton_array(singletonName, arrayName) ⇒ Object
337
338
339
340
341
342
343
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 337
def self.singleton_array(singletonName, arrayName)
array arrayName
define_method(singletonName) do |value|
json.set! arrayName, value.to_s.split(' ').compact_blank
end
end
|
.string(propName, options = {}) ⇒ Object
157
158
159
160
161
162
163
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 157
def self.string(propName, options = {})
define_method(propName) do |value|
str = to_string(value)
instance_variable_set("@#{propName}", str) if options[:cache]
json.set! propName, str
end
end
|
.text(propName, options = {}) ⇒ Object
Support either string or dynamic_text hash
182
183
184
185
186
187
188
189
190
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 182
def self.text(propName, options = {})
define_method(propName) do |value|
if value.is_a?(Hash)
json.set! propName, value
else
json.set! propName, value&.to_s
end
end
end
|
.url(propName) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 116
def self.url(propName)
define_method(propName) do |value|
if (value = value&.to_s)
if !Rails.env.production?
if !UrlValidator.validate(value)
raise "Invalid URL: #{value}"
end
end
json.set! propName, value
end
end
end
|
.views(propName) ⇒ Object
294
295
296
297
298
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 294
def self.views(propName)
define_method(propName) do |value|
json.set!(propName) { value.call(page.view_builder) }
end
end
|
Instance Method Details
#props(args = {}) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 58
def props(args = {})
@args = args
if args.is_a? Hash
args.each do |k, v|
send(k, v)
end
required_properties = @@_required_properties[self.class.component_name] || []
required_properties.each do |prop|
raise "Property required: #{prop}. Properties provided: #{args}. Component: #{self.class.component_name}" unless args[prop].present?
end
else
raise "Invalid properties: #{args}"
end
created
end
|