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

141 lines
2.4 KiB
Ruby
Raw Normal View History

2010-05-18 16:43:57 +00:00
module Apache
module Apachify
# Apachify a string
#
# Split the provided name on underscores and capitalize the individual parts
# Certain character strings are capitalized to match Apache directive names:
# * Cgi => CGI
# * Ssl => SSL
# * Ldap => LDAP
def apachify
self.to_s.split("_").collect { |part|
part.capitalize!
case part
when 'Ssl', 'Cgi', 'Ldap', 'Url'; part.upcase
when 'Etag'; 'ETag'
else; part
end
}.join
end
end
end
# Ruby strings
class String
include Apache::Apachify
alias :optionify :apachify
2010-05-18 17:08:17 +00:00
def commentize
self.split("\n")
end
def quoteize
%{"#{self}"}
end
2010-05-18 17:20:49 +00:00
alias :blockify :quoteize
2010-05-18 21:43:23 +00:00
def headerize
"#{self.quoteize}"
end
def replace_placeholderize(opts)
self.gsub(%r{%\{([^\}]+)\}}) do |match|
key = $1.downcase.to_sym
opts[key] || ''
end
end
2010-05-18 16:43:57 +00:00
end
# Ruby symbols
class Symbol
include Apache::Apachify
2010-05-18 17:08:17 +00:00
# Turn this into an option for IndexOptions
2010-05-18 16:43:57 +00:00
def optionify
output = self.apachify
2010-05-19 14:47:18 +00:00
output = "-#{output[3..-1]}" if self.to_s[0..3] == 'not_'
2010-05-18 16:43:57 +00:00
output
end
2010-05-18 17:08:17 +00:00
def quoteize
2010-05-18 17:20:49 +00:00
self.to_s.gsub('_', ' ')
end
def blockify
self.to_s
2010-05-18 17:08:17 +00:00
end
2010-05-18 21:43:23 +00:00
def headerize
"#{self.quoteize}"
end
2010-05-18 17:08:17 +00:00
end
2010-05-19 14:53:15 +00:00
class Fixnum
def quoteize; self; end
2010-05-18 16:43:57 +00:00
end
# Ruby arrays
class Array
# Apachify all the elements within this array
def apachify
self.collect(&:apachify)
end
2010-05-18 17:08:17 +00:00
def quoteize
self.collect(&:quoteize)
end
def quoteize!
self.collect!(&:quoteize)
end
2010-05-18 17:20:49 +00:00
def blockify
self.quoteize * " "
end
2010-05-18 17:08:17 +00:00
alias :commentize :to_a
2010-05-18 21:43:23 +00:00
def headerize
"#{self.first.quoteize} #{self.last}"
end
def rewrite_cond_optionify
self.collect do |opt|
{
:or => 'OR',
:case_insensitive => 'NC',
:no_vary => 'NV'
}[opt]
end
end
def rewrite_option_listify
(!self.empty?) ? "[#{self * ','}]" : nil
end
end
# Ruby hashes
class Hash
REWRITE_RULE_CONDITIONS = {
:last => 'L',
:forbidden => 'F',
:no_escape => 'NE',
:redirect => lambda { |val| val == true ? 'R' : "R=#{val}" },
:pass_through => 'PT',
:preserve_query_string => 'QSA',
:query_string_append => 'QSA',
:env => lambda { |val| "E=#{val}" }
}
def rewrite_rule_optionify
self.collect do |key, value|
what = REWRITE_RULE_CONDITIONS[key]
what = what.call(value) if what.kind_of? Proc
what
end.compact.sort
end
2010-05-18 16:43:57 +00:00
end