Class: TreeHaver::Base::Language Abstract
- Inherits:
-
Object
- Object
- TreeHaver::Base::Language
- Includes:
- Comparable
- Defined in:
- lib/tree_haver/base/language.rb
Overview
Subclasses must implement #name and #backend at minimum
Base class for backend Language implementations
This class defines the API contract for all language implementations. Backend-specific Language classes should inherit from this and implement the required interface.
Direct Known Subclasses
TreeHaver::Backends::Prism::Language, TreeHaver::Backends::Psych::Language
Instance Attribute Summary collapse
-
#backend ⇒ Symbol
readonly
The backend this language is for.
-
#name ⇒ Symbol
(also: #language_name)
readonly
The language name (e.g., :markdown, :ruby, :json).
-
#options ⇒ Hash
readonly
Language-specific options.
Class Method Summary collapse
-
.from_library(_path = nil, symbol: nil, name: nil) ⇒ Language
Load a language from a library path (factory method).
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer?
Comparison based on backend then name.
-
#eql?(other) ⇒ Boolean
Equality check for Hash keys.
-
#hash ⇒ Integer
Hash value for use in Sets/Hashes.
-
#initialize(name, backend:, options: {}) ⇒ Language
constructor
Create a new Language instance.
-
#inspect ⇒ String
Human-readable representation.
Constructor Details
#initialize(name, backend:, options: {}) ⇒ Language
Create a new Language instance
32 33 34 35 36 |
# File 'lib/tree_haver/base/language.rb', line 32 def initialize(name, backend:, options: {}) @name = name.to_sym @backend = backend.to_sym @options = end |
Instance Attribute Details
#backend ⇒ Symbol (readonly)
The backend this language is for
21 22 23 |
# File 'lib/tree_haver/base/language.rb', line 21 def backend @backend end |
#name ⇒ Symbol (readonly) Also known as: language_name
The language name (e.g., :markdown, :ruby, :json)
17 18 19 |
# File 'lib/tree_haver/base/language.rb', line 17 def name @name end |
#options ⇒ Hash (readonly)
Language-specific options
25 26 27 |
# File 'lib/tree_haver/base/language.rb', line 25 def @options end |
Class Method Details
.from_library(_path = nil, symbol: nil, name: nil) ⇒ Language
Load a language from a library path (factory method)
For pure-Ruby backends (Commonmarker, Markly, Prism, Psych), this typically ignores the path and returns the single supported language.
For tree-sitter backends (MRI, Rust, FFI, Java), this loads the language from the shared library file.
92 93 94 |
# File 'lib/tree_haver/base/language.rb', line 92 def from_library(_path = nil, symbol: nil, name: nil) raise NotImplementedError, "#{self}.from_library must be implemented" end |
Instance Method Details
#<=>(other) ⇒ Integer?
Comparison based on backend then name
46 47 48 49 50 51 |
# File 'lib/tree_haver/base/language.rb', line 46 def <=>(other) return unless other.is_a?(Language) return unless other.respond_to?(:backend) && other.backend == backend name <=> other.name end |
#eql?(other) ⇒ Boolean
Equality check for Hash keys
62 63 64 65 66 |
# File 'lib/tree_haver/base/language.rb', line 62 def eql?(other) return false unless other.is_a?(Language) backend == other.backend && name == other.name && == other. end |
#hash ⇒ Integer
Hash value for use in Sets/Hashes
55 56 57 |
# File 'lib/tree_haver/base/language.rb', line 55 def hash [backend, name, .to_a.sort].hash end |
#inspect ⇒ String
Human-readable representation
70 71 72 73 74 |
# File 'lib/tree_haver/base/language.rb', line 70 def inspect opts = .empty? ? "" : " options=#{}" class_name = self.class.name || "#{self.class.superclass.name}(anonymous)" "#<#{class_name} name=#{name} backend=#{backend}#{opts}>" end |