Module: Foobara::BuiltinTypes

Defined in:
foobara-0.3.0/projects/builtin_types/lib/foobara/builtin_types.rb,
foobara-0.3.0/projects/builtin_types/src/duck.rb,
foobara-0.3.0/projects/builtin_types/src/duckture.rb,
foobara-0.3.0/projects/builtin_types/src/atomic_duck.rb,
foobara-0.3.0/projects/builtin_types/src/builtin_types.rb,
foobara-0.3.0/projects/builtin_types/src/date/casters/hash.rb,
foobara-0.3.0/projects/builtin_types/src/date/casters/string.rb,
foobara-0.3.0/projects/builtin_types/src/email/validator_base.rb,
foobara-0.3.0/projects/builtin_types/src/float/casters/string.rb,
foobara-0.3.0/projects/builtin_types/src/datetime/casters/date.rb,
foobara-0.3.0/projects/builtin_types/src/datetime/casters/hash.rb,
foobara-0.3.0/projects/builtin_types/src/float/casters/integer.rb,
foobara-0.3.0/projects/builtin_types/src/string/casters/symbol.rb,
foobara-0.3.0/projects/builtin_types/src/symbol/casters/string.rb,
foobara-0.3.0/projects/builtin_types/src/integer/casters/string.rb,
foobara-0.3.0/projects/builtin_types/src/string/casters/numeric.rb,
foobara-0.3.0/projects/builtin_types/src/array/casters/arrayable.rb,
foobara-0.3.0/projects/builtin_types/src/attributes/casters/hash.rb,
foobara-0.3.0/projects/builtin_types/src/boolean/casters/numeric.rb,
foobara-0.3.0/projects/builtin_types/src/datetime/casters/string.rb,
foobara-0.3.0/projects/builtin_types/src/attributes/casters/array.rb,
foobara-0.3.0/projects/entity/src/extensions/builtin_types/entity.rb,
foobara-0.3.0/projects/builtin_types/src/big_decimal/casters/string.rb,
foobara-0.3.0/projects/builtin_types/src/big_decimal/casters/integer.rb,
foobara-0.3.0/projects/builtin_types/src/email/transformers/downcase.rb,
foobara-0.3.0/projects/builtin_types/src/array/supported_validators/size.rb,
foobara-0.3.0/projects/builtin_types/src/associative_array/casters/array.rb,
foobara-0.3.0/projects/builtin_types/src/number/supported_validators/max.rb,
foobara-0.3.0/projects/builtin_types/src/number/supported_validators/min.rb,
foobara-0.3.0/projects/builtin_types/src/boolean/casters/string_or_symbol.rb,
foobara-0.3.0/projects/builtin_types/src/duck/supported_casters/allow_nil.rb,
foobara-0.3.0/projects/builtin_types/src/duck/supported_validators/one_of.rb,
foobara-0.3.0/projects/builtin_types/src/string/supported_validators/matches.rb,
foobara-0.3.0/projects/model/src/extensions/builtin_types/model/casters/hash.rb,
foobara-0.3.0/projects/builtin_types/src/datetime/casters/seconds_since_epoch.rb,
foobara-0.3.0/projects/builtin_types/src/duck/supported_validators/instance_of.rb,
foobara-0.3.0/projects/entity/src/extensions/builtin_types/entity/casters/hash.rb,
foobara-0.3.0/projects/builtin_types/src/string/supported_transformers/downcase.rb,
foobara-0.3.0/projects/builtin_types/src/string/supported_validators/max_length.rb,
foobara-0.3.0/projects/builtin_types/src/attributes/supported_validators/required.rb,
foobara-0.3.0/projects/builtin_types/src/attributes/supported_transformers/defaults.rb,
foobara-0.3.0/projects/detached_entity/src/extensions/builtin_types/detached_entity.rb,
foobara-0.3.0/projects/entity/src/extensions/builtin_types/entity/casters/primary_key.rb,
foobara-0.3.0/projects/builtin_types/src/array/supported_processors/element_type_declaration.rb,
foobara-0.3.0/projects/builtin_types/src/attributes/transformers/remove_unexpected_attributes.rb,
foobara-0.3.0/projects/builtin_types/src/tuple/supported_processors/element_type_declarations.rb,
foobara-0.3.0/projects/model/src/extensions/builtin_types/model/supported_transformers/mutable.rb,
foobara-0.3.0/projects/detached_entity/src/extensions/builtin_types/detached_entity/casters/hash.rb,
foobara-0.3.0/projects/builtin_types/src/attributes/supported_processors/element_type_declarations.rb,
foobara-0.3.0/projects/model/src/extensions/builtin_types/model/validators/model_instance_is_valid.rb,
foobara-0.3.0/projects/builtin_types/src/associative_array/supported_processors/key_type_declaration.rb,
foobara-0.3.0/projects/entity/src/extensions/builtin_types/entity/validators/model_instance_is_valid.rb,
foobara-0.3.0/projects/builtin_types/src/associative_array/supported_processors/value_type_declaration.rb,
foobara-0.3.0/projects/detached_entity/src/extensions/builtin_types/detached_entity/casters/primary_key.rb,
foobara-0.3.0/projects/entity/src/extensions/builtin_types/entity/casters/record_from_closed_transaction.rb,
foobara-0.3.0/projects/entity/src/extensions/builtin_types/entity/casters/record_from_current_transaction.rb,
foobara-0.3.0/projects/detached_entity/src/extensions/builtin_types/detached_entity/validators/model_instance_is_valid.rb,
foobara-0.3.0/projects/builtin_types/src/duck/supported_validators/one_of/type_declaration_extension/extend_registered_type_declaration/desugarizers/cast_one_of.rb,
foobara-0.3.0/projects/builtin_types/src/duck/supported_validators/one_of/type_declaration_extension/extend_registered_type_declaration/desugarizers/module_desugarizer.rb,
foobara-0.3.0/projects/builtin_types/src/duck/supported_validators/instance_of/type_declaration_extension/extend_registered_type_declaration/desugarizers/class_desugarizer.rb,
foobara-0.3.0/projects/builtin_types/src/tuple/supported_processors/element_type_declarations/type_declaration_extension/extend_tuple_type_declaration/desugarizers/set_size.rb,
foobara-0.3.0/projects/builtin_types/src/duck/supported_validators/instance_of/type_declaration_extension/extend_registered_type_declaration/desugarizers/class_type_desugarizer.rb,
foobara-0.3.0/projects/builtin_types/src/attributes/supported_transformers/defaults/type_declaration_extension/extend_attributes_type_declaration/desugarizers/symbolize_defaults.rb,
foobara-0.3.0/projects/builtin_types/src/attributes/supported_validators/required/type_declaration_extension/extend_attributes_type_declaration/desugarizers/alphabetize_required.rb,
foobara-0.3.0/projects/builtin_types/src/duck/supported_validators/instance_of/type_declaration_extension/extend_registered_type_declaration/desugarizers/instance_of_class_desugarizer.rb,
foobara-0.3.0/projects/builtin_types/src/duck/supported_validators/instance_of/type_declaration_extension/extend_registered_type_declaration/type_declaration_validators/is_valid_class.rb,
foobara-0.3.0/projects/builtin_types/src/duck/supported_validators/instance_of/type_declaration_extension/extend_registered_type_declaration/desugarizers/instance_of_symbol_desugarizer.rb,
foobara-0.3.0/projects/builtin_types/src/attributes/supported_validators/required/type_declaration_extension/extend_attributes_type_declaration/type_declaration_validators/array_of_symbols.rb,
foobara-0.3.0/projects/builtin_types/src/tuple/supported_processors/element_type_declarations/type_declaration_extension/extend_tuple_type_declaration/type_declaration_validators/size_matches.rb,
foobara-0.3.0/projects/builtin_types/src/attributes/supported_transformers/defaults/type_declaration_extension/extend_attributes_type_declaration/type_declaration_validators/valid_attribute_names.rb,
foobara-0.3.0/projects/builtin_types/src/attributes/supported_transformers/defaults/type_declaration_extension/extend_attributes_type_declaration/type_declaration_validators/hash_with_symbolic_keys.rb,
foobara-0.3.0/projects/builtin_types/src/attributes/supported_validators/required/type_declaration_extension/extend_attributes_type_declaration/desugarizers/move_required_from_element_types_to_root.rb,
foobara-0.3.0/projects/builtin_types/src/attributes/supported_transformers/defaults/type_declaration_extension/extend_attributes_type_declaration/desugarizers/move_defaults_from_element_types_to_root.rb,
foobara-0.3.0/projects/builtin_types/src/attributes/supported_validators/required/type_declaration_extension/extend_attributes_type_declaration/type_declaration_validators/array_with_valid_attribute_names.rb

