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



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

def initialize(path)
  @path = path
end

Instance Method Details

#diffObject

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



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

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

#fix(license = '') ⇒ Object

Fixes the document.



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

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.



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

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