Class: Lutaml::ModelTransformations::Parsers::BaseParser Abstract
- Inherits:
-
Object
- Object
- Lutaml::ModelTransformations::Parsers::BaseParser
- Defined in:
- lib/lutaml/model_transformations/parsers/base_parser.rb
Overview
Subclass and implement the abstract methods
Base parser interface defining the contract for all model format parsers.
This abstract base class implements the Template Method pattern and follows the Liskov Substitution Principle - all concrete parsers must be substitutable for this base class.
Concrete parsers must implement:
-
parse_internal: Core parsing logic
-
supported_extensions: List of supported file extensions
-
format_name: Human-readable format name
Instance Attribute Summary collapse
-
#configuration ⇒ Configuration
readonly
Parser configuration.
-
#options ⇒ Hash
readonly
Parsing options.
Instance Method Summary collapse
-
#can_parse?(file_path) ⇒ Boolean
Check if this parser can handle the given file.
-
#errors ⇒ Array<String>
Get all parsing errors.
-
#format_name ⇒ String
abstract
Get parser format name.
-
#has_errors? ⇒ Boolean
Check if parser has any errors.
-
#has_warnings? ⇒ Boolean
Check if parser has any warnings.
-
#initialize(configuration: nil, options: {}) ⇒ BaseParser
constructor
Initialize parser with configuration and options.
-
#parse(file_path) ⇒ Lutaml::Uml::Document
Parse a model file into a UML document.
-
#priority ⇒ Object
Default parser priority.
-
#statistics ⇒ Hash
Get parsing statistics.
-
#supported_extensions ⇒ Array<String>
abstract
Get list of supported file extensions.
-
#warnings ⇒ Array<String>
Get all parsing warnings.
Constructor Details
#initialize(configuration: nil, options: {}) ⇒ BaseParser
Initialize parser with configuration and options
30 31 32 33 34 35 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 30 def initialize(configuration: nil, options: {}) @configuration = configuration @options = .merge() @errors = [] @warnings = [] end |
Instance Attribute Details
#configuration ⇒ Configuration (readonly)
Returns Parser configuration.
21 22 23 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 21 def configuration @configuration end |
#options ⇒ Hash (readonly)
Returns Parsing options.
24 25 26 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 24 def @options end |
Instance Method Details
#can_parse?(file_path) ⇒ Boolean
Check if this parser can handle the given file
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 73 def can_parse?(file_path) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity extension = File.extname(file_path).downcase return true if supported_extensions.include?(extension) if respond_to?(:content_patterns) && File.exist?(file_path) File.open(file_path, "rb") do |file| header = file.read(1024) # Read first 1KB return false if header.nil? || header.empty? content_patterns.each do |pattern| return true if header.match?(pattern) end end end false end |
#errors ⇒ Array<String>
Get all parsing errors
130 131 132 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 130 def errors @errors.dup end |
#format_name ⇒ String
Implement in subclass
Get parser format name
95 96 97 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 95 def format_name raise NotImplementedError, "Subclasses must implement #format_name" end |
#has_errors? ⇒ Boolean
Check if parser has any errors
116 117 118 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 116 def has_errors? !@errors.empty? end |
#has_warnings? ⇒ Boolean
Check if parser has any warnings
123 124 125 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 123 def has_warnings? !@warnings.empty? end |
#parse(file_path) ⇒ Lutaml::Uml::Document
Parse a model file into a UML document
This is the main public interface method that implements the Template Method pattern. It handles common concerns like validation, error handling, and post-processing.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 46 def parse(file_path) # rubocop:disable Metrics/MethodLength validate_file!(file_path) if should_validate_input? clear_errors_and_warnings begin # Pre-parsing hook before_parse(file_path) # Core parsing (implemented by subclasses) document = parse_internal(file_path) # Post-parsing processing document = after_parse(document, file_path) # Validate output if requested validate_output!(document) if should_validate_output? document rescue StandardError => e handle_parsing_error(e, file_path) end end |
#priority ⇒ Object
Default parser priority
109 110 111 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 109 def priority 100 end |
#statistics ⇒ Hash
Get parsing statistics
144 145 146 147 148 149 150 151 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 144 def statistics { format: format_name, errors: @errors.size, warnings: @warnings.size, options: @options, } end |
#supported_extensions ⇒ Array<String>
Implement in subclass
Get list of supported file extensions
103 104 105 106 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 103 def supported_extensions raise NotImplementedError, "Subclasses must implement #supported_extensions" end |
#warnings ⇒ Array<String>
Get all parsing warnings
137 138 139 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 137 def warnings @warnings.dup end |