class Apache::Config

Constants

Blank
Comment
Directive
SectionClose
SectionOpen
TabSize

Attributes

path[R]

Public Class Methods

new(path) click to toggle source
# File lib/apache_config.rb, line 17
def initialize(path)
  @path = path
  @config = parse_config(path)
end

Public Instance Methods

save!() click to toggle source
# File lib/apache_config.rb, line 26
def save!
  File.open(@path, 'w') do |f|
    f.write to_config
  end
end
to_config(root = nil, indent = "") click to toggle source
# File lib/apache_config.rb, line 32
def to_config(root = nil, indent = "")
  element = root || @config
  content = ""

  case
    when element.isRoot?
      element.children.map { |c| to_config(c) }.join
    when element.hasChildren?
      "#{indent}<#{element.name} #{element.content}>\n" +
      element.children.map {|c| to_config(c, indent + TabSize) }.join +
      "#{indent}</#{element.name}>\n"
    when element.name == :comment
      "#{indent}# #{element.content}\n"
    when element.name == :blank
      "#{indent}\n"
    else
      "#{indent}#{element.name} #{element.content}\n"
    end
end
virtual_hosts() click to toggle source
# File lib/apache_config.rb, line 22
def virtual_hosts
  @config.children.map { |vh| VirtualHost.new(vh) }
end

Private Instance Methods

parse_config(path) click to toggle source
# File lib/apache_config.rb, line 53
def parse_config(path)
  current_child = Node.new("ROOT")

  File.open(path).read.split(/\n/).each do |line|
    case line
    when Comment
      current_child << Node.new(:comment, $1)
    when Blank
      current_child << Node.new(:blank)
    when Directive
      current_child << Node.new($1, $2)
    when SectionOpen
      current_child = current_child << Node.new($1, $2)
    when SectionClose
      current_child = current_child.parent
    end
  end

  current_child
end