Class: Foobara::TypeDeclarations::TypeDeclarationHandler

Inherits:
Value::Processor::Pipeline show all
Includes:
WithRegistries
Defined in:
foobara-0.0.110/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 Concern

foobara_class_methods_module_for, foobara_concern?, included

Methods inherited from Value::Processor::Pipeline

#process_outcome

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_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!, processor_name, requires_declaration_data?, requires_parent_declaration_data?, #respond_to_missing?, #runner, 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.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'foobara-0.0.110/projects/type_declarations/src/type_declaration_handler.rb', line 44

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.



42
43
44
# File 'foobara-0.0.110/projects/type_declarations/src/type_declaration_handler.rb', line 42

def desugarizers
  @desugarizers
end

#type_declaration_validatorsObject

Returns the value of attribute type_declaration_validators.



42
43
44
# File 'foobara-0.0.110/projects/type_declarations/src/type_declaration_handler.rb', line 42

def type_declaration_validators
  @type_declaration_validators
end

Class Method Details

.foobara_manifestObject



7
8
9
10
11
# File 'foobara-0.0.110/projects/type_declarations/src/type_declaration_handler.rb', line 7

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

.starting_desugarizersObject



13
14
15
# File 'foobara-0.0.110/projects/type_declarations/src/type_declaration_handler.rb', line 13

def starting_desugarizers
  starting_desugarizers_with_inherited
end

.starting_desugarizers_with_inheritedObject



17
18
19
20
21
22
23
24
25
# File 'foobara-0.0.110/projects/type_declarations/src/type_declaration_handler.rb', line 17

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



27
28
29
30
31
# File 'foobara-0.0.110/projects/type_declarations/src/type_declaration_handler.rb', line 27

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



33
34
35
36
37
# File 'foobara-0.0.110/projects/type_declarations/src/type_declaration_handler.rb', line 33

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)


85
86
87
88
89
# File 'foobara-0.0.110/projects/type_declarations/src/type_declaration_handler.rb', line 85

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

#desugarize(value) ⇒ Object



109
110
111
# File 'foobara-0.0.110/projects/type_declarations/src/type_declaration_handler.rb', line 109

def desugarize(value)
  desugarizer.process_value!(value)
end

#desugarizerObject



104
105
106
107
# File 'foobara-0.0.110/projects/type_declarations/src/type_declaration_handler.rb', line 104

def desugarizer
  # TODO: memoize this?
  Value::Processor::Pipeline.new(processors: desugarizers)
end

#inspectObject



73
74
75
76
77
78
79
80
81
82
83
# File 'foobara-0.0.110/projects/type_declarations/src/type_declaration_handler.rb', line 73

def inspect
  # :nocov:
  s = super

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

  s
  # :nocov:
end

#process_value(raw_type_declaration) ⇒ Object



95
96
97
98
99
100
101
102
# File 'foobara-0.0.110/projects/type_declarations/src/type_declaration_handler.rb', line 95

def process_value(raw_type_declaration)
  # TODO: deep_dup this again??
  super.tap do |type_outcome|
    if type_outcome.success?
      type_outcome.result.raw_declaration_data = raw_type_declaration
    end
  end
end

#processorsObject



91
92
93
# File 'foobara-0.0.110/projects/type_declarations/src/type_declaration_handler.rb', line 91

def processors
  [desugarizer, type_declaration_validator, to_type_transformer]
end

#to_type_transformerObject



69
70
71
# File 'foobara-0.0.110/projects/type_declarations/src/type_declaration_handler.rb', line 69

def to_type_transformer
  self.class::ToTypeTransformer.instance
end

#type_declaration_validatorObject



113
114
115
# File 'foobara-0.0.110/projects/type_declarations/src/type_declaration_handler.rb', line 113

def type_declaration_validator
  Value::Processor::Pipeline.new(processors: type_declaration_validators)
end