Class: AIXM::Executables::Mkmid
Instance Method Summary collapse
-
#initialize(**options) ⇒ Mkmid
constructor
A new instance of Mkmid.
- #run ⇒ Object
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 = OptionParser.new do |o| o. = <<~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
#run ⇒ Object
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.}" exit 1 end end |