Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/html_to_markdown/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, &block) ⇒ 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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/html_to_markdown/native.rb', line 27

def method_missing(method_name, *args, &block)
  # 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
  if snake_case_method == format_type.to_s.downcase
    return self[:'_0'] || self['_0'] || self
  end

  super
end

Instance Method Details

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

rubocop:enable Metrics/CyclomaticComplexity

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
# File 'lib/html_to_markdown/native.rb', line 50

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