Class: AIPP::Parser Abstract

Inherits:
Object
  • Object
show all
Includes:
Debugger, Patcher
Defined in:
lib/aipp/parser.rb

Overview

This class is abstract.

Direct Known Subclasses

AIP::Parser, NOTAM::Parser, SHOOT::Parser

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Patcher

#attach_patches, #detach_patches, included

Methods included from Debugger

#info, #original_warn, #verbose_info, #warn, #with_debugger

Constructor Details

#initialize(downloader:, aixm:) ⇒ Parser

Returns a new instance of Parser.



29
30
31
32
# File 'lib/aipp/parser.rb', line 29

def initialize(downloader:, aixm:)
  @downloader, @aixm = downloader, aixm
  setup if respond_to? :setup
end

Instance Attribute Details

#aixmAIXM::Document (readonly)

Returns AIXM document instance.

Returns:

  • (AIXM::Document)

    AIXM document instance



9
10
11
# File 'lib/aipp/parser.rb', line 9

def aixm
  @aixm
end

Class Method Details

.dependenciesArray<String>

Declared dependencies

Returns:

  • (Array<String>)

    class names of other parsers this parser depends on



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

def dependencies
  @dependencies || []
end

.depends_on(*dependencies) ⇒ Object

Declare a dependency

Parameters:

  • dependencies (Array<String>)

    class names of other parsers this parser depends on



16
17
18
# File 'lib/aipp/parser.rb', line 16

def depends_on(*dependencies)
  @dependencies = dependencies.map(&:to_s)
end

Instance Method Details

#add(feature) ⇒ AIXM::Feature

Add feature to AIXM

Parameters:

  • feature (AIXM::Feature)

    e.g. airport or airspace

Returns:

  • (AIXM::Feature)

    added feature



72
73
74
75
76
# File 'lib/aipp/parser.rb', line 72

def add(feature)
  verbose_info "adding #{feature.inspect}"
  aixm.add_feature feature
  feature
end

#find(object) ⇒ Object

Note:

This method is delegated to AIXM::Association::Array.

Find equal objects previously written to AIXM.



90
91
92
93
94
# File 'lib/aipp/parser.rb', line 90

%i(find_by find).each do |method|
  define_method method do |*args|
    aixm.features.send(method, *args)
  end
end

#find_by(klass, attributes = {}) ⇒ Object

Note:

This method is delegated to AIXM::Association::Array.

Find objects of the given class and optionally with the given attribute values previously written to AIXM.



90
91
92
93
94
# File 'lib/aipp/parser.rb', line 90

%i(find_by find).each do |method|
  define_method method do |*args|
    aixm.features.send(method, *args)
  end
end

#given(*objects) ⇒ Object #given(*objects) {|Array<Object>| ... } ⇒ Object

Overloads:

  • #given(*objects) ⇒ Object

    Return objects unless at least one of them equals nil

    Examples:

    # Instead of this:
    first, last = unless ((first = expensive_first).nil? || (last = expensive_last).nil?)
      [first, last]
    end
    
    # Use the following:
    first, last = given(expensive_first, expensive_last)

    Parameters:

    • *objects (Array<Object>)

      any objects really

    Returns:

    • (Object)

      nil if at least one of the objects is nil, given objects otherwise

  • #given(*objects) {|Array<Object>| ... } ⇒ Object

    Yield objects unless at least one of them equals nil

    Examples:

    # Instead of this:
    name = unless ((first = expensive_first.nil? || (last = expensive_last.nil?)
      "#{first} #{last}"
    end
    
    # Use any of the following:
    name = given(expensive_first, expensive_last) { |f, l| "#{f} #{l}" }
    name = given(expensive_first, expensive_last) { "#{_1} #{_2}" }

    Parameters:

    • *objects (Array<Object>)

      any objects really

    Yields:

    • (Array<Object>)

      objects passed as parameter

    Returns:

    • (Object)

      nil if at least one of the objects is nil, return of block otherwise



129
130
131
132
133
# File 'lib/aipp/parser.rb', line 129

def given(*objects)
  if objects.none?(&:nil?)
    block_given? ? yield(*objects) : objects
  end
end

#inspectString

Returns:



35
36
37
# File 'lib/aipp/parser.rb', line 35

def inspect
  "#<AIPP::Parser #{section}>"
end

Build and optionally check a Markdown link

Examples:

AIPP.options.check_links = false
link_to('foo', 'https://bar.com/exists')      # => "[foo](https://bar.com/exists)"
link_to('foo', 'https://bar.com/not-found')   # => "[foo](https://bar.com/not-found)"
AIPP.options.check_links = true
link_to('foo', 'https://bar.com/exists')      # => "[foo](https://bar.com/exists)"
link_to('foo', 'https://bar.com/not-found')   # => nil

Parameters:

  • body (String)

    body text of the link

  • url (String)

    URL of the link

Returns:

  • (String, nil)

    Markdown link



148
149
150
# File 'lib/aipp/parser.rb', line 148

def link_to(body, url)
  "[#{body}](#{url})" if !AIPP.options.check_links || url_exists?(url)
end

#origin_forObject

This method is abstract.


45
46
47
# File 'lib/aipp/parser.rb', line 45

def origin_for(*)
  fail "origin_for method must be implemented in parser"
end

#read(document = section) ⇒ Nokogiri::XML::Document, ...

Read a source document

Read the cached document if it exists in the source archive. Otherwise, download and cache it.

An origin builder method origin_for must be implemented by the parser definition.

Parameters:

  • document (String) (defaults to: section)

    e.g. “ENR-2.1” or “aerodromes” (default: current section)

Returns:

  • (Nokogiri::XML::Document, Nokogiri::HTML5::Document, Roo::Spreadsheet, String)

    document



61
62
63
64
65
66
# File 'lib/aipp/parser.rb', line 61

def read(document=section)
  @downloader.read(
    document: document,
    origin: origin_for(document)
  )
end

#sectionString

Returns:



40
41
42
# File 'lib/aipp/parser.rb', line 40

def section
  self.class.to_s.sectionize
end