Class: AIXM::Executables::Mkmid

Inherits:
Object
  • Object
show all
Defined in:
lib/aixm/executables.rb

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Mkmid

Returns a new instance of Mkmid.


7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/aixm/executables.rb', line 7

def initialize(**options)
  @options = options
  OptionParser.new do |o|
    o.banner = <<~END
      Add mid attributes to a schema valid OFMX file.
      Usage: #{File.basename($0)} infile.ofmx
    END
    o.on('-i', '--[no-]in-place', 'overwrite file instead of dumping to STDOUT (default: false)') { @options[:in_place] = _1 }
    o.on('-f', '--[no-]force', 'ignore XML schema validation errors (default: false)') { @options[:force] = _1 }
    o.on('-A', '--about', 'show author/license information and exit') { AIXM::Executables.about }
    o.on('-V', '--version', 'show version and exit') { AIXM::Executables.version }
  end.parse!
  @infile = ARGV.shift
end

Instance Method Details

#runObject


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/aixm/executables.rb', line 22

def run
  fail 'cannot read file' unless @infile && File.readable?(@infile)
  fail 'file ist not OFMX' unless @infile.match?(/\.ofmx$/)
  AIXM.ofmx!
  document = File.open(@infile) { Nokogiri::XML(_1) }
  AIXM::PayloadHash::Mid.new(document).insert_mid
  errors = Nokogiri::XML::Schema(File.open(AIXM.schema(:xsd))).validate(document)
  case
  when errors.any? && !@options[:force]
    puts errors
    fail "OFMX file is not schema valid"
  when @options[:in_place]
    File.write(@infile, document.to_xml)
  else
    puts document.to_xml
  end
rescue => error
  puts "ERROR: #{error.message}"
  exit 1
end