Class: Kumi::Core::RubyParser::InputBuilder

Inherits:
Object
  • Object
show all
Includes:
ErrorReporting, Syntax
Defined in:
lib/kumi/core/ruby_parser/input_builder.rb

Instance Method Summary collapse

Methods included from ErrorReporting

#inferred_location, #raise_localized_error, #raise_syntax_error, #raise_type_error, #report_enhanced_error, #report_error, #report_semantic_error, #report_syntax_error, #report_type_error

Constructor Details

#initialize(context) ⇒ InputBuilder

Returns a new instance of InputBuilder.



10
11
12
# File 'lib/kumi/core/ruby_parser/input_builder.rb', line 10

def initialize(context)
  @context = context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *_args) ⇒ Object



63
64
65
66
67
# File 'lib/kumi/core/ruby_parser/input_builder.rb', line 63

def method_missing(method_name, *_args)
  allowed_methods = "'key', 'integer', 'float', 'decimal', 'string', 'boolean', 'any', 'scalar', 'array', 'hash', and 'element'"
  raise_syntax_error("Unknown method '#{method_name}' in input block. Only #{allowed_methods} are allowed.",
                     location: @context.current_location)
end

Instance Method Details

#array(name_or_elem_type, **kwargs) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kumi/core/ruby_parser/input_builder.rb', line 26

def array(name_or_elem_type, **kwargs, &)
  if block_given?
    create_array_field_with_block(name_or_elem_type, kwargs, &)
  elsif kwargs.any?
    create_array_field(name_or_elem_type, kwargs)
  elsif name_or_elem_type.is_a?(Symbol)
    # A bare `array :xs` input with no block names the array but not its
    # element. Kumi maps over arrays by default, so the element MUST be
    # named — that name is the per-element binding you reference in the
    # body. Guide the user to the one-child form.
    raise_syntax_error(
      "Array input '#{name_or_elem_type}' needs a block that names its element. " \
      "Kumi maps over arrays by default, so the element needs a name to map onto, e.g.\n" \
      "  array :#{name_or_elem_type} do\n" \
      "    float :value          # scalar element, referenced as input.#{name_or_elem_type}.value\n" \
      "  end\n" \
      "Use a `hash` child when each element has several fields, " \
      "or a nested `array` child for an array of arrays.",
      location: @context.current_location
    )
  else
    Kumi::Core::Types.array(name_or_elem_type)
  end
end

#hash(name_or_key_type, val_type = nil, **kwargs) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/kumi/core/ruby_parser/input_builder.rb', line 51

def hash(name_or_key_type, val_type = nil, **kwargs, &)
  if block_given?
    create_hash_field_with_block(name_or_key_type, kwargs, &)
  elsif val_type.nil? && name_or_key_type.is_a?(Symbol)
    create_bare_hash_field(name_or_key_type, kwargs)
  elsif val_type.nil?
    create_hash_field(name_or_key_type, kwargs)
  else
    Kumi::Core::Types.hash(name_or_key_type, val_type)
  end
end

#key(name, type: :any, domain: nil) ⇒ Object



14
15
16
17
# File 'lib/kumi/core/ruby_parser/input_builder.rb', line 14

def key(name, type: :any, domain: nil)
  normalized_type = normalize_type(type, name)
  @context.inputs << Kumi::Syntax::InputDeclaration.new(name, domain, normalized_type, [], nil, loc: @context.current_location)
end

#respond_to_missing?(_method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/kumi/core/ruby_parser/input_builder.rb', line 69

def respond_to_missing?(_method_name, _include_private = false)
  false
end