Class: Apache::RewriteManager

Inherits:
Object
  • Object
show all
Defined in:
lib/apache/rewrites.rb

Overview

Handle the creation of Rewritable things

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.rewritesObject

Returns the value of attribute rewrites.



49
50
51
# File 'lib/apache/rewrites.rb', line 49

def rewrites
  @rewrites
end

Class Method Details

.block_name(first) ⇒ Object



71
72
73
74
75
# File 'lib/apache/rewrites.rb', line 71

def block_name(first)
  first_rewrite = @rewrites.first

  first || (@rewrites.empty? ? 'unnamed block' : "#{first_rewrite.from} => #{first_rewrite.to}")
end

.build(*opt, &block) ⇒ Object

Build rewritable things from the provided block



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/apache/rewrites.rb', line 59

def build(*opt, &block)
  reset!

  self.instance_eval(&block)

  name = block_name(opt.first)

  show_messages! name

  [ "# #{name}", @rewrites.collect(&:to_a) ].flatten
end

.commit!Object

Commit the latest rewritable thing to the list of rewrites



85
86
87
88
# File 'lib/apache/rewrites.rb', line 85

def commit!
  @rewrites << @rewrite
  @rewrite = nil
end

.cond(*opts) ⇒ Object

Create a RewriteCond with the given options

cond "%{REQUEST_FILENAME}", "^/here" #=>
  RewriteCond "%{REQUEST_FILENAME}", "^/here"


111
112
113
114
# File 'lib/apache/rewrites.rb', line 111

def cond(*opts)
  ensure_rewrite!
  @rewrite.cond(*opts)
end

.cond_is_a_directory(opts = {}) ⇒ Object



162
163
164
# File 'lib/apache/rewrites.rb', line 162

def cond_is_a_directory(opts = {})
  cond_file_flag '-d', opts
end

.cond_is_a_file(opts = {}) ⇒ Object



154
155
156
# File 'lib/apache/rewrites.rb', line 154

def cond_is_a_file(opts = {})
  cond_file_flag '-f', opts
end

.cond_not_a_directory(opts = {}) ⇒ Object



158
159
160
# File 'lib/apache/rewrites.rb', line 158

def cond_not_a_directory(opts = {})
  cond_file_flag '!-d', opts
end

.cond_not_a_file(opts = {}) ⇒ Object



150
151
152
# File 'lib/apache/rewrites.rb', line 150

def cond_not_a_file(opts = {})
  cond_file_flag '!-f', opts
end

.ensure_rewrite!Object

Ensure that there’s a RewriteRule to be worked with



91
92
93
# File 'lib/apache/rewrites.rb', line 91

def ensure_rewrite!
  @rewrite = RewriteRule.new if !@rewrite
end

.needs_testsObject



146
147
148
# File 'lib/apache/rewrites.rb', line 146

def needs_tests
  @needs_tests = true
end

.r301(*opts) ⇒ Object

Create a permanent RedirectMatch

r301 %r{/here(.*)}, "/there$1" #=>
  RedirectMatch permanent "/here(.*)" "/there$1"


120
121
122
123
124
125
# File 'lib/apache/rewrites.rb', line 120

def r301(*opts)
  redirect = RedirectMatchPermanent.new
  redirect.rule(*opts)

  @rewrites << redirect
end

.reset!Object

Reset the current list of rewrites



52
53
54
55
56
# File 'lib/apache/rewrites.rb', line 52

def reset!
  @rewrites = []
  @any_tests = false
  @needs_tests = false
end

.rewrite(*opts) ⇒ Object Also known as: rule

Create a RewriteRule with the given options

rewrite %r{/here(.*)}, '/there$1', :last => true #=>
  RewriteRule "/here(.*)" "/there$1" [L]


99
100
101
102
103
# File 'lib/apache/rewrites.rb', line 99

def rewrite(*opts)
  ensure_rewrite!
  @rewrite.rule(*opts)
  commit!
end

.rewrite_test(from, to, opts = {}) ⇒ Object

Test the rewritable things defined in this block



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/apache/rewrites.rb', line 128

def rewrite_test(from, to, opts = {})
  @any_tests = true
  orig_from = from.dup
  @rewrites.each do |rewrite|
    if rewrite.match?(from, opts)
      from = rewrite.from_tester(from, opts)
      break if rewrite.stop_if_match?
    end
  end

  if from != to
    [ "#{orig_from} >> #{to} failed!", "Result: #{from}"
    ].each do |line|
      puts "  [#{"rewrite".foreground(:blue)}] #{line}"
    end
  end
end

.show_messages!(name) ⇒ Object



77
78
79
80
81
82
# File 'lib/apache/rewrites.rb', line 77

def show_messages!(name)
  blue = "rewrite".foreground(:blue)

  puts "  [#{blue}] no tests found for #{name}" if !@any_tests && !@rewrites.empty?
  puts "  [#{blue}] #{name} needs more tests" if @needs_tests
end