Class: Openphar::Parsers::BaseMonographParser

Inherits:
Object
  • Object
show all
Defined in:
lib/openphar/parsers/base_monograph_parser.rb

Overview

Abstract base class for all monograph parsers.

Defines the contract that all parsers must follow:

  • Return Lutaml::Model instances (not Hashes)
  • Track parsing statistics
  • Handle errors consistently

Examples:

Creating a custom parser

class MyParser < BaseMonographParser
  def parse(source)
    # Parse source and return model instances
    MyMonograph.new(**attributes)
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBaseMonographParser

Returns a new instance of BaseMonographParser.



22
23
24
25
26
27
28
29
# File 'lib/openphar/parsers/base_monograph_parser.rb', line 22

def initialize
  @stats = {
    total: 0,
    parsed: 0,
    failed: 0,
    errors: []
  }
end

Instance Attribute Details

#statsObject (readonly)

Returns the value of attribute stats.



20
21
22
# File 'lib/openphar/parsers/base_monograph_parser.rb', line 20

def stats
  @stats
end

Instance Method Details

#parse(source) ⇒ Lutaml::Model::Serializable?

Parse a single source (string, file content, etc.) Subclasses MUST implement this method.

Parameters:

  • source (String)

    The source content to parse

Returns:

  • (Lutaml::Model::Serializable, nil)

    A model instance or nil on failure

Raises:

  • (NotImplementedError)

    If not implemented by subclass



37
38
39
# File 'lib/openphar/parsers/base_monograph_parser.rb', line 37

def parse(source)
  raise NotImplementedError, 'Subclasses must implement #parse'
end

#parse_directory(directory) ⇒ Array<Lutaml::Model::Serializable>

Parse all files in a directory. Subclasses MUST implement this method.

Parameters:

  • directory (String)

    Path to the directory containing files

Returns:

  • (Array<Lutaml::Model::Serializable>)

    Array of model instances

Raises:

  • (NotImplementedError)

    If not implemented by subclass



57
58
59
# File 'lib/openphar/parsers/base_monograph_parser.rb', line 57

def parse_directory(directory)
  raise NotImplementedError, 'Subclasses must implement #parse_directory'
end

#parse_file(file_path) ⇒ Lutaml::Model::Serializable?

Parse a single file. Subclasses MUST implement this method.

Parameters:

  • file_path (String)

    Path to the file to parse

Returns:

  • (Lutaml::Model::Serializable, nil)

    A model instance or nil on failure

Raises:

  • (NotImplementedError)

    If not implemented by subclass



47
48
49
# File 'lib/openphar/parsers/base_monograph_parser.rb', line 47

def parse_file(file_path)
  raise NotImplementedError, 'Subclasses must implement #parse_file'
end

#reset_statsObject

Reset parsing statistics



62
63
64
65
66
67
68
69
# File 'lib/openphar/parsers/base_monograph_parser.rb', line 62

def reset_stats
  @stats = {
    total: 0,
    parsed: 0,
    failed: 0,
    errors: []
  }
end