Class: Xcop::Document
- Inherits:
-
Object
- Object
- Xcop::Document
- 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
-
#diff(nocolor: false) ⇒ Object
Return the difference, if any (empty string if everything is clean).
-
#fix ⇒ Object
Fixes the document, returning
:fixedwhen the file content changed on disk and:untouchedwhen the file was already canonical. -
#initialize(path) ⇒ Document
constructor
Ctor.
-
#validate ⇒ Object
Validates the document against its declared XSD schema, if any.
Constructor Details
#initialize(path) ⇒ Document
Ctor.
pathPath 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 |
#fix ⇒ Object
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 |
#validate ⇒ Object
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 |