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 OFMX files.
      Usage: #{File.basename($0)} files
    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!
  @files = ARGV
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
42
# File 'lib/aixm/executables.rb', line 22

def run
  @files.each do |file|
    fail "cannot read #{file}" unless file && File.readable?(file)
    fail "#{file} is not OFMX" unless file.match?(/\.ofmx$/)
    AIXM.ofmx!
    document = File.open(file) { 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]
      fail (["#{file} is not valid..."] + errors).join("\n")
    when @options[:in_place]
      File.write(file, document.to_xml)
    else
      puts document.to_xml
    end
  rescue => error
    puts "ERROR: #{error.message}"
    exit 1
  end
end