class Stanza::StanzaFile
Attributes
commentChar[RW]
stanzas[RW]
Public Class Methods
new(file)
click to toggle source
# File lib/Stanza/StanzaFile.rb, line 8 def initialize(file) @fileName = file @commentChar = '*' @comment = '' @stanzas = [] if ::File.exist?(file) readStanzaFile(file) else ::File.new(file, File::CREAT|File::WRONLY, 0644) end end
Public Instance Methods
addComment(comment)
click to toggle source
# File lib/Stanza/StanzaFile.rb, line 43 def addComment(comment) @comment += comment + "\n" end
addStanza(stanza)
click to toggle source
# File lib/Stanza/StanzaFile.rb, line 47 def addStanza(stanza) if stanza.kind_of?(Stanza) @stanzas << stanza end end
deleteStanza(name)
click to toggle source
# File lib/Stanza/StanzaFile.rb, line 61 def deleteStanza(name) @stanzas.each do |st| @stanzas.delete(st) if st.name == name end end
deleteStanzaAttr(stanzaName, attr)
click to toggle source
# File lib/Stanza/StanzaFile.rb, line 90 def deleteStanzaAttr(stanzaName, attr) @stanzas.each do |st| if st.name == stanzaName st.delAttribute(attr) end end end
empty!()
click to toggle source
# File lib/Stanza/StanzaFile.rb, line 29 def empty! @comment = '' @stanzas = [] end
Also aliased as: clear!
getStanza(name)
click to toggle source
# File lib/Stanza/StanzaFile.rb, line 67 def getStanza(name) @stanzas.each do |st| return st if st.name == name end return nil end
getStanzaAttr(stanzaName, attr)
click to toggle source
# File lib/Stanza/StanzaFile.rb, line 82 def getStanzaAttr(stanzaName, attr) @stanzas.each do |st| if st.name == stanzaName return st.getAttribute(attr) end end end
read!()
click to toggle source
# File lib/Stanza/StanzaFile.rb, line 20 def read! empty! readStanzaFile(@fileName) end
readFromFile(file)
click to toggle source
# File lib/Stanza/StanzaFile.rb, line 34 def readFromFile(file) empty! readStanzaFile(file) end
setStanza(name, stanza)
click to toggle source
# File lib/Stanza/StanzaFile.rb, line 53 def setStanza(name, stanza) if stanza.kind_of?(Stanza) @stanzas.each do |st| st.copy(stanza) if st.name == name end end end
setStanzaAttr(stanzaName, attr, value)
click to toggle source
# File lib/Stanza/StanzaFile.rb, line 74 def setStanzaAttr(stanzaName, attr, value) @stanzas.each do |st| if st.name == stanzaName st.setAttribute(attr, value) end end end
to_s()
click to toggle source
# File lib/Stanza/StanzaFile.rb, line 98 def to_s @stanzas.each do |st| st.commentChar = @commentChar end s = ERB.new("<% @comment.lines do |line| %> <%= @commentChar %> <%= line %> <% end %> <% @stanzas.each do |st| %> <%= st %> <% end %> ", 0, '<>').result(binding) return s end
Also aliased as: inspect
write!()
click to toggle source
# File lib/Stanza/StanzaFile.rb, line 25 def write! writeStanzaFile(@fileName) end
writeToFile(filename)
click to toggle source
# File lib/Stanza/StanzaFile.rb, line 39 def writeToFile(filename) writeStanzaFile(filename) end
Private Instance Methods
readStanza(name, lines)
click to toggle source
# File lib/Stanza/StanzaFile.rb, line 157 def readStanza(name, lines) return nil if name == '' || name == '-' h = Hash.new lines.each do |line| line.strip! sAttr, sValue = line.split('=') next if sAttr.nil? next if sValue.nil? sAttr.strip! sValue.strip! h[sAttr] = sValue end s = Stanza.new(name, h) return s end
readStanzaFile(file)
click to toggle source
# File lib/Stanza/StanzaFile.rb, line 121 def readStanzaFile(file) open(file) do |f| stanzaName = '' stanzaLines = [] nextcomment = '' f.each do |line| next if line.nil? line.rstrip! if line.start_with?("#{@commentChar}") if stanzaName == '-' nextcomment += line.sub(/^./, "").strip + "\n" else @comment += line.sub(/^./, "").strip + "\n" end next end if line.end_with?(':') stanzaName = line.gsub(/:$/, "") next end if line.empty? unless stanzaName == '' || stanzaName == '-' s = readStanza(stanzaName, stanzaLines) s.comment = nextcomment unless s.nil? @stanzas << s unless s.nil? stanzaName = '-' stanzaLines = [] nextcomment = '' end next end stanzaLines << line unless stanzaName == '' || stanzaName == '-' end end end
writeStanzaFile(file)
click to toggle source
# File lib/Stanza/StanzaFile.rb, line 115 def writeStanzaFile(file) open(file, 'w') do |f| f.write(to_s) end end