Class: GovCodes::Dafecd::RiSdi::SectionSlicer

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

Overview

Slices the full directory text into the RI/SDI sections named in the injected Config.

Each section is located by its header line (e.g. "AIR FORCE REPORTING IDENTIFIERS (RI)"). The header regexes require the "(SDI)"/"(RI)"/"(SFSC)" parenthetical, which the table-of-contents entries lack, so the TOC is never mistaken for a section. A section runs from its header to the next located header. The out-of-scope Space Force Specialty Codes (SFSC) section is used only as a boundary (it terminates the AF RI section) and is dropped from the result.

Defined Under Namespace

Classes: Section

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of SectionSlicer.



21
22
23
24
# File 'lib/gov_codes/dafecd/ri_sdi/section_slicer.rb', line 21

def initialize(text, config: Config.dafecd)
  @text = text
  @config = config
end

Instance Method Details

#sectionsArray<Section>

Returns located, in-scope sections in document order.

Returns:

  • (Array<Section>)

    located, in-scope sections in document order



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gov_codes/dafecd/ri_sdi/section_slicer.rb', line 27

def sections
  located = @config.sections
    .map { |spec| {spec: spec, at: (@text =~ spec[:header])} }
    .reject { |m| m[:at].nil? }
    .sort_by { |m| m[:at] }

  located.each_with_index.map do |mark, i|
    finish = (i + 1 < located.size) ? located[i + 1][:at] : @text.length
    Section.new(
      kind: mark[:spec][:kind],
      force: mark[:spec][:force],
      text: @text[mark[:at]...finish]
    )
  end.reject { |section| section.kind == :skip }
end