apache-config-generator/lib/apache/config.rb

90 lines
2.1 KiB
Ruby
Raw Normal View History

2010-05-04 15:48:59 +00:00
Dir[File.join(File.dirname(__FILE__), '*.rb')].each { |f| require f }
2010-04-27 20:11:47 +00:00
module Apache
class Config
class << self
2010-05-04 15:48:59 +00:00
attr_accessor :line_indent, :config
2010-04-27 20:11:47 +00:00
include Apache::Master
include Apache::Quoteize
2010-04-27 21:00:43 +00:00
include Apache::Permissions
2010-04-27 20:11:47 +00:00
def build(target, &block)
2010-05-04 15:48:59 +00:00
reset!
2010-04-27 20:11:47 +00:00
self.instance_eval(&block)
puts @config * "\n"
2010-05-04 15:48:59 +00:00
end
2010-04-27 20:11:47 +00:00
2010-05-04 15:48:59 +00:00
# Reset the current settings
def reset!
@config = []
@line_indent = 0
2010-04-27 20:11:47 +00:00
end
2010-04-28 20:19:03 +00:00
2010-05-04 15:48:59 +00:00
# Indent the string by the current @line_indent level
2010-04-28 20:19:03 +00:00
def indent(string)
2010-05-04 15:48:59 +00:00
" " * (@line_indent * 2) + string
2010-04-28 20:19:03 +00:00
end
2010-05-04 15:48:59 +00:00
# Add the string to the current config
2010-04-28 20:19:03 +00:00
def <<(string)
@config << indent(string)
end
2010-05-04 15:48:59 +00:00
# Apachify a string
#
# Split the provided name on underscores and capitalize the individual parts
def apachify(name)
name.to_s.split("_").collect(&:capitalize).join
end
# Handle options that aren't specially handled
def method_missing(method, *args)
if method.to_s[-1..-1] == "!"
method = method.to_s[0..-2].to_sym
else
args = *quoteize(*args)
end
self << [ apachify(method), *args ] * ' '
end
# Handle creating block methods
def block_methods(*methods)
methods.each do |method|
self.class.class_eval <<-EOT
def #{method}(*name, &block)
blockify(apachify("#{method}"), name, &block)
end
EOT
end
end
# Handle the blockification of a provided block
def blockify(tag_name, name, &block)
start = [ tag_name ]
case name
when String
start << quoteize(name).first if name
when Array
start << (quoteize(*name) * " ") if name
end
start = start.join(' ')
self << "" if (@indent == 0)
self << "<#{start}>"
@line_indent += 1
self.instance_eval(&block)
@line_indent -= 1
self << "</#{tag_name}>"
end
2010-04-27 20:11:47 +00:00
end
block_methods :if_module, :directory, :virtual_host
end
end