Class: Xcop::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/xcop/document.rb

Overview

One document.

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright (c) 2017-2026 Yegor Bugayenko

License

MIT

Constant Summary collapse

XSI_NS =
'http://www.w3.org/2001/XMLSchema-instance'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Document

Ctor.

path

Path of it



17
18
19
# File 'lib/xcop/document.rb', line 17

def initialize(path)
  @path = path
end

Instance Method Details

#diff(nocolor: false) ⇒ Object

Return the difference, if any (empty string if everything is clean).



22
23
24
# File 'lib/xcop/document.rb', line 22

def diff(nocolor: false)
  differ(ideal, File.read(@path), nocolor: nocolor)
end

#fixObject

Fixes the document, returning :fixed when the file content changed on disk and :untouched when the file was already canonical.



29
30
31
32
33
34
# File 'lib/xcop/document.rb', line 29

def fix
  canonical = ideal
  return :untouched if canonical == File.read(@path)
  File.write(@path, canonical)
  :fixed
end

#validateObject

Validates the document against its declared XSD schema, if any. Returns an array of error message strings (empty when valid or no schema declared); a declared but missing schema is itself an error.



39
40
41
42
43
44
45
# File 'lib/xcop/document.rb', line 39

def validate
  xml = Nokogiri::XML(File.read(@path))
  xsd = schema(xml)
  return [] unless xsd
  return ["schema file is absent at #{xsd}"] unless File.exist?(xsd)
  Nokogiri::XML::Schema(File.read(xsd)).validate(xml).map(&:message)
end