Class: Ast::Merge::RegionDetectorBase Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/ast/merge/region_detector_base.rb

Overview

This class is abstract.

Subclass and implement #region_type and #detect_all

Base class for region detection.

Region detectors identify portions of a document that should be handled by a specialized merger. For example, detecting YAML frontmatter in a Markdown file, or Ruby code blocks that should be merged with Prism.

Subclasses must implement:

  • #region_type - Returns the type symbol for detected regions

  • #detect_all - Finds all regions of this type in a document

Examples:

Implementing a custom detector

class MyBlockDetector < Ast::Merge::RegionDetectorBase
  def region_type
    :my_block
  end

  def detect_all(source)
    # Return array of Region structs
    []
  end
end

Instance Method Summary collapse

Instance Method Details

#detect_all(source) ⇒ Array<Region>

This method is abstract.

Subclasses must implement this method

Detects all regions of this type in the given source.

Examples:

Return value structure

[
  Region.new(
    type: :yaml_frontmatter,
    content: "title: My Doc\n",
    start_line: 1,
    end_line: 3,
    delimiters: { start: "---\n", end: "---\n" },
    metadata: { format: :yaml }
  )
]

Parameters:

  • source (String)

    The full document content to scan

Returns:

  • (Array<Region>)

    All detected regions, sorted by start_line

Raises:

  • (NotImplementedError)


58
59
60
# File 'lib/ast/merge/region_detector_base.rb', line 58

def detect_all(source)
  raise NotImplementedError, "#{self.class}#detect_all must be implemented"
end

#inspectString

Returns a string representation of this detector.

Returns:

  • (String)

    A description of the detector



87
88
89
# File 'lib/ast/merge/region_detector_base.rb', line 87

def inspect
  "#<#{name} region_type=#{region_type}>"
end

#nameString

A human-readable name for this detector.

Used in error messages and debugging output.

Returns:

  • (String)

    The detector name



80
81
82
# File 'lib/ast/merge/region_detector_base.rb', line 80

def name
  self.class.name || "AnonymousDetector"
end

#region_typeSymbol

This method is abstract.

Subclasses must implement this method

Returns the type symbol for regions detected by this detector.

This symbol is used to identify the region type in the Region struct and for matching regions between template and destination documents.

Returns:

  • (Symbol)

    The region type (e.g., :yaml_frontmatter, :ruby_code_block)

Raises:

  • (NotImplementedError)


37
38
39
# File 'lib/ast/merge/region_detector_base.rb', line 37

def region_type
  raise NotImplementedError, "#{self.class}#region_type must be implemented"
end

#strip_delimiters?Boolean

Whether to strip delimiters from content before passing to merger.

When true (default), only the inner content is passed to the region’s merger. The delimiters are stored separately and reattached after merging.

When false, the full content including delimiters is passed to the merger, which must then handle the delimiters itself.

Returns:

  • (Boolean)

    true if delimiters should be stripped (default: true)



71
72
73
# File 'lib/ast/merge/region_detector_base.rb', line 71

def strip_delimiters?
  true
end