rework more stuff to make reek happy

This commit is contained in:
John Bintz 2010-05-18 13:08:17 -04:00
parent af01487160
commit f383d35a80
10 changed files with 71 additions and 73 deletions

View File

@ -26,17 +26,34 @@ class String
include Apache::Apachify include Apache::Apachify
alias :optionify :apachify alias :optionify :apachify
def commentize
self.split("\n")
end
def quoteize
%{"#{self}"}
end
end end
# Ruby symbols # Ruby symbols
class Symbol class Symbol
include Apache::Apachify include Apache::Apachify
# Turn this into an option for IndexOptions
def optionify def optionify
output = self.apachify output = self.apachify
output = "-#{output[4..-1].apachify}" if self.to_s[0..3] == 'not_' output = "-#{output[4..-1].apachify}" if self.to_s[0..3] == 'not_'
output output
end end
def quoteize
to_s.gsub('_', ' ')
end
end
class Fixnum
def quoteize; to_s; end
end end
# Ruby arrays # Ruby arrays
@ -45,4 +62,14 @@ class Array
def apachify def apachify
self.collect(&:apachify) self.collect(&:apachify)
end end
def quoteize
self.collect(&:quoteize)
end
def quoteize!
self.collect!(&:quoteize)
end
alias :commentize :to_a
end end

View File

@ -57,7 +57,6 @@ module Apache
attr_accessor :line_indent, :rotate_logs_path attr_accessor :line_indent, :rotate_logs_path
include Apache::Master include Apache::Master
include Apache::Quoteize
include Apache::Permissions include Apache::Permissions
include Apache::Directories include Apache::Directories
include Apache::Logging include Apache::Logging
@ -132,7 +131,7 @@ module Apache
if method_name[-1..-1] == "!" if method_name[-1..-1] == "!"
method = method_name[0..-2].to_sym method = method_name[0..-2].to_sym
else else
args = *quoteize(*args) args.quoteize!
end end
self << [ method.apachify, *args ].compact * ' ' self << [ method.apachify, *args ].compact * ' '
@ -195,9 +194,9 @@ module Apache
def blockify_name(name) def blockify_name(name)
case name case name
when String when String
quoteize(name).first name.quoteize.first
when Array when Array
(quoteize(*name) * " ") name.quoteize * " "
when Symbol when Symbol
name.to_s name.to_s
end end

View File

@ -6,7 +6,7 @@ module Apache
# The options passed into this method are Apachified: # The options passed into this method are Apachified:
# options :exec_cgi, :follow_sym_links #=> Options ExecCGI FollowSymLinks # options :exec_cgi, :follow_sym_links #=> Options ExecCGI FollowSymLinks
def options(*opt) def options(*opt)
create_options_list('Options', *opt) create_options_list('options'.apachify, *opt)
end end
# Create an IndexOptions directive # Create an IndexOptions directive
@ -14,7 +14,7 @@ module Apache
# The options passed into this method are Apachified: # The options passed into this method are Apachified:
# index_options :fancy_indexing, :suppress_description #=> IndexOptions FancyIndexing SuppressDescription # index_options :fancy_indexing, :suppress_description #=> IndexOptions FancyIndexing SuppressDescription
def index_options(*opt) def index_options(*opt)
create_options_list('IndexOptions', *opt) create_options_list('index_options'.apachify, *opt)
end end
private private

View File

@ -19,11 +19,11 @@ module Apache
[ :custom, :error, :script, :rewrite ].each do |type| [ :custom, :error, :script, :rewrite ].each do |type|
class_eval <<-EOT class_eval <<-EOT
def #{type}_log(*opts) def #{type}_log(*opts)
handle_log '#{type.to_s.capitalize}Log', opts.first, quoteize(opts.first), opts[1..-1] handle_log '#{type.to_s.capitalize}Log', opts.first, opts.first.quoteize, opts[1..-1]
end end
def rotate_#{type}_log(*opts) def rotate_#{type}_log(*opts)
handle_log '#{type.to_s.capitalize}Log', opts.first, quoteize(rotatelogs(*opts[0..1])), opts[2..-1] handle_log '#{type.to_s.capitalize}Log', opts.first, rotatelogs(*opts[0..1]).quoteize, opts[2..-1]
end end
EOT EOT
end end

View File