Overview

TODO: rename this to PrimitiveTypes and deprecate the name BuiltinTypes

Defined Under Namespace

Modules: Array, AssociativeArray, AtomicDuck, Attributes, BigDecimal, Boolean, Date, Datetime, DetachedEntity, Duck, Duckture, Email, Entity, Float, Integer, Model, Number, String, Symbol, Tuple

Class Method Summary collapse

Class Method Details

.[](symbol) ⇒ Object



31
32
33
34
35
# File 'projects/builtin_types/lib/foobara/builtin_types.rb', line 31

def [](symbol)
  builtin_types.find do |type|
    type.type_symbol == symbol
  end
end

.add_builtin_type(type) ⇒ Object



13
14
15
16
17
# File 'projects/builtin_types/lib/foobara/builtin_types.rb', line 13

def add_builtin_type(type)
  @builtin_references = nil
  builtin_types << type
  type.is_builtin = true
end

.build_and_register!(type_symbol, base_type, target_classes = const_get("::#{Util.classify(type_symbol)}"), description: "Built-in #{type_symbol} type", type_module: nil) ⇒ Object

TODO: break this up TODO: much of this behavior is helpful to non-builtin types as well.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'projects/builtin_types/src/builtin_types.rb', line 10

def build_and_register!(
  type_symbol,
  base_type,
  target_classes = const_get("::#{Util.classify(type_symbol)}"),
  description: "Built-in #{type_symbol} type",
  type_module: nil
)
  declaration_data = type_symbol.to_sym

  module_symbol = Util.classify(type_symbol).to_sym

  builtin_type_module = type_module || const_get(module_symbol, false)

  processor_classes_requiring_type = []

  casters_module = Util.constant_value(builtin_type_module, :Casters)
  caster_classes = if casters_module
                     Util.constant_values(casters_module, extends: Value::Processor)
                   end
  casters = []
  caster_classes&.each do |caster_class|
    if caster_class.respond_to?(:requires_type?) && caster_class.requires_type?
      processor_classes_requiring_type << caster_class
    else
      casters << caster_class.new_with_agnostic_args(parent_declaration_data: declaration_data)
    end
  end

  transformers_module = Util.constant_value(builtin_type_module, :Transformers)
  transformer_classes = if transformers_module
                          Util.constant_values(transformers_module, extends: Value::Processor)
                        end
  transformers = []
  transformer_classes&.each do |transformer_class|
    if transformer_class.respond_to?(:requires_type?) && transformer_class.requires_type?
      # :nocov:
      processor_classes_requiring_type << transformer_class
      # :nocov:
    else
      transformers << transformer_class.new_with_agnostic_args(parent_declaration_data: declaration_data)
    end
  end

  validators_module = Util.constant_value(builtin_type_module, :Validators)
  validator_classes = if validators_module
                        Util.constant_values(validators_module, extends: Value::Processor)
                      end
  validators = []
  validator_classes&.each do |validator_class|
    if validator_class.respond_to?(:requires_type?) && validator_class.requires_type?
      # :nocov:
      processor_classes_requiring_type << validator_class
      # :nocov:
    else
      validators << validator_class.new_with_agnostic_args(parent_declaration_data: declaration_data)
    end
  end

  type = Foobara::Types::Type.new(
    declaration_data,
    base_type:,
    name: type_symbol,
    casters:,
    transformers:,
    validators:,
    # TODO: this is for controlling casting or not casting but could give the wrong information from a
    # reflection point of view...
    target_classes:,
    description:,
    processor_classes_requiring_type:
  )

  add_builtin_type(type)

  # TODO: is this necessary?
  Foobara::Namespace::NamespaceHelpers.foobara_namespace!(type)

  # TODO: really need to encapsulate this somehow...
  type.type_symbol = type_symbol
  (type.foobara_parent_namespace || GlobalDomain).foobara_register(type)

  supported_casters_module = Util.constant_value(builtin_type_module, :SupportedCasters)
  supported_caster_classes = if supported_casters_module
                               Util.constant_values(supported_casters_module, extends: Value::Processor)
                             end
  supported_transformers_module = Util.constant_value(builtin_type_module, :SupportedTransformers)
  supported_transformer_classes = if supported_transformers_module
                                    Util.constant_values(supported_transformers_module, extends: Value::Processor)
                                  end
  supported_validators_module = Util.constant_value(builtin_type_module, :SupportedValidators)
  supported_validator_classes = if supported_validators_module
                                  Util.constant_values(supported_validators_module, extends: Value::Processor)
                                end
  supported_processors_module = Util.constant_value(builtin_type_module, :SupportedProcessors)
  supported_processor_classes = if supported_processors_module
                                  Util.constant_values(supported_processors_module, extends: Value::Processor)
                                end

  processor_classes = [*transformers, *validators].map(&:class)

  [
    *supported_caster_classes,
    *supported_transformer_classes,
    *supported_validator_classes,
    *supported_processor_classes
  ].each do |processor_class|
    type.register_supported_processor_class(processor_class)
    processor_classes << processor_class
  end

  processor_classes.each do |processor_class|
    install_type_declaration_extensions_for(processor_class)
  end

  [
    [casters_module, caster_classes, casters],
    [transformers_module, transformer_classes, transformers],
    [validators_module, validator_classes, validators],
    [supported_casters_module, supported_caster_classes],
    [supported_processors_module, supported_processor_classes],
    [supported_transformers_module, supported_transformer_classes],
    [supported_validators_module, supported_validator_classes]
  ].each do |(mod, klasses, instances)|
    next unless mod

    prefix = Util.non_full_name(mod)

    [*klasses, *instances].each do |scoped|
      if !scoped.scoped_path_set? || scoped.scoped_path_autoset?
        # TODO: Do we actually need this?
        short_name = Util.non_full_name(scoped)
        short_name = Util.underscore(short_name) unless scoped.is_a?(::Class)

        scoped.scoped_path = [prefix, short_name]
      end

      parent = scoped.scoped_namespace

      if parent.nil? || parent == Foobara || parent == Namespace.global || parent == GlobalDomain
        type.foobara_register(scoped)
      end
    end
  end

  type
