Class: Foobara::TypeDeclarations::TypeDeclarationHandler

Inherits:
Value::Processor::Pipeline show all
Includes:
WithRegistries
Defined in:
foobara-0.5.8/projects/typesystem/projects/type_declarations/src/type_declaration_handler.rb

Direct Known Subclasses

Handlers::RegisteredTypeDeclaration

Instance Attribute Summary collapse

Attributes inherited from Value::Processor::Multi

#prioritize

Attributes inherited from Value::Processor

#created_in_namespace, #declaration_data, #parent_declaration_data

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WithRegistries

#handler_for_class, #lookup_absolute_type!, #lookup_type, #lookup_type!, #type_declaration_handler_for, #type_for_declaration, #type_registered?

Methods included from Concern

foobara_class_methods_module_for, foobara_concern?, included

Methods inherited from Value::Processor::Pipeline

#process_outcome, #process_value

Methods inherited from Value::Processor::Multi

#error_classes, #possible_errors, #processor_names, #register, requires_declaration_data?

Methods inherited from Value::Processor

#always_applicable?, #attribute_name, #build_error, default_declaration_data, #dup_processor, error_class, #error_class, error_classes, #error_classes, #error_context, #error_message, #error_path, #foobara_manifest, instance, #method_missing, #name, new_with_agnostic_args, #possible_errors, #priority, #process_outcome, #process_outcome!, #process_value, #process_value!, processor_name, requires_declaration_data?, #requires_declaration_data?, requires_parent_declaration_data?, #requires_parent_declaration_data?, #respond_to_missing?, #runner, symbol, #symbol

Methods included from IsManifestable

#foobara_domain, #foobara_manifest, #foobara_organization, #scoped_clear_caches

Constructor Details

#initialize(*args, processors: nil, desugarizers: starting_desugarizers, type_declaration_validators: starting_type_declaration_validators, **opts) ⇒ TypeDeclarationHandler

Returns a new instance of TypeDeclarationHandler.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'projects/typesystem/projects/type_declarations/src/type_declaration_handler.rb', line 46

def initialize(
  *args,
  processors: nil,
  desugarizers: starting_desugarizers,
  type_declaration_validators: starting_type_declaration_validators,
  **opts
)
  if processors && !processors.empty?
    # :nocov:
    raise ArgumentError, "Cannot set processors directly for a type declaration handler"
    # :nocov:
  end

  self.desugarizers = Util.array(desugarizers)
  self.type_declaration_validators = Util.array(type_declaration_validators)

  super(*Util.args_and_opts_to_args(args, opts))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Foobara::Value::Processor

Instance Attribute Details

#desugarizersObject

Returns the value of attribute desugarizers.



44
45
46
# File 'projects/typesystem/projects/type_declarations/src/type_declaration_handler.rb', line 44

def desugarizers
  @desugarizers
end

#type_declaration_validatorsObject

Returns the value of attribute type_declaration_validators.



44
45
46
# File 'projects/typesystem/projects/type_declarations/src/type_declaration_handler.rb', line 44

def type_declaration_validators
  @type_declaration_validators
end

Class Method Details

.foobara_manifestObject



9
10
11
12
13
# File 'projects/typesystem/projects/type_declarations/src/type_declaration_handler.rb', line 9

def foobara_manifest
  # :nocov:
  super.merge(processor_type: :type_declaration_handler)
  # :nocov:
end

.starting_desugarizersObject



15
16
17
# File 'projects/typesystem/projects/type_declarations/src/type_declaration_handler.rb', line 15

def starting_desugarizers
  starting_desugarizers_with_inherited
end

.starting_desugarizers_with_inheritedObject



19
20
21
22
23
24
25
26
27
# File 'projects/typesystem/projects/type_declarations/src/type_declaration_handler.rb', line 19

