Class: Apache::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/apache_config.rb

Constant Summary collapse

TabSize =
"    "
Comment =
/^\s*#\s?(.*?)\s*$/
Blank =
/^\s+$/
Directive =
/^\s*(\w+)(?:\s+(.*?)|)$/
SectionOpen =
/^\s*<\s*(\w+)(?:\s+([^>]+)|\s*)>$/
SectionClose =
/^\s*<\/\s*(\w+)\s*>$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Config

Returns a new instance of Config.



17
18
19
20
# File 'lib/apache_config.rb', line 17

def initialize(path)
  @path = path
  @config = parse_config(path)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



15
16
17
# File 'lib/apache_config.rb', line 15

def path
  @path
end

Instance Method Details

#save!Object



26
27
28
29
30
# 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 = "") ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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_hostsObject



22
23
24
# File 'lib/apache_config.rb', line 22

def virtual_hosts
  @config.children.map { |vh| VirtualHost.new(vh) }
end