From af01487160093b6a89dcd0403a190f77937bf7d9 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 18 May 2010 12:43:57 -0400 Subject: [PATCH] rework some things to make reek happy --- Rakefile | 6 ++++ lib/apache.rb | 1 + lib/apache/apachify.rb | 48 ++++++++++++++++++++++++++++++ lib/apache/config.rb | 66 +++++++++++++++++++---------------------- lib/apache/directory.rb | 15 +--------- lib/apache/master.rb | 6 ++++ lib/apache/ssl.rb | 2 +- spec/apachify_spec.rb | 17 +++++++++++ spec/config_spec.rb | 18 ++--------- 9 files changed, 112 insertions(+), 67 deletions(-) create mode 100644 lib/apache/apachify.rb create mode 100644 spec/apachify_spec.rb diff --git a/Rakefile b/Rakefile index 1e1dc81..27928d4 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,7 @@ $LOAD_PATH << 'lib' +require 'rubygems' + require 'apache' require 'spec/rake/spectask' require 'sdoc' @@ -39,3 +41,7 @@ Rake::RDocTask.new do |rdoc| rdoc.main = 'README.rdoc' rdoc.rdoc_dir = 'docs' end + +task :reek do + system('reek lib/*') +end diff --git a/lib/apache.rb b/lib/apache.rb index d49c9b4..03ebe1e 100644 --- a/lib/apache.rb +++ b/lib/apache.rb @@ -1 +1,2 @@ require 'apache/config' +require 'apache/apachify' diff --git a/lib/apache/apachify.rb b/lib/apache/apachify.rb new file mode 100644 index 0000000..8ef7b04 --- /dev/null +++ b/lib/apache/apachify.rb @@ -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 diff --git a/lib/apache/config.rb b/lib/apache/config.rb index 539cca7..9da2d88 100644 --- a/lib/apache/config.rb +++ b/lib/apache/config.rb @@ -1,3 +1,4 @@ +require 'rubygems' require 'fileutils' require 'rainbow' @@ -71,19 +72,27 @@ module Apache end # Build the provided configuration - def build(target = nil, &block) + def build_and_return(&block) reset! 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 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 def reset! @config = [] @@ -94,7 +103,7 @@ module Apache def indent(string_or_array) case string_or_array when Array - string_or_array.collect { |s| indent(s) } + string_or_array.collect { |line| indent(line) } else " " * (@line_indent * 2) + string_or_array.to_s end @@ -115,35 +124,18 @@ module Apache @config 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 # # Method names are NerdCapsed and paramters are quoted, unless the method ends with ! def method_missing(method, *args) - if method.to_s[-1..-1] == "!" - method = method.to_s[0..-2].to_sym + method_name = method.to_s + if method_name[-1..-1] == "!" + method = method_name[0..-2].to_sym else args = *quoteize(*args) end - self << [ apachify(method), *args ].compact * ' ' + self << [ method.apachify, *args ].compact * ' ' end # Handle creating block methods @@ -156,7 +148,7 @@ module Apache methods.each do |method| self.class.class_eval <<-EOT def #{method}(*name, &block) - blockify(apachify("#{method}"), name, &block) + blockify("#{method}".apachify, name, &block) end EOT end @@ -167,25 +159,25 @@ module Apache # The provided module name is converted into Apache module name format: # if_module(:php5) do #=> def if_module(mod, &block) - blockify(apachify('if_module'), "#{mod}_module".to_sym, &block) + blockify('if_module'.apachify, "#{mod}_module".to_sym, &block) end # Create a directory block, checking to see if the source directory exists. def directory(dir, &block) directory? dir - blockify(apachify('directory'), dir, &block) + blockify('directory'.apachify, dir, &block) end # Create a LocationMatch block with the provided Regexp: # location_match %r{^/my/location/[a-z0-9]+\.html} do #=> def location_match(regexp, &block) - blockify(apachify('location_match'), regexp.source, &block) + blockify('location_match'.apachify, regexp.source, &block) end # Create a FilesMatch block with the provied Regexp: # files_match %r{\.html$} do #=> FilesMatch "\.html$"> def files_match(regexp, &block) - blockify(apachify('files_match'), regexp.source, &block) + blockify('files_match'.apachify, regexp.source, &block) end # 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 def blockify(tag_name, name, &block) - self << "" - self << "<#{[ tag_name, blockify_name(name) ].compact * ' '}>" + self + [ '', "<#{[ tag_name, blockify_name(name) ].compact * ' '}>" ] @line_indent += 1 self.instance_eval(&block) @line_indent -= 1 - self << "" + self + [ "", '' ] + end + + def blank_line! self << "" end diff --git a/lib/apache/directory.rb b/lib/apache/directory.rb index a1205b2..6cbaf78 100644 --- a/lib/apache/directory.rb +++ b/lib/apache/directory.rb @@ -19,20 +19,7 @@ module Apache private def create_options_list(tag, *opt) - opt = opt.collect do |o| - 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 * " "}" + self << "#{tag} #{opt.collect(&:optionify) * " "}" end end end diff --git a/lib/apache/master.rb b/lib/apache/master.rb index 01c353b..bfe3906 100644 --- a/lib/apache/master.rb +++ b/lib/apache/master.rb @@ -8,6 +8,12 @@ module Apache @config += Modules.build(*modules, &block) 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) opt.each { |o| self << "Listen #{quoteize(o)}" } end diff --git a/lib/apache/ssl.rb b/lib/apache/ssl.rb index 650196e..28d8774 100644 --- a/lib/apache/ssl.rb +++ b/lib/apache/ssl.rb @@ -7,7 +7,7 @@ module Apache value = quoteize(value).first case key when :certificate_file, :certificate_key_file - self << "SSL#{apachify(key)} #{value}" + self << "SSL#{key.apachify} #{value}" when :ca_certificate_file self << "SSLCACertificateFile #{value}" end diff --git a/spec/apachify_spec.rb b/spec/apachify_spec.rb new file mode 100644 index 0000000..011d70a --- /dev/null +++ b/spec/apachify_spec.rb @@ -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 diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 0808e3e..a9ee1c4 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -40,20 +40,6 @@ describe Apache::Config, "builds configurations" do ] 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 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 set_apache_env(:test) - apache.build_if(nil, :other) { my_test 'this' }.should == nil - apache.build_if(nil, :test) { my_test 'this' }.should == [ 'MyTest "this"' ] + apache.build_and_return_if(:other) { my_test 'this' }.should == nil + apache.build_and_return_if(:test) { my_test 'this' }.should == [ 'MyTest "this"' ] end it "should only execute a block if the environment is correct" do