end

.builtin?(type) ⇒ Boolean

Returns:



23
24
25
# File 'projects/builtin_types/lib/foobara/builtin_types.rb', line 23

def builtin?(type)
  builtin_types.include?(type)
end

.builtin_reference?(reference) ⇒ Boolean

Returns:



27
28
29
# File 'projects/builtin_types/lib/foobara/builtin_types.rb', line 27

def builtin_reference?(reference)
  builtin_references.include?(reference)
end

.builtin_referencesObject



19
20
21
# File 'projects/builtin_types/lib/foobara/builtin_types.rb', line 19

def builtin_references
  @builtin_references ||= builtin_types.to_set(&:foobara_manifest_reference)
end

.builtin_typesObject



9
10
11
# File 'projects/builtin_types/lib/foobara/builtin_types.rb', line 9

def builtin_types
  @builtin_types ||= Set.new
end

.install!Object



37
38
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
# File 'projects/builtin_types/lib/foobara/builtin_types.rb', line 37

def install!
  duck = build_and_register!(:duck, nil, ::Object)
  # TODO: should we ban ::Object that are ::Enumerable from atomic_duck?
  atomic_duck = build_and_register!(:atomic_duck, duck, ::Object)
  build_and_register!(:symbol, atomic_duck)
  # TODO: wtf why pass ::Object? It's to avoid casting? Do we need a way to flag abstract types?
  number = build_and_register!(:number, atomic_duck, ::Object)
  build_and_register!(:integer, number)
  build_and_register!(:float, number)
  build_and_register!(:big_decimal, number)
  # Let's skip these for now since they rarely come up in business contexts and both could be
  # represented by a tuple of numbers.
  # build_and_register!(:rational, number)
  # build_and_register!(:complex, number)
  string = build_and_register!(:string, atomic_duck)
  build_and_register!(:date, atomic_duck)
  build_and_register!(:datetime, atomic_duck, ::Time)
  build_and_register!(:boolean, atomic_duck, [::TrueClass, ::FalseClass])
  build_and_register!(:email, string, ::String)
  # TODO: not urgent and derisked already via :email
  # phone_number = build_and_register!(:phone_number, string)
  # TODO: wtf
  duckture = build_and_register!(:duckture, duck, ::Object)
  array = build_and_register!(:array, duckture)
  build_and_register!(:tuple, array, ::Array)
  associative_array = build_and_register!(:associative_array, duckture, ::Hash)
  # TODO: uh oh... we do some translations in the casting here...
  build_and_register!(:attributes, associative_array, nil)
end

.reset_allObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'projects/builtin_types/lib/foobara/builtin_types.rb', line 67

def reset_all
  Foobara.raise_if_production!("reset_all")

  builtin_types.each do |builtin_type|
    builtin_type.foobara_each do |scoped|
      if scoped.scoped_namespace == builtin_type
        scoped.scoped_namespace = nil
      end
    end
  end

  builtin_types.clear

  install!
end