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.
38
39
40
41
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 38
def initialize(json, page)
@json = json
@page = page
end
|
Instance Attribute Details
#json ⇒ Object
Returns the value of attribute json.
34
35
36
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 34
def json
@json
end
|
#page ⇒ Object
Returns the value of attribute page.
34
35
36
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 34
def page
@page
end
|
Class Method Details
.action(propName) ⇒ Object
271
272
273
274
275
276
277
278
279
280
281
282
283
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 271
def self.action(propName)
define_method(propName) do |value|
if value json.set!(propName) do
action_builder = page.action_builder
action_builder.with_isolated_singleton_tracking { value.call(action_builder) }
end
end
end
end
|
.any(propName) ⇒ Object
67
68
69
70
71
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 67
def self.any(propName)
define_method(propName) do |value|
json.set! propName, value
end
end
|
.array(propName) ⇒ Object
291
292
293
294
295
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 291
def self.array(propName)
define_method(propName) do |value|
json.set! propName, value&.to_a
end
end
|
.badgeable ⇒ Object
262
263
264
265
266
267
268
269
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 262
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
189
190
191
192
193
194
195
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 189
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
115
116
117
118
119
120
121
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 115
def self.color(propName)
define_method(propName) do |value|
if (value = to_color(value))
json.set! propName, value
end
end
end
|
.component_name ⇒ Object
63
64
65
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 63
def self.component_name
@component_name ||= self.name.sub('Glib::JsonUi::ActionBuilder::', '')
end
|
.date(propName) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 73
def self.date(propName)
define_method(propName) do |value|
json.set! propName, value&.to_date
end
end
|
.date_time(propName) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 87
def self.date_time(propName)
define_method(propName) do |value|
json.set! propName, value&.to_datetime
end
end
|
.enum(propName, options = {}) ⇒ Object
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 150
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
183
184
185
186
187
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 183
def self.float(propName)
define_method(propName) do |value|
json.set! propName, value&.to_f
end
end
|
.hash(propName, **options) ⇒ Object
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 297
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 197
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
177
178
179
180
181
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 177
def self.int(propName)
define_method(propName) do |value|
json.set! propName, value&.to_i
end
end
|
.length(propName) ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 123
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
|
320
321
322
323
324
325
326
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 320
def self.(propName)
define_method(propName) do |value|
json.set! propName do
value.call(page.)
end
end
end
|
.panels_builder(propName, *panelNames) ⇒ Object
336
337
338
339
340
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 336
def self.panels_builder(propName, *panelNames)
define_method(propName) do |value|
value.call(page.content_builder(panelNames))
end
end
|
.required(*prop_names) ⇒ Object
342
343
344
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 342
def self.required(*prop_names)
@@_required_properties[self.component_name] = prop_names
end
|
.singleton_array(singletonName, arrayName) ⇒ Object
328
329
330
331
332
333
334
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 328
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
142
143
144
145
146
147
148
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 142
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
167
168
169
170
171
172
173
174
175
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 167
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
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 101
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
285
286
287
288
289
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 285
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'app/helpers/glib/json_ui/abstract_builder.rb', line 43
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}" if args[prop].nil?
end
else
raise "Invalid properties: #{args}"
end
created
end
|