Class: Paxmex::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/paxmex/parser.rb

Constant Summary collapse

SCHEMATA =
%w(epraw eptrn epa cbnot).reduce({}) do |h, fn|
  file = File.expand_path("../../config/#{fn}.yml", File.dirname(__FILE__))
  h.merge(fn => Paxmex::Schema.new(YAML.load(File.open(file))))
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, schema) ⇒ Parser

Returns a new instance of Parser.



13
14
15
16
17
18
19
20
21
22
# File 'lib/paxmex/parser.rb', line 13

def initialize(path, schema)
  @path = path
  @parent_chain = []

  if File.file?(schema)
    @schema = Paxmex::Schema.new(YAML.load_file(schema))
  else
    @schema = SCHEMATA.fetch(schema)
  end
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



11
12
13
# File 'lib/paxmex/parser.rb', line 11

def path
  @path
end

#schemaObject (readonly)

Returns the value of attribute schema.



11
12
13
# File 'lib/paxmex/parser.rb', line 11

def schema
  @schema
end

Instance Method Details

#parse(opts = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/paxmex/parser.rb', line 28

def parse(opts = {})
  return @parsed if @parsed

  content = raw.split("\n")

  # Parse the trailing section first so that we don't need
  # to consider it when parsing recurring sections
  trailer_section = schema.sections.detect(&:trailer?)
  trailer_content = [content.slice!(-1)]
  @parsed = parse_section(trailer_section, trailer_content, raw: opts[:raw_values])

  schema.sections.reject(&:trailer?).each_with_object(@parsed) do |section, parsed|
    break parsed if content.size == 0
    section_content = section.recurring? ? content : [content.slice!(0)]
    parsed.update(parse_section(section, section_content, raw: opts[:raw_values]))
  end
end

#rawObject



24
25
26
# File 'lib/paxmex/parser.rb', line 24

def raw
  @raw ||= File.read(@path).chomp
end