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.
-
#last_duration ⇒ Float?
readonly
Duration of last parse in seconds.
-
#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.
- #execute_parse(file_path) ⇒ Object
-
#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.
- #record_parse_stats(succeeded, handled, start_time) ⇒ Object
-
#reset_statistics ⇒ void
Reset all parsing statistics.
-
#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
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 33 def initialize(configuration: nil, options: {}) @configuration = configuration @options = .merge() @errors = [] @warnings = [] @parse_stats = { total_parses: 0, successful_parses: 0, failed_parses: 0, total_duration: 0, durations: [], } @last_duration = nil @stats_mutex = Mutex.new 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 |
#last_duration ⇒ Float? (readonly)
Returns Duration of last parse in seconds.
27 28 29 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 27 def last_duration @last_duration 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
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 104 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 self.class.method_defined?(: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
161 162 163 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 161 def errors @errors.dup end |
#execute_parse(file_path) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 76 def execute_parse(file_path) before_parse(file_path) document = parse_internal(file_path) document = after_parse(document, file_path) validate_output!(document) if should_validate_output? @stats_mutex.synchronize { @parse_stats[:successful_parses] += 1 } [true, document] rescue StandardError => e [false, handle_parsing_error(e, file_path)] end |
#format_name ⇒ String
Implement in subclass
Get parser format name
126 127 128 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 126 def format_name raise NotImplementedError, "Subclasses must implement #format_name" end |
#has_errors? ⇒ Boolean
Check if parser has any errors
147 148 149 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 147 def has_errors? !@errors.empty? end |
#has_warnings? ⇒ Boolean
Check if parser has any warnings
154 155 156 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 154 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.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 58 def parse(file_path) start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) @stats_mutex.synchronize { @parse_stats[:total_parses] += 1 } parse_succeeded = false parse_handled = false begin validate_file!(file_path) if should_validate_input? clear_errors_and_warnings parse_succeeded, result = execute_parse(file_path) parse_handled = !parse_succeeded result ensure record_parse_stats(parse_succeeded, parse_handled, start_time) end end |
#priority ⇒ Object
Default parser priority
140 141 142 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 140 def priority 100 end |
#record_parse_stats(succeeded, handled, start_time) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 88 def record_parse_stats(succeeded, handled, start_time) unless succeeded || handled @stats_mutex.synchronize { @parse_stats[:failed_parses] += 1 } end duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time @last_duration = duration @stats_mutex.synchronize do @parse_stats[:total_duration] += duration @parse_stats[:durations] << duration end end |
#reset_statistics ⇒ void
This method returns an undefined value.
Reset all parsing statistics
195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 195 def reset_statistics @stats_mutex.synchronize do @parse_stats = { total_parses: 0, successful_parses: 0, failed_parses: 0, total_duration: 0, durations: [], } end @last_duration = nil end |
#statistics ⇒ Hash
Get parsing statistics
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 175 def statistics stats = @stats_mutex.synchronize { @parse_stats.dup } durations = stats[:durations] { format: format_name, errors: @errors.size, warnings: @warnings.size, options: @options, total_parses: stats[:total_parses], successful_parses: stats[:successful_parses], failed_parses: stats[:failed_parses], success_rate: calculate_success_rate(stats), average_duration: calculate_average_duration(durations), total_duration: stats[:total_duration], } end |
#supported_extensions ⇒ Array<String>
Implement in subclass
Get list of supported file extensions
134 135 136 137 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 134 def supported_extensions raise NotImplementedError, "Subclasses must implement #supported_extensions" end |
#warnings ⇒ Array<String>
Get all parsing warnings
168 169 170 |
# File 'lib/lutaml/model_transformations/parsers/base_parser.rb', line 168 def warnings @warnings.dup end |