class Xcop::Document

One document.

Public Class Methods

new(path) click to toggle source

Ctor.

path

Path of it

# File lib/xcop.rb, line 71
def initialize(path)
  @path = path
end

Public Instance Methods

diff(nocolor = false) click to toggle source

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

# 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 = '') click to toggle source

Fixes the document.

# 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) click to toggle source

Return the difference for the license.

# 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

Private Instance Methods

differ(ideal, fact, nocolor = false) click to toggle source
# File lib/xcop.rb, line 107
def differ(ideal, fact, nocolor = false)
  return '' if ideal == fact
  if nocolor
    Differ.diff_by_line(ideal, fact).to_s
  else
    Differ.format = :color
    Differ.diff_by_line(schars(ideal), schars(fact)).to_s
  end
end
schars(text) click to toggle source
# File lib/xcop.rb, line 117
def schars(text)
  text.gsub(/\n/, "\\n\n")
end