lib/aipp/parser.rb in aipp-0.2.3 vs lib/aipp/parser.rb in aipp-0.2.4
- old
+ new
@@ -10,10 +10,13 @@
attr_reader :config
# @return [AIXM::Document] target document
attr_reader :aixm
+ # @return [Hash] map from AIP name to fixtures
+ attr_reader :fixtures
+
# @return [Hash] map from border names to border objects
attr_reader :borders
# @return [OpenStruct] object cache
attr_reader :cache
@@ -23,12 +26,14 @@
@options[:storage] = options[:storage].join(options[:region])
@options[:storage].mkpath unless @options[:storage].exist?
@config = {}
@aixm = AIXM.document(region: @options[:region], effective_at: @options[:airac].date)
@dependencies = THash.new
+ @fixtures = {}
@borders = {}
@cache = OpenStruct.new
+ AIXM.send("#{options[:schema]}!")
end
# Read the configuration from config.yml.
def read_config
info("Reading config.yml")
@@ -40,17 +45,27 @@
# Read the region directory and build the dependency list.
def read_region
info("Reading region #{options[:region]}")
dir = Pathname(__FILE__).dirname.join('regions', options[:region])
fail("unknown region `#{options[:region]}'") unless dir.exist?
+ # Fixtures
+ dir.glob('fixtures/*.yml').each do |file|
+ verbose_info "Reading fixture fixtures/#{file.basename}"
+ fixture = YAML.load_file(file)
+ @fixtures[file.basename('.yml').to_s] = fixture
+ end
# Borders
dir.glob('borders/*.geojson').each do |file|
+ verbose_info "Reading border borders/#{file.basename}"
border = AIPP::Border.new(file)
@borders[border.name] = border
end
# Helpers
- dir.glob('helpers/*.rb').each { |f| require f }
+ dir.glob('helpers/*.rb').each do |file|
+ verbose_info "Reading helper helpers/#{file.basename}"
+ require file
+ end
# Parsers
dir.glob('*.rb').each do |file|
verbose_info "Requiring #{file.basename}"
require file
aip = file.basename('.*').to_s
@@ -65,10 +80,11 @@
@dependencies.tsort(options[:aip]).each do |aip|
info("Parsing #{aip}")
("AIPP::%s::%s" % [options[:region], aip.remove(/\W/).classify]).constantize.new(
aip: aip,
downloader: downloader,
+ fixture: @fixtures[aip],
parser: self
).attach_patches.tap(&:parse).detach_patches
end
end
end
@@ -77,19 +93,18 @@
#
# @raise [RuntimeError] if the document is not valid
def validate_aixm
info("Validating #{options[:schema].upcase}")
unless aixm.valid?
- message = "invalid AIXM document:\n" + aixm.errors.map(&:message).join("\n")
+ message = "invalid #{options[:schema].upcase} document:\n" + aixm.errors.map(&:message).join("\n")
@options[:force] ? warn(message, pry: binding) : fail(message)
end
end
# Write the AIXM document.
def write_aixm
file = "#{options[:region]}_#{options[:airac].date.xmlschema}.#{options[:schema]}"
info("Writing #{file}")
- AIXM.send("#{options[:schema]}!")
File.write(file, aixm.to_xml)
end
# Write the configuration to config.yml.
def write_config