rework some things to make reek happy

This commit is contained in:
John Bintz 2010-05-18 12:43:57 -04:00
parent 34631862a4
commit af01487160
9 changed files with 112 additions and 67 deletions

View File

@ -1,5 +1,7 @@
$LOAD_PATH << 'lib' $LOAD_PATH << 'lib'
require 'rubygems'
require 'apache' require 'apache'
require 'spec/rake/spectask' require 'spec/rake/spectask'
require 'sdoc' require 'sdoc'
@ -39,3 +41,7 @@ Rake::RDocTask.new do |rdoc|
rdoc.main = 'README.rdoc' rdoc.main = 'README.rdoc'
rdoc.rdoc_dir = 'docs' rdoc.rdoc_dir = 'docs'
end end
task :reek do
system('reek lib/*')
end

View File

@ -1 +1,2 @@
require 'apache/config' require 'apache/config'
require 'apache/apachify'

48
lib/apache/apachify.rb Normal file
View File

@ -0,0 +1,48 @@
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
end
# Ruby symbols
class Symbol
include Apache::Apachify
def optionify
output = self.apachify
output = "-#{output[4..-1].apachify}" if self.to_s[0..3] == 'not_'
output
end
end
# Ruby arrays
class Array
# Apachify all the elements within this array
def apachify
self.collect(&:apachify)
end
end

View File

@ -1,3 +1,4 @@
require 'rubygems'
require 'fileutils' require 'fileutils'
require 'rainbow' require 'rainbow'
@ -71,19 +72,27 @@ module Apache
end end
# Build the provided configuration # Build the provided configuration
def build(target = nil, &block) def build_and_return(&block)
reset! reset!
self.instance_eval(&block) self.instance_eval(&block)
if target
FileUtils.mkdir_p File.split(target).first
File.open(target, 'w') { |f| f.puts [ "# Generated by apache-config-generator #{Time.now.to_s}", @config ].flatten * "\n" }
end
@config @config
end end
def build_and_return_if(*conditions, &block)
build_and_return(&block) if conditions.include? APACHE_ENV
end
def build(target, &block)
config = build_and_return(&block)
FileUtils.mkdir_p File.split(target).first
File.open(target, 'w') { |file| file.puts [ "# Generated by apache-config-generator #{Time.now.to_s}", config ].flatten * "\n" }
config
end
# Reset the current settings # Reset the current settings
def reset! def reset!
@config = [] @config = []
@ -94,7 +103,7 @@ module Apache
def indent(string_or_array) def indent(string_or_array)
case string_or_array case string_or_array
when Array when Array
string_or_array.collect { |s| indent(s) } string_or_array.collect { |line| indent(line) }
else else
" " * (@line_indent * 2) + string_or_array.to_s " " * (@line_indent * 2) + string_or_array.to_s
end end
@ -115,35 +124,18 @@ module Apache
@config @config
end end
# 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(name)
case name
when String, Symbol
name.to_s.split("_").collect(&:capitalize).join.gsub('Ssl', 'SSL').
gsub('Cgi', 'CGI').gsub('Ldap', 'LDAP').gsub('Url', 'URL').
gsub('Etag', 'ETag')
when Array
name.collect { |n| apachify(n) }
end
end
# Handle options that aren't specially handled # Handle options that aren't specially handled
# #
# Method names are NerdCapsed and paramters are quoted, unless the method ends with ! # Method names are NerdCapsed and paramters are quoted, unless the method ends with !
def method_missing(method, *args) def method_missing(method, *args)
if method.to_s[-1..-1] == "!" method_name = method.to_s
method = method.to_s[0..-2].to_sym if method_name[-1..-1] == "!"
method = method_name[0..-2].to_sym
else else
args = *quoteize(*args) args = *quoteize(*args)
end end
self << [ apachify(method), *args ].compact * ' ' self << [ method.apachify, *args ].compact * ' '
end end
# Handle creating block methods # Handle creating block methods
@ -156,7 +148,7 @@ module Apache
methods.each do |method| methods.each do |method|
self.class.class_eval <<-EOT self.class.class_eval <<-EOT
def #{method}(*name, &block) def #{method}(*name, &block)
blockify(apachify("#{method}"), name, &block) blockify("#{method}".apachify, name, &block)
end end
EOT EOT
end end
@ -167,25 +159,25 @@ module Apache
# The provided module name is converted into Apache module name format: # The provided module name is converted into Apache module name format:
# if_module(:php5) do #=> <IfModule mod_php5> # if_module(:php5) do #=> <IfModule mod_php5>
def if_module(mod, &block) def if_module(mod, &block)
blockify(apachify('if_module'), "#{mod}_module".to_sym, &block) blockify('if_module'.apachify, "#{mod}_module".to_sym, &block)
end end
# Create a directory block, checking to see if the source directory exists. # Create a directory block, checking to see if the source directory exists.
def directory(dir, &block) def directory(dir, &block)
directory? dir directory? dir
blockify(apachify('directory'), dir, &block) blockify('directory'.apachify, dir, &block)
end end
# Create a LocationMatch block with the provided Regexp: # Create a LocationMatch block with the provided Regexp:
# location_match %r{^/my/location/[a-z0-9]+\.html} do #=> <LocationMatch "^/my/location/[a-z0-9]+\.html"> # location_match %r{^/my/location/[a-z0-9]+\.html} do #=> <LocationMatch "^/my/location/[a-z0-9]+\.html">
def location_match(regexp, &block) def location_match(regexp, &block)
blockify(apachify('location_match'), regexp.source, &block) blockify('location_match'.apachify, regexp.source, &block)
end end
# Create a FilesMatch block with the provied Regexp: # Create a FilesMatch block with the provied Regexp:
# files_match %r{\.html$} do #=> FilesMatch "\.html$"> # files_match %r{\.html$} do #=> FilesMatch "\.html$">
def files_match(regexp, &block) def files_match(regexp, &block)
blockify(apachify('files_match'), regexp.source, &block) blockify('files_match'.apachify, regexp.source, &block)
end end
# Only execute the provided block if APACHE_ENV matches one of the provided enviroment symbols: # Only execute the provided block if APACHE_ENV matches one of the provided enviroment symbols:
@ -213,12 +205,14 @@ module Apache
# Handle the blockification of a provided block # Handle the blockification of a provided block
def blockify(tag_name, name, &block) def blockify(tag_name, name, &block)
self << "" self + [ '', "<#{[ tag_name, blockify_name(name) ].compact * ' '}>" ]
self << "<#{[ tag_name, blockify_name(name) ].compact * ' '}>"
@line_indent += 1 @line_indent += 1
self.instance_eval(&block) self.instance_eval(&block)
@line_indent -= 1 @line_indent -= 1
self << "</#{tag_name}>" self + [ "</#{tag_name}>", '' ]
end
def blank_line!
self << "" self << ""
end end