def starting_desugarizers_with_inherited
  # TODO: this is not great because if new stuff gets registered at runtime then we can't really
  # update this cached data easily
  if superclass == TypeDeclarationHandler
    starting_desugarizers_without_inherited
  else
    [*superclass.starting_desugarizers, *starting_desugarizers_without_inherited]
  end
end

.starting_desugarizers_without_inheritedObject



29
30
31
32
33
# File 'projects/typesystem/projects/type_declarations/src/type_declaration_handler.rb', line 29

def starting_desugarizers_without_inherited
  # TODO: this is not great because if new stuff gets registered at runtime then we can't really
  # update this cached data easily
  Util.constant_values(self, extends: TypeDeclarations::Desugarizer).map(&:instance)
end

.starting_type_declaration_validatorsObject



35
36
37
38
39
# File 'projects/typesystem/projects/type_declarations/src/type_declaration_handler.rb', line 35

def starting_type_declaration_validators
  # TODO: this is not great because if new stuff gets registered at runtime then we can't really
  # update this cached data easily
  Util.constant_values(self, extends: Value::Validator, inherit: true).map(&:instance)
end

Instance Method Details

#applicable?(_sugary_type_declaration) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
104
105
# File 'projects/typesystem/projects/type_declarations/src/type_declaration_handler.rb', line 101

def applicable?(_sugary_type_declaration)
  # :nocov:
  raise "subclass responsibility"
  # :nocov:
end

#desugarize(value) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'projects/typesystem/projects/type_declarations/src/type_declaration_handler.rb', line 116

def desugarize(value)
  unless value.strict?
    if desugarizer.applicable?(value)
      value = desugarizer.process_value!(value)
      value.is_strict = true
    end
  end

  value
end

#desugarizerObject



111
112
113
114
# File 'projects/typesystem/projects/type_declarations/src/type_declaration_handler.rb', line 111

def desugarizer
  # TODO: memoize this?
  DesugarizerPipeline.new(processors: desugarizers)
end

#inspectObject



89
90
91
92
93
94
95
96
97
98
99
# File 'projects/typesystem/projects/type_declarations/src/type_declaration_handler.rb', line 89

def inspect
  # :nocov:
  s = super

  if s.size > 400
    s = "#{s[0..400]}..."
  end

  s
  # :nocov:
end

#processorsObject



107
108
109
# File 'projects/typesystem/projects/type_declarations/src/type_declaration_handler.rb', line 107

def processors
  [desugarizer, type_declaration_validator, to_type_transformer]
end

#starting_desugarizersObject



65
66
67
# File 'projects/typesystem/projects/type_declarations/src/type_declaration_handler.rb', line 65

def starting_desugarizers
  self.class.starting_desugarizers
end

#starting_desugarizers_with_inheritedObject



69
70
71
72
73
# File 'projects/typesystem/projects/type_declarations/src/type_declaration_handler.rb', line 69

def starting_desugarizers_with_inherited
  # :nocov:
  self.class.starting_desugarizers_with_inherited
  # :nocov:
end

#starting_desugarizers_without_inheritedObject



75
76
77
78
79
# File 'projects/typesystem/projects/type_declarations/src/type_declaration_handler.rb', line 75

def starting_desugarizers_without_inherited
  # :nocov:
  self.class.starting_desugarizers_without_inherited
  # :nocov:
end

#starting_type_declaration_validatorsObject



81
82
83
# File 'projects/typesystem/projects/type_declarations/src/type_declaration_handler.rb', line 81

def starting_type_declaration_validators
  self.class.starting_type_declaration_validators
end

#to_type_transformerObject



85
86
87
# File 'projects/typesystem/projects/type_declarations/src/type_declaration_handler.rb', line 85

def to_type_transformer
  self.class::ToTypeTransformer.instance
end

#type_declaration_validatorObject



127
128
129
130
# File 'projects/typesystem/projects/type_declarations/src/type_declaration_handler.rb', line 127

def type_declaration_validator
  # TODO: memoize this
  Value::Processor::Pipeline.new(processors: type_declaration_validators)
end