Class: Xcop::Document

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

Overview

One document.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2017-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Document

Ctor.

path

Path of it



33
34
35
# File 'lib/xcop/document.rb', line 33

def initialize(path)
  @path = path
end

Instance Method Details

#diff(nocolor: false) ⇒ Object

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



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

def diff(nocolor: false)
  xml = Nokogiri::XML(File.open(@path), &:noblanks)
  ideal = xml.to_xml(indent: 2)
  now = File.read(@path)
  differ(ideal, now, nocolor: nocolor)
end

#fix(license = '') ⇒ Object

Fixes the document.



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/xcop/document.rb', line 55

def fix(license = '')
  xml = Nokogiri::XML(File.open(@path), &:noblanks)
  unless license.empty?
    xml.xpath('/comment()').remove
    xml.children.before(
      Nokogiri::XML::Comment.new(xml, "\n#{license.strip}\n")
    )
  end
  ideal = xml.to_xml(indent: 2)
  File.write(@path, ideal)
end

#ldiff(license) ⇒ Object

Return the difference for the license.



46
47
48
49
50
51
52
# File 'lib/xcop/document.rb', line 46

def ldiff(license)
  xml = Nokogiri::XML(File.open(@path), &:noblanks)
  comment = xml.xpath('/comment()')[0]
  now = comment.nil? ? '' : comment.text.to_s.strip
  ideal = license.strip
  differ(ideal, now)
end