View File

@ -19,20 +19,7 @@ module Apache
private private
def create_options_list(tag, *opt) def create_options_list(tag, *opt)
opt = opt.collect do |o| self << "#{tag} #{opt.collect(&:optionify) * " "}"
case o
when Symbol
if o.to_s[0..3] == 'not_'
"-#{apachify(o.to_s[4..-1])}"
else
apachify(o)
end
else
apachify(o)
end
end
self << "#{tag} #{opt * " "}"
end end
end end
end end

View File

@ -8,6 +8,12 @@ module Apache
@config += Modules.build(*modules, &block) @config += Modules.build(*modules, &block)
end end
# Listen on network interfaces and ports
#
# Each provided parameter generates a new Listen line:
# listen "1.2.3.4:80", "2.3.4.5:80" #=>
# Listen "1.2.3.4:80"
# Listen "2.3.4.5:80"
def listen(*opt) def listen(*opt)
opt.each { |o| self << "Listen #{quoteize(o)}" } opt.each { |o| self << "Listen #{quoteize(o)}" }
end end

View File

@ -7,7 +7,7 @@ module Apache
value = quoteize(value).first value = quoteize(value).first
case key case key
when :certificate_file, :certificate_key_file when :certificate_file, :certificate_key_file
self << "SSL#{apachify(key)} #{value}" self << "SSL#{key.apachify} #{value}"
when :ca_certificate_file when :ca_certificate_file
self << "SSLCACertificateFile #{value}" self << "SSLCACertificateFile #{value}"
end end

17
spec/apachify_spec.rb Normal file
View File

@ -0,0 +1,17 @@
require 'apache/apachify'
describe Apache::Apachify, "extends objects to apachify themselves" do
it "should Apachify the name" do
[
%w{test Test},
%w{test_full_name TestFullName},
%w{ssl_option SSLOption},
%w{exec_cgi ExecCGI},
%w{authz_ldap_authoritative AuthzLDAPAuthoritative},
%w{authz_ldap_url AuthzLDAPURL},
[ ["name", "other_name"], [ 'Name', 'OtherName' ] ]
].each do |input, output|
input.apachify.should == output
end
end
end

View File

@ -40,20 +40,6 @@ describe Apache::Config, "builds configurations" do
] ]
end end
it "should Apachify the name" do
[
%w{test Test},
%w{test_full_name TestFullName},
%w{ssl_option SSLOption},
%w{exec_cgi ExecCGI},
%w{authz_ldap_authoritative AuthzLDAPAuthoritative},
%w{authz_ldap_url AuthzLDAPURL},
[ ["name", "other_name"], [ 'Name', 'OtherName' ] ]
].each do |input, output|
apache.apachify(input).should == output
end
end
it "should quoteize properly" do it "should quoteize properly" do
apache.quoteize("test", "test2").should == %w{"test" "test2"} apache.quoteize("test", "test2").should == %w{"test" "test2"}
apache.quoteize(:test, :test2).should == %w{test test2} apache.quoteize(:test, :test2).should == %w{test test2}
@ -84,8 +70,8 @@ describe Apache::Config, "builds configurations" do
it "should handle building if the environment is correct" do it "should handle building if the environment is correct" do
set_apache_env(:test) set_apache_env(:test)
apache.build_if(nil, :other) { my_test 'this' }.should == nil apache.build_and_return_if(:other) { my_test 'this' }.should == nil
apache.build_if(nil, :test) { my_test 'this' }.should == [ 'MyTest "this"' ] apache.build_and_return_if(:test) { my_test 'this' }.should == [ 'MyTest "this"' ]
end end
it "should only execute a block if the environment is correct" do it "should only execute a block if the environment is correct" do