Class: GovCodes::Dafecd::RiSdi::SdiSectionSplitter

Inherits:
Object
  • Object
show all
Defined in:
lib/gov_codes/dafecd/ri_sdi/sdi_section_splitter.rb

Overview

Splits a Special Duty Identifiers (SDI) section into one string per record.

A record begins at either:

* a run of "SDI <code>" card anchors (the first anchor of the run), or
* a "CEM Code <code>" line — the single ladder record (Premier Honor
Guard, 8G000) embedded in the enlisted AF SDI section. It is split out
as its own record so its body (notably its shredout table) can never
bleed into the preceding card; the AFSC pipeline parses it downstream.

Running page headers are stripped first. Consecutive card anchors stay in one record (the multi-code Air Advisor blocks). A wrapped-prose false positive ("SDI 8P000, completion of a current T5 ...") does NOT start a record — its inline "title" fails the real-title test, so it is left as body of the surrounding record and later rejected by SdiCardParser.

Instance Method Summary collapse

Constructor Details

#initialize(text, config: Config.dafecd) ⇒ SdiSectionSplitter

Returns a new instance of SdiSectionSplitter.



24
25
26
27
# File 'lib/gov_codes/dafecd/ri_sdi/sdi_section_splitter.rb', line 24

def initialize(text, config: Config.dafecd)
  @config = config
  @lines = text.lines.reject { |line| line =~ config.header }
end

Instance Method Details

#recordsArray<String>

Returns one string per record.

Returns:

  • (Array<String>)

    one string per record



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gov_codes/dafecd/ri_sdi/sdi_section_splitter.rb', line 30

def records
  records = []
  current = nil
  prev_meaningful_was_anchor = false

  @lines.each do |line|
    is_card = card_anchor?(line)
    is_cem = line.match?(@config.cem)
    stripped = line.strip
    neutral = stripped.empty? || stripped.match?(/\A\d+\z/)

    starts_record = is_cem || (is_card && !prev_meaningful_was_anchor)

    if starts_record
      current = +""
      records << current
    end

    current << line if current

    unless neutral
      # A CEM line's ladder ("AFSC 8G091 ...") keeps the run open so the
      # card detector does not re-trigger inside the ladder record.
      prev_meaningful_was_anchor = is_card || is_cem || ladder_line?(line)
    end
  end

  records
end