Class: Xcop::Document

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

Overview

One document.

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Document

Ctor.

path

Path of it



71
72
73
# File 'lib/xcop.rb', line 71

def initialize(path)
  @path = path
end

Instance Method Details

#diff(nocolor = false) ⇒ Object

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



76
77
78
79
80
81
# File 'lib/xcop.rb', line 76

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)
end

#fix(license = '') ⇒ Object

Fixes the document.



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/xcop.rb', line 93

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.



84
85
86
87
88
89
90
# File 'lib/xcop.rb', line 84

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