Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/tree_sitter_language_pack/native.rb

Overview

Add accessor methods to Hash-based internally-tagged enum instances

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

Support internally-tagged enum accessors like format.excel, format.email, etc. Also support direct field access like format.sheet_count rubocop:disable Metrics/CyclomaticComplexity



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/tree_sitter_language_pack/native.rb', line 16

def method_missing(method_name, *args, &)
  # Try symbol key first (how Magnus converts JSON keys)
  return self[method_name] if key?(method_name)

  # Try string key
  return self[method_name.to_s] if key?(method_name.to_s)

  # Check if this hash has a 'format_type' field (indicating an internally-tagged enum)
  format_type = self[:format_type] || self['format_type']
  return super unless format_type

  # If the method name matches the format_type (snake_case), extract and return the variant's wrapped data
  # Internally-tagged enums store variant data in the '_0' field (from alef's struct variant conversion)
  # This allows format.excel to return the ExcelMetadata hash with sheet_count, sheet_names, etc.
  snake_case_method = method_name.to_s.downcase
  return self[:_0] || self['_0'] || self if snake_case_method == format_type.to_s.downcase

  super
end

Instance Method Details

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

rubocop:enable Metrics/CyclomaticComplexity

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
# File 'lib/tree_sitter_language_pack/native.rb', line 37

def respond_to_missing?(method_name, include_private = false)
  return true if key?(method_name) || key?(method_name.to_s)

  format_type = self[:format_type] || self['format_type']
  return false unless format_type

  snake_case_method = method_name.to_s.downcase
  snake_case_method == format_type.to_s.downcase || super
end