@ -15,7 +15,7 @@ module Apache
# Listen "1.2.3.4:80" # Listen "1.2.3.4:80"
# Listen "2.3.4.5:80" # Listen "2.3.4.5:80"
def listen(*opt) def listen(*opt)
opt.each { |o| self << "Listen #{quoteize(o)}" } opt.each { |adapter| self << "Listen #{adapter.quoteize}" }
end end
alias :listen! :listen alias :listen! :listen
@ -48,27 +48,19 @@ module Apache
end end
# Set the TCP timeout. Defined here to get around various other timeout methods. # Set the TCP timeout. Defined here to get around various other timeout methods.
def timeout(t) def timeout(time)
self << "Timeout #{t}" self << "Timeout #{time}"
end end
# Add a comment to the Apache config. Can pass in either a String or Array of comment lines. # Add a comment to the Apache config. Can pass in either a String or Array of comment lines.
def comment(c) def comment(what)
out = [ '' ] self + [ '', what.commentize, '' ].flatten.collect { |line| "# #{line.strip}".strip }
case c
when String
out += c.split("\n")
when Array
out += c
end
out << ''
self + out.collect { |line| "# #{line.strip}".strip }
end end
# Create a ScriptAlias, checking to make sure the filesystem path exists. # Create a ScriptAlias, checking to make sure the filesystem path exists.
def script_alias(uri, path) def script_alias(uri, path)
directory? path directory? path
self << %{ScriptAlias #{quoteize(uri, path) * ' '}} self << %{ScriptAlias #{[ uri, path ].quoteize * ' '}}
end end
alias :script_alias! :script_alias alias :script_alias! :script_alias
@ -92,7 +84,7 @@ module Apache
# Alias a URL to a directory in the filesystem. # Alias a URL to a directory in the filesystem.
# Used to get around reserved Ruby keyword. # Used to get around reserved Ruby keyword.
def apache_alias(*opts) def apache_alias(*opts)
self << "Alias #{quoteize(*opts) * " "}" self << "Alias #{opts.quoteize * " "}"
end end
# Set multiple headers to be delivered for a particular section # Set multiple headers to be delivered for a particular section
@ -102,12 +94,12 @@ module Apache
# Header set "Content-dispoaition" "attachment" env=only-for-downloads # Header set "Content-dispoaition" "attachment" env=only-for-downloads
def set_header(hash) def set_header(hash)
hash.each do |key, value| hash.each do |key, value|
output = "Header set #{quoteize(key)}" output = "Header set #{key.quoteize}"
case value case value
when String, Symbol when String, Symbol
output += " #{quoteize(value)}" output += " #{value.quoteize}"
when Array when Array
output += " #{quoteize(value.first)} #{value.last}" output += " #{value.first.quoteize} #{value.last}"
end end
self << output self << output
end end
@ -115,14 +107,15 @@ module Apache
def server_name(*opts) def server_name(*opts)
if first = opts.shift if first = opts.shift
self << "ServerName #{quoteize(first)}" self << "ServerName #{first.quoteize}"
opts.each { |o| server_alias o } if !opts.empty? opts.each { |name| server_alias name } if !opts.empty?
end end
end end
def document_root(*opts) def document_root(*opts)
directory? opts.first dir = opts.first
self << "DocumentRoot #{quoteize(opts.first)}" directory? dir
self << "DocumentRoot #{dir.quoteize}"
end end
alias :document_root! :document_root alias :document_root! :document_root
end end

View File

@ -1,11 +1,7 @@
require 'apache/quoteize'
module Apache module Apache
# Create lists of modules to load in the Apache 2.2 style (with LoadModule only) # Create lists of modules to load in the Apache 2.2 style (with LoadModule only)
class Modules class Modules
class << self class << self
include Apache::Quoteize
attr_accessor :modules attr_accessor :modules
# Reset the list of modules to output # Reset the list of modules to output
@ -27,7 +23,7 @@ module Apache
def build(*modules, &block) def build(*modules, &block)
reset! reset!
modules.each { |m| self.send(m) } modules.each { |mod| self.send(mod) }
self.instance_eval(&block) if block self.instance_eval(&block) if block
[ '' ] + @modules + [ '' ] [ '' ] + @modules + [ '' ]
@ -37,7 +33,7 @@ module Apache
def method_missing(method, *args) def method_missing(method, *args)
module_name = "#{method}_module" module_name = "#{method}_module"
module_path = args[0] || "modules/mod_#{method}.so" module_path = args[0] || "modules/mod_#{method}.so"
@modules << [ 'LoadModule', *quoteize(module_name, module_path) ] * " " @modules << [ 'LoadModule', *[ module_name, module_path ].quoteize ] * " "
end end
end end
end end

View File

@ -21,7 +21,7 @@ module Apache
# #
# allow_from '127.0.0.1' #=> Allow from "127.0.0.1" # allow_from '127.0.0.1' #=> Allow from "127.0.0.1"
def allow_from(*where) def allow_from(*where)
self << "Allow from #{quoteize(*where) * " "}" self << "Allow from #{where.quoteize * " "}"
end end
# Specify default access order # Specify default access order
@ -58,26 +58,18 @@ module Apache
# basic_authentication "My other secret", '/my.users', :user => [ :john ] # basic_authentication "My other secret", '/my.users', :user => [ :john ]
def basic_authentication(zone, users_file, requires = {}) def basic_authentication(zone, users_file, requires = {})
exist? users_file exist? users_file
auth_type :basic authentication_basics(zone, requires)
auth_name zone
auth_user_file users_file auth_user_file users_file
requires.each do |type, values|
apache_require type, *values
end
end end
alias :basic_authentication! :basic_authentication alias :basic_authentication! :basic_authentication
# Set up LDAP authentication # Set up LDAP authentication
def ldap_authentication(zone, url, requires = {}) def ldap_authentication(zone, url, requires = {})
auth_type :basic authentication_basics(zone, requires)
auth_name zone
auth_basic_provider :ldap auth_basic_provider :ldap
authz_ldap_authoritative :on authz_ldap_authoritative :on
auth_ldap_url url auth_ldap_url url
requires.each do |type, values|
apache_require type, *values
end
end end
alias :ldap_authentication! :ldap_authentication alias :ldap_authentication! :ldap_authentication
@ -87,5 +79,14 @@ module Apache
def apache_require(*opts) def apache_require(*opts)
self << "Require #{opts.compact * " "}" self << "Require #{opts.compact * " "}"
end end
private
def authentication_basics(zone, requires)
auth_type :basic
auth_name zone
requires.each do |type, values|
apache_require type, *values
end
end
end end
end end

View File

@ -1,16 +0,0 @@
module Apache
# Add quotes around parameters as needed
module Quoteize
# Add quotes around most parameters, and don't add quotes around Symbols
def quoteize(*args)
args.collect do |arg|
case arg
when Symbol
arg.to_s.gsub('_', ' ')
else
%{"#{arg}"}
end
end
end
end
end

View File

@ -27,7 +27,7 @@ module Apache
end end
def rewrite(*opt, &block) def rewrite(*opt, &block)
raise "You probably want rewrites #{quoteize(*opt) * " "} do" if block raise "You probably want rewrites #{opt.quoteize * " "} do" if block
end end
# Create a permanent Redirect # Create a permanent Redirect
@ -37,7 +37,7 @@ module Apache
if opt.first && !opt.first.kind_of?(::String) if opt.first && !opt.first.kind_of?(::String)
raise "First parameter should be a String. Did you mean to wrap this in a rewrites block? #{opt.first}" raise "First parameter should be a String. Did you mean to wrap this in a rewrites block? #{opt.first}"
end end
self << "Redirect permanent #{quoteize(*opt) * " "}" self << "Redirect permanent #{opt.quoteize * " "}"
end end
end end
@ -192,8 +192,6 @@ module Apache
# A matchable thing to be extended # A matchable thing to be extended
class MatchableThing class MatchableThing
include Apache::Quoteize
attr_reader :from, :to attr_reader :from, :to
# The Apache directive tag for this thing # The Apache directive tag for this thing
@ -210,7 +208,7 @@ module Apache
end end
def to_s def to_s
"#{tag} #{[quoteize(@from), quoteize(@to)].compact.flatten * " "}" "#{tag} #{[@from, @to].quoteize.compact.flatten * " "}"
end end
def to_a def to_a
@ -275,7 +273,7 @@ module Apache
end end
def to_s def to_s
"#{tag} #{[quoteize(@from.source), quoteize(@to), @options].compact.flatten * " "}" "#{tag} #{[@from.source.quoteize, @to.quoteize, @options].compact.flatten * " "}"
end end
def to_a def to_a
@ -329,7 +327,7 @@ module Apache
end end
def to_s def to_s
"#{tag} #{[quoteize(@from.source), quoteize(@to)].compact.flatten * " "}" "#{tag} #{[@from.source, @to].quoteize.compact.flatten * " "}"
end end
def stop_if_match; true; end def stop_if_match; true; end
@ -370,7 +368,7 @@ module Apache
end end
def to_s def to_s
"#{tag} #{[quoteize(@from), quoteize(@to), @options].compact.flatten * " "}" "#{tag} #{[@from.quoteize, @to.quoteize, @options].compact.flatten * " "}"
end end
# Test this RewriteCond # Test this RewriteCond

View File

@ -41,8 +41,8 @@ describe Apache::Config, "builds configurations" do
end end
it "should quoteize properly" do it "should quoteize properly" do
apache.quoteize("test", "test2").should == %w{"test" "test2"} ["test", "test2"].quoteize.should == %w{"test" "test2"}
apache.quoteize(:test, :test2).should == %w{test test2} [:test, :test2].quoteize.should == %w{test test2}
end end
it "should blockify a block" do it "should blockify a block" do