class AIXM::Executables::Mkmid

Public Class Methods

new(**options) click to toggle source
   # File lib/aixm/executables.rb
 7       def initialize(**options)
 8         @options = options
 9         OptionParser.new do |o|
10           o.banner = <<~END
11             Add mid attributes to a schema valid OFMX file.
12             Usage: #{File.basename($0)} infile.ofmx
13           END
14           o.on('-i', '--[no-]in-place', 'overwrite file instead of dumping to STDOUT (default: false)') { @options[:in_place] = _1 }
15           o.on('-f', '--[no-]force', 'ignore XML schema validation errors (default: false)') { @options[:force] = _1 }
16           o.on('-A', '--about', 'show author/license information and exit') { AIXM::Executables.about }
17           o.on('-V', '--version', 'show version and exit') { AIXM::Executables.version }
18         end.parse!
19         @infile = ARGV.shift
20       end

Public Instance Methods

run() click to toggle source
   # File lib/aixm/executables.rb
22 def run
23   fail 'cannot read file' unless @infile && File.readable?(@infile)
24   fail 'file ist not OFMX' unless @infile.match?(/\.ofmx$/)
25   AIXM.ofmx!
26   document = File.open(@infile) { Nokogiri::XML(_1) }
27   AIXM::PayloadHash::Mid.new(document).insert_mid
28   errors = Nokogiri::XML::Schema(File.open(AIXM.schema(:xsd))).validate(document)
29   case
30   when errors.any? && !@options[:force]
31     puts errors
32     fail "OFMX file is not schema valid"
33   when @options[:in_place]
34     File.write(@infile, document.to_xml)
35   else
36     puts document.to_xml
37   end
38 rescue => error
39   puts "ERROR: #{error.message}"
40   exit 1
41 end