module XML::Smart

Constants

COPY
LIBXML_VERSION
MOVE
MUTEX
VERSION

Public Class Methods

modify(name,default=nil,&block) click to toggle source
# File lib/xml/smart.rb, line 161
def self::modify(name,default=nil,&block)
  raise Error, 'first parameter has to be a filename or filehandle' unless name.is_a?(String) || name.is_a?(IO) || name.is_a?(Tempfile)
  raise Error, 'a block is mandatory' unless block_given?
  dom = io = nil
  begin
    if name.is_a?(String) && File.exist?(name)
      MUTEX.synchronize do
        io = ::URI::open(name,'r+')
        io.flock(File::LOCK_EX)
      end
      dom = Dom.new Nokogiri::XML::parse(io){|config| config.noblanks.noent.nsclean.strict }, name
      io.rewind
    elsif name.is_a?(String) && !File.exist?(name)
      MUTEX.synchronize do
        io = ::URI::open(name,'w')
        io.flock(File::LOCK_EX)
      end
      dom = Smart::string(default,name)
    elsif name.is_a?(IO) || name.is_a?(Tempfile)
      MUTEX.synchronize do
        io = name
        io.flock(File::LOCK_EX)
      end
      dom = Dom.new Nokogiri::XML::parse(io){|config| config.noblanks.noent.nsclean.strict }
      io.rewind
    end
    block.call(dom)
    dom.save_as(io)
  rescue => e
    puts e.message
    raise Error, "could not open #{name}"
  ensure
    if io
      io.flush
      io.truncate(io.pos)
      io.flock(File::LOCK_UN)
      io.close if name.is_a?(String)
    end
  end
  nil
end
new(name,default=nil) click to toggle source
# File lib/xml/smart.rb, line 159
def initialize(name,default=nil); open(name,default); end
open(name,default=nil) { |dom| ... } click to toggle source
# File lib/xml/smart.rb, line 203
def self::open(name,default=nil)
  raise Error, 'first parameter has to be a filename or filehandle' unless name.is_a?(String) || name.is_a?(IO) || name.is_a?(Tempfile)
  raise Error, 'second parameter has to be an xml string' unless default.is_a?(String) || default.nil?
  dom = Smart::open_unprotected(name,default,true)
  if dom && block_given?
    yield dom
  else
    dom
  end
end
open_unprotected(name,default=nil,lock=false) { |dom| ... } click to toggle source
# File lib/xml/smart.rb, line 214
def self::open_unprotected(name,default=nil,lock=false)
  raise Error, 'first parameter has to be a filename or filehandle' unless name.is_a?(String) || name.is_a?(IO) || name.is_a?(Tempfile)
  raise Error, 'second parameter has to be an xml string' unless default.is_a?(String) || default.nil?
  dom = begin
    filename = nil
    io = if name.is_a?(String)
      filename = name
      ::URI::open(name)
    else
      filename = name.path
      name
    end
    begin
      io.flock(File::LOCK_EX) if lock
      Dom.new Nokogiri::XML::parse(io){|config| config.noblanks.noent.nsclean.strict }, filename
    ensure
      io.flock(File::LOCK_UN)
    end
  rescue => e
    if default.nil?
      puts e.message
      raise Error, "could not open #{name}"
    else
      Smart::string(default)
    end
  end
  if block_given?
    yield dom
    nil
  else
    dom
  end
end
string(str,basepath=nil) { |dom| ... } click to toggle source
# File lib/xml/smart.rb, line 248
def self::string(str,basepath=nil)
  raise Error, 'first parameter has to be stringable (:to_s)' unless str.is_a?(String)
  dom = Dom.new Nokogiri::XML::parse(str.to_s){|config| config.noblanks.noent.nsclean.strict }, basepath
  if block_given?
    yield dom
    nil
  else
    dom
  end
end