class SpigitConf::Engage
Attributes
doc[R]
file[R]
spigit_config[R]
Public Class Methods
new(opts = {})
click to toggle source
# File lib/spigit_conf/engage.rb, line 78 def initialize(opts = {}) @file = opts.fetch(:file) @doc = Nokogiri::XML(File.read(@file)) { |x| x.noblanks } @spigit_config = @doc.at_css('spigit-config') end
Public Instance Methods
save!(opts = {})
click to toggle source
# File lib/spigit_conf/engage.rb, line 84 def save!(opts = {}) backup = opts.fetch(:backup, true) take_backup if backup File.open(file, 'w') do |f| f.write(doc.to_xml(save_with: SpigitConf::XML_SAVE_OPTIONS, :indent => 4, :encoding => 'UTF-8')) end end
Private Instance Methods
add_config(key)
click to toggle source
# File lib/spigit_conf/engage.rb, line 108 def add_config(key) if node_exists?(key) return get_node(key) end # Create the new node node = Nokogiri::XML::Node.new(key, doc) # Add the new node to the spigit-config namespace spigit_config.add_child(node) node end
get_node(key)
click to toggle source
# File lib/spigit_conf/engage.rb, line 122 def get_node(key) spigit_config.at_xpath("./#{key}") end
get_nodes(key)
click to toggle source
# File lib/spigit_conf/engage.rb, line 126 def get_nodes(key) spigit_config.xpath("./#{key}") end
node_exists?(key)
click to toggle source
# File lib/spigit_conf/engage.rb, line 130 def node_exists?(key) elements = get_nodes(key) if elements.length == 0 return false elsif elements.length == 1 return true elsif element.length > 1 remove_nodes(elements[1..-1]) return true else return false end end
remove_nodes(*nodes)
click to toggle source
# File lib/spigit_conf/engage.rb, line 145 def remove_nodes(*nodes) nodes.each do |node| node.remove end end
take_backup()
click to toggle source
# File lib/spigit_conf/engage.rb, line 95 def take_backup timestamp = Time.now.strftime('%Y%m%d-%H%M%Z') backup_filename = [ file, timestamp].join('_') ext = 0 while File.exists?(backup_filename) backup_filename = [ file, timestamp, ext].join('_') ext += 1 end FileUtils.cp(file, backup_filename) end