Class: Foobara::Types::Type
Overview
TODO: move casting interface to here?
Defined Under Namespace
Modules: Concerns
Instance Attribute Summary collapse
#prioritize
Class Method Summary
collapse
Instance Method Summary
collapse
-
#add_caster(processor) ⇒ Object
-
#applicable?(value) ⇒ Boolean
-
#base_type_for_manifest ⇒ Object
-
#builtin? ⇒ Boolean
TODO: replace the concept of builtin? with primitive? and delete this method since primitive? already exists.
-
#cast(value) ⇒ Object
-
#cast!(value) ⇒ Object
-
#clear_caches ⇒ Object
-
#declaration_data_for_manifest ⇒ Object
-
#derived? ⇒ Boolean
-
#each_processor_class_requiring_type(&block) ⇒ Object
-
#element_processor ⇒ Object
-
#extends?(type) ⇒ Boolean
-
#extends_directly?(type) ⇒ Boolean
-
#extends_type?(type) ⇒ Boolean
-
#foobara_manifest ⇒ Object
-
#foobara_manifest_reference ⇒ Object
TODO: put this somewhere else.
-
#full_type_name ⇒ Object
-
#full_type_symbol ⇒ Object
-
#handle_reregistered_types!(seen) ⇒ Object
-
#has_sensitive_types? ⇒ Boolean
-
#initialize(declaration_data, target_classes:, base_type:, description: nil, name: "anonymous", casters: [], transformers: [], validators: [], element_processors: nil, structure_count: nil, processor_classes_requiring_type: nil, sensitive: nil, sensitive_exposed: nil, **opts) ⇒ Type
constructor
-
#needs_cast?(value) ⇒ Boolean
-
#primitive? ⇒ Boolean
TODO: replace the concept of builtin? with primitive?.
-
#processors ⇒ Object
-
#processors= ⇒ Object
-
#reference_or_declaration_data(declaration_data = self.declaration_data) ⇒ Object
-
#reference_or_declaration_data_for_manifest ⇒ Object
-
#reference_symbol ⇒ Object
-
#registered? ⇒ Boolean
-
#remove_caster_instances_of(klass) ⇒ Object
-
#remove_processor_by_symbol(symbol) ⇒ Object
-
#sensitive? ⇒ Boolean
-
#sensitive_exposed? ⇒ Boolean
-
#target_class ⇒ Object
-
#validation_errors(value) ⇒ Object
TODO: some way of memoizing these values? Would need to introduce a new class that takes the value to its constructor.
-
#value_caster ⇒ Object
-
#value_transformer ⇒ Object
TODO: an interesting thought...
-
#value_validator ⇒ Object
TODO: figure out how to safely memoize stuff so like this for performance reasons A good way, but potentially a decent amount of work, is to have a class that takes value to its initialize method.
#foobara_domain, #foobara_organization, #scoped_clear_caches
#deep_types_depended_on, #inspect, #type_at_path, #types_depended_on, #types_to_add_to_manifest
Methods included from Concern
foobara_class_methods_module_for, foobara_concern?, included
#all_supported_processor_classes, #find_supported_processor_class, #register_supported_processor_class, #supported_processor_classes
foobara_manifest, #process_outcome, #process_value
#error_classes, #possible_errors, #processor_names, #register
Constructor Details
#initialize(declaration_data, target_classes:, base_type:, description: nil, name: "anonymous", casters: [], transformers: [], validators: [], element_processors: nil, structure_count: nil, processor_classes_requiring_type: nil, sensitive: nil, sensitive_exposed: nil, **opts) ⇒ Type
Returns a new instance of Type.
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'projects/typesystem/projects/types/src/type.rb', line 39
def initialize(
declaration_data,
target_classes:,
base_type:,
description: nil,
name: "anonymous",
casters: [],
transformers: [],
validators: [],
element_processors: nil,
structure_count: nil,
processor_classes_requiring_type: nil,
sensitive: nil,
sensitive_exposed: nil,
**opts
)
self.declaration_data = declaration_data
self.sensitive = sensitive
self.sensitive_exposed = sensitive_exposed
self.base_type = base_type
self.description = description
self.name = name
self.casters = [*casters, *base_type&.casters]
self.transformers = [*transformers, *base_type&.transformers]
self.validators = [*validators, *base_type&.validators]
self.element_processors = [*element_processors, *base_type&.element_processors]
self.structure_count = structure_count
self.target_classes = Util.array(target_classes)
self.processor_classes_requiring_type = processor_classes_requiring_type
super(declaration_data, **opts.merge(processors:, prioritize: false))
apply_all_processors_needing_type!
validate_processors!
end
|
Instance Attribute Details
#base_type ⇒ Object
Returns the value of attribute base_type.
17
18
19
|
# File 'projects/typesystem/projects/types/src/type.rb', line 17
def base_type
@base_type
end
|
#cast_even_if_instance_of_target_type ⇒ Object
Returns the value of attribute cast_even_if_instance_of_target_type.
27
28
29
|
# File 'projects/typesystem/projects/types/src/type.rb', line 27
def cast_even_if_instance_of_target_type
@cast_even_if_instance_of_target_type
end
|
#casters ⇒ Object
Returns the value of attribute casters.
27
28
29
|
# File 'projects/typesystem/projects/types/src/type.rb', line 27
def casters
@casters
end
|
#description ⇒ Object
Returns the value of attribute description.
17
18
19
|
# File 'projects/typesystem/projects/types/src/type.rb', line 17
def description
@description
end
|
#element_processors ⇒ Object
Returns the value of attribute element_processors.
27
28
29
|
# File 'projects/typesystem/projects/types/src/type.rb', line 27
def element_processors
@element_processors
end
|
#element_type ⇒ Object
94
95
96
97
98
99
100
101
102
|
# File 'projects/typesystem/projects/types/src/type.rb', line 94
def element_type
lru_cache.cached([self, :element_type]) do
if element_type_loader
element_type_loader.resolve(self)
else
base_type&.element_type
end
end
end
|
#element_type_loader ⇒ Object
Returns the value of attribute element_type_loader.
17
18
19
|
# File 'projects/typesystem/projects/types/src/type.rb', line 17
def element_type_loader
@element_type_loader
end
|
#element_types ⇒ Object
104
105
106
107
108
109
110
111
112
|
# File 'projects/typesystem/projects/types/src/type.rb', line 104
def element_types
lru_cache.cached([self, :element_types]) do
if element_types_loader
element_types_loader.resolve(self)
else
base_type&.element_types
end
end
end
|
#element_types_loader ⇒ Object
Returns the value of attribute element_types_loader.
17
18
19
|
# File 'projects/typesystem/projects/types/src/type.rb', line 17
def element_types_loader
@element_types_loader
end
|
#is_builtin ⇒ Object
Returns the value of attribute is_builtin.
17
18
19
|
# File 'projects/typesystem/projects/types/src/type.rb', line 17
def is_builtin
@is_builtin
end
|
#name ⇒ Object
Returns the value of attribute name.
17
18
19
|
# File 'projects/typesystem/projects/types/src/type.rb', line 17
def name
@name
end
|
#processor_classes_requiring_type ⇒ Object
Returns the value of attribute processor_classes_requiring_type.
27
28
29
|
# File 'projects/typesystem/projects/types/src/type.rb', line 27
def processor_classes_requiring_type
@processor_classes_requiring_type
end
|
#sensitive ⇒ Object
Returns the value of attribute sensitive.
17
18
19
|
# File 'projects/typesystem/projects/types/src/type.rb', line 17
def sensitive
@sensitive
end
|
#sensitive_exposed ⇒ Object
Returns the value of attribute sensitive_exposed.
17
18
19
|
# File 'projects/typesystem/projects/types/src/type.rb', line 17
def sensitive_exposed
@sensitive_exposed
end
|
#structure_count ⇒ Object
Returns the value of attribute structure_count.
17
18
19
|
# File 'projects/typesystem/projects/types/src/type.rb', line 17
def structure_count
@structure_count
end
|
#target_classes ⇒ Object
Returns the value of attribute target_classes.
27
28
29
|
# File 'projects/typesystem/projects/types/src/type.rb', line 27
def target_classes
@target_classes
end
|
Returns the value of attribute transformers.
27
28
29
|
# File 'projects/typesystem/projects/types/src/type.rb', line 27
def transformers
@transformers
end
|
#type_symbol ⇒ Object
Returns the value of attribute type_symbol.
27
28
29
|
# File 'projects/typesystem/projects/types/src/type.rb', line 27
def type_symbol
@type_symbol
end
|
#validators ⇒ Object
Returns the value of attribute validators.
27
28
29
|
# File 'projects/typesystem/projects/types/src/type.rb', line 27
def validators
@validators
end
|
Class Method Details
.requires_declaration_data? ⇒ Boolean
12
13
14
|
# File 'projects/typesystem/projects/types/src/type.rb', line 12
def requires_declaration_data?
true
end
|
Instance Method Details
#add_caster(processor) ⇒ Object
286
287
288
289
|
# File 'projects/typesystem/projects/types/src/type.rb', line 286
def add_caster(processor)
casters << processor
clear_caches
end
|
#applicable?(value) ⇒ Boolean
357
358
359
|
# File 'projects/typesystem/projects/types/src/type.rb', line 357
def applicable?(value)
!value_caster.needs_cast?(value) || value_caster.can_cast?(value)
end
|
#base_type_for_manifest ⇒ Object
544
545
546
|
# File 'projects/typesystem/projects/types/src/type.rb', line 544
def base_type_for_manifest
base_type
end
|
#builtin? ⇒ Boolean
TODO: replace the concept of builtin? with primitive? and delete this method since
primitive? already exists.
540
541
542
|
# File 'projects/typesystem/projects/types/src/type.rb', line 540
def builtin?
is_builtin
end
|
#cast(value) ⇒ Object
365
366
367
|
# File 'projects/typesystem/projects/types/src/type.rb', line 365
def cast(value)
value_caster.process_value(value)
end
|
#cast!(value) ⇒ Object
369
370
371
|
# File 'projects/typesystem/projects/types/src/type.rb', line 369
def cast!(value)
value_caster.process_value!(value)
end
|
#clear_caches ⇒ Object
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'projects/typesystem/projects/types/src/type.rb', line 138
def clear_caches
[
:@value_validator,
:@processors,
:@value_caster,
:@value_transformer,
:@element_processor,
:@possible_errors,
:@processors_without_casters
].each do |instance_variable|
if instance_variable_defined?(instance_variable)
remove_instance_variable(instance_variable)
end
end
end
|
#declaration_data_for_manifest ⇒ Object
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
|
# File 'projects/typesystem/projects/types/src/type.rb', line 449
def declaration_data_for_manifest
data = declaration_data
if data.is_a?(::Hash) && data[:type] == :attributes
if data.key?(:defaults)
defaults = data[:defaults]
if defaults.is_a?(::Hash) && defaults.values.any?(Proc)
cleaned_defaults = defaults.transform_values do |value|
if value.is_a?(Proc)
"[<Lazily Set>]"
else
value
end
end
data = data.merge(defaults: cleaned_defaults)
end
end
end
data
end
|
#derived? ⇒ Boolean
82
83
84
|
# File 'projects/typesystem/projects/types/src/type.rb', line 82
def derived?
declaration_data.is_a?(::Hash)
end
|
#each_processor_class_requiring_type(&block) ⇒ Object
178
179
180
181
182
183
184
|
# File 'projects/typesystem/projects/types/src/type.rb', line 178
def each_processor_class_requiring_type(&block)
base_type&.each_processor_class_requiring_type(&block)
processor_classes_requiring_type&.each do |processor_class|
block.call(processor_class)
end
end
|
#element_processor ⇒ Object
396
397
398
399
400
401
402
|
# File 'projects/typesystem/projects/types/src/type.rb', line 396
def element_processor
return @element_processor if defined?(@element_processor)
@element_processor = if element_processors && !element_processors.empty?
Value::Processor::Pipeline.new(processors: element_processors)
end
end
|
#extends?(type) ⇒ Boolean
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
# File 'projects/typesystem/projects/types/src/type.rb', line 204
def extends?(type)
case type
when Type
extends_type?(type)
when Symbol, String
concrete_type = created_in_namespace.foobara_lookup_type(type)
if concrete_type.nil?
raise "No type found for #{type}"
end
extends_type?(concrete_type)
else
raise ArgumentError, "Expected a Type or a Symbol/String, but got #{type.inspect}"
end
end
|
#extends_directly?(type) ⇒ Boolean
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
# File 'projects/typesystem/projects/types/src/type.rb', line 224
def extends_directly?(type)
case type
when Type
base_type == type
when Symbol, String
concrete_type = created_in_namespace.foobara_lookup_type(type)
if concrete_type.nil?
raise "No type found for #{type}"
end
extends_directly?(concrete_type)
else
raise ArgumentError, "Expected a Type or a Symbol/String, but got #{type.inspect}"
end
end
|
#extends_type?(type) ⇒ Boolean
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
# File 'projects/typesystem/projects/types/src/type.rb', line 245
def extends_type?(type)
return true if self == type
unless type
raise ArgumentError, "Expected a type but got nil"
end
if registered?
if type.registered?
if type.foobara_manifest_reference == foobara_manifest_reference
return true
end
end
end
base_type&.extends_type?(type)
end
|
#foobara_manifest ⇒ Object
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
# File 'projects/typesystem/projects/types/src/type.rb', line 478
def foobara_manifest
to_include = TypeDeclarations.foobara_manifest_context_to_include
remove_sensitive = TypeDeclarations.foobara_manifest_context_remove_sensitive?
include_processors = TypeDeclarations.include_processors?
types = []
types_depended_on.each do |dependent_type|
if dependent_type.registered?
types << dependent_type.foobara_manifest_reference
if to_include
to_include << dependent_type
end
end
end
possible_errors_manifests = possible_errors.map do |possible_error|
[possible_error.key.to_s, possible_error.foobara_manifest]
end.sort.to_h
declaration_data = declaration_data_for_manifest
if remove_sensitive
declaration_data = TypeDeclarations.remove_sensitive_types(declaration_data)
end
h = Util.remove_blank(
name:,
target_classes: target_classes.map(&:name).sort,
declaration_data:,
types_depended_on: types.sort,
possible_errors: possible_errors_manifests,
builtin: builtin?
).merge(description:, base_type: base_type_for_manifest&.full_type_name&.to_sym)
if sensitive?
h[:sensitive] = true
end
if sensitive_exposed?
h[:sensitive_exposed] = true
end
if include_processors
h.merge!(
supported_processor_manifest.merge(
Util.remove_blank(processors: processor_manifest)
)
)
end
target_classes.sort_by(&:name).each do |target_class|
if target_class.respond_to?(:foobara_manifest)
h.merge!(target_class.foobara_manifest)
end
end
super.merge(h)
end
|
#foobara_manifest_reference ⇒ Object
TODO: put this somewhere else
474
475
476
|
# File 'projects/typesystem/projects/types/src/type.rb', line 474
def foobara_manifest_reference
scoped_full_name
end
|
#full_type_name ⇒ Object
419
420
421
|
# File 'projects/typesystem/projects/types/src/type.rb', line 419
def full_type_name
scoped_full_name
end
|
#full_type_symbol ⇒ Object
316
317
318
319
320
321
322
|
# File 'projects/typesystem/projects/types/src/type.rb', line 316
def full_type_symbol
return @full_type_symbol if defined?(@full_type_symbol)
@full_type_symbol ||= if scoped_path_set?
scoped_full_name.to_sym
end
end
|
#handle_reregistered_types!(seen) ⇒ Object
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
|
# File 'projects/typesystem/projects/types/src/type.rb', line 552
def handle_reregistered_types!(seen)
seen << self
if base_type&.scoped_unregistered?
self.base_type = base_type.created_in_namespace.foobara_lookup_type!(
base_type.type_symbol
)
end
if target_classes.size == 1 && target_class.respond_to?(:handle_reregistered_types!)
unless seen.include?(target_class)
target_class.handle_reregistered_types!(seen)
end
end
case element_types
when Hash
element_types.each_value do |child|
child.handle_reregistered_types!(seen) unless seen.include?(child)
end
when Array
element_types.each do |child|
child.handle_reregistered_types!(seen) unless seen.include?(child)
end
when Type
element_types.handle_reregistered_types!(seen) unless seen.include?(element_types)
end
if element_type
element_type.handle_reregistered_types!(seen) unless seen.include?(element_type)
end
end
|
#has_sensitive_types? ⇒ Boolean
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'projects/typesystem/projects/types/src/type.rb', line 114
def has_sensitive_types?
return true if sensitive?
if declaration_data.is_a?(::Hash)
private = declaration_data[:private]
return true if private.is_a?(::Array) && !private.empty?
end
if element_type
return true if element_type.has_sensitive_types?
end
if element_types
types = if element_types.is_a?(::Hash)
element_types.values
else
[*element_types]
end
types.any?(&:has_sensitive_types?)
end
end
|
#needs_cast?(value) ⇒ Boolean
361
362
363
|
# File 'projects/typesystem/projects/types/src/type.rb', line 361
def needs_cast?(value)
value_caster.needs_cast?(value)
end
|
#primitive? ⇒ Boolean
TODO: replace the concept of builtin? with primitive?
78
79
80
|
# File 'projects/typesystem/projects/types/src/type.rb', line 78
def primitive?
declaration_data.is_a?(::Symbol)
end
|
#processors ⇒ Object
324
325
326
327
328
329
330
331
|
# File 'projects/typesystem/projects/types/src/type.rb', line 324
def processors
@processors ||= [
value_caster,
value_transformer,
value_validator,
element_processor
].compact.sort_by(&:priority)
end
|
#processors= ⇒ Object
265
266
267
268
|
# File 'projects/typesystem/projects/types/src/type.rb', line 265
def processors=(...)
clear_caches
super
end
|
#reference_or_declaration_data(declaration_data = self.declaration_data) ⇒ Object
427
428
429
430
431
432
433
434
435
436
437
|
# File 'projects/typesystem/projects/types/src/type.rb', line 427
def reference_or_declaration_data(declaration_data = self.declaration_data)
remove_sensitive = TypeDeclarations.foobara_manifest_context_remove_sensitive?
if registered?
reference_symbol
elsif remove_sensitive
TypeDeclarations.remove_sensitive_types(declaration_data)
else
declaration_data
end
end
|
#reference_or_declaration_data_for_manifest ⇒ Object
423
424
425
|
# File 'projects/typesystem/projects/types/src/type.rb', line 423
def reference_or_declaration_data_for_manifest
reference_or_declaration_data(declaration_data_for_manifest)
end
|
#reference_symbol ⇒ Object
439
440
441
442
443
444
445
446
447
|
# File 'projects/typesystem/projects/types/src/type.rb', line 439
def reference_symbol
raise "Not registered so no reference symbol" unless registered?
if scoped_unregistered?
unregistered_foobara_manifest_reference.to_sym
else
foobara_manifest_reference.to_sym
end
end
|
#registered? ⇒ Boolean
548
549
550
|
# File 'projects/typesystem/projects/types/src/type.rb', line 548
def registered?
!!type_symbol
end
|
#remove_caster_instances_of(klass) ⇒ Object
154
155
156
157
158
159
160
|
# File 'projects/typesystem/projects/types/src/type.rb', line 154
def remove_caster_instances_of(klass)
self.casters = casters.reject do |caster|
caster.is_a?(klass)
end
clear_caches
end
|
#remove_processor_by_symbol(symbol) ⇒ Object
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
# File 'projects/typesystem/projects/types/src/type.rb', line 162
def remove_processor_by_symbol(symbol)
[
casters,
element_processors,
processor_classes_requiring_type,
processors,
transformers,
validators
].each do |processor_collection|
processor_collection&.delete_if { |p| p.symbol == symbol }
end
supported_processor_classes&.each { |processor_hash| processor_hash.delete(symbol) }
processor_classes_requiring_type&.delete_if { |p| p.symbol == symbol }
clear_caches
end
|
#sensitive? ⇒ Boolean
86
87
88
|
# File 'projects/typesystem/projects/types/src/type.rb', line 86
def sensitive?
sensitive
end
|
#sensitive_exposed? ⇒ Boolean
90
91
92
|
# File 'projects/typesystem/projects/types/src/type.rb', line 90
def sensitive_exposed?
sensitive_exposed
end
|
#target_class ⇒ Object
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
# File 'projects/typesystem/projects/types/src/type.rb', line 186
def target_class
if target_classes.empty?
raise "No target classes"
elsif target_classes.size > 1
raise "Cannot use #target_class because this type has multiple target_classes"
end
target_classes[0]
end
|
#validation_errors(value) ⇒ Object
TODO: some way of memoizing these values? Would need to introduce a new class that takes the value to its
constructor
406
407
408
409
410
411
412
413
414
415
416
417
|
# File 'projects/typesystem/projects/types/src/type.rb', line 406
def validation_errors(value)
value = cast!(value)
if value_transformer
value = value_transformer.process_value!(value)
end
if value_validator
value_validator.process_value(value).errors
else
[]
end
end
|
#value_caster ⇒ Object
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
# File 'projects/typesystem/projects/types/src/type.rb', line 333
def value_caster
return @value_caster if defined?(@value_caster)
enforce_unique = if declaration_data.is_a?(::Hash)
declaration_data[:type] != :duck
else
true
end
Namespace.use created_in_namespace do
@value_caster = Value::Processor::Casting.new(
{ cast_to: reference_or_declaration_data },
casters:,
target_classes:,
enforce_unique:,
cast_even_if_instance_of_target_type:
)
end
end
|
TODO: an interesting thought... we have Processor and then a subclass of Processor and then an instance of
processor that encapsulates the declaration_data for that processor. But then we pass value to every
method in the instance of the processor as needed. This means it can't really memoize stuff. Should we create
an instance of something from the instance of the processor and then ask it questions?? TODO: try this
377
378
379
380
381
382
383
|
# File 'projects/typesystem/projects/types/src/type.rb', line 377
def value_transformer
return @value_transformer if defined?(@value_transformer)
@value_transformer = if transformers && !transformers.empty?
Value::Processor::Pipeline.new(processors: transformers)
end
end
|
#value_validator ⇒ Object
TODO: figure out how to safely memoize stuff so like this for performance reasons
A good way, but potentially a decent amount of work, is to have a class that takes value to its initialize
method.
388
389
390
391
392
393
394
|
# File 'projects/typesystem/projects/types/src/type.rb', line 388
def value_validator
return @value_validator if defined?(@value_validator)
@value_validator = if validators && !validators.empty?
Value::Processor::Pipeline.new(processors: validators)
end
end
|