Class: RakeRoutesNormalizer::Route

Inherits:
Valuable
  • Object
show all
Defined in:
lib/rake_routes_normalizer/route.rb

Defined Under Namespace

Classes: CannotNormalizeRoute, CannotParseLine

Constant Summary collapse

LINE_REGEX =
%r{ *([abcdefghijklmnopqrstuvwxyz_]*) +([ABCDEFGHIJKLMNOPQRSTUVWXYZ]*) +(\S+) +(.*)}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(line) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rake_routes_normalizer/route.rb', line 10

def self.parse(line)
  new.tap do |result|
    tokens = line.match(LINE_REGEX)
    result.name = tokens[1]
    result.http_verb = tokens[2]
    result.url_pattern = tokens[3]
    result.params = eval(tokens[4])
  end
rescue NoMethodError => e
  raise CannotParseLine.new(:line => line)
end

Instance Method Details

#<=>(other) ⇒ Object



22
23
24
25
26
# File 'lib/rake_routes_normalizer/route.rb', line 22

def <=>(other)
  [:url_pattern, :http_verb].inject(0) do |result, attribute|
    result == 0 ? (self.send(attribute) || '') <=> (other.send(attribute) || '') : result
  end
end

#normalize(options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rake_routes_normalizer/route.rb', line 28

def normalize(options = {})
  clone.tap do |result|
    result.http_verb = 'GET' if result.http_verb.to_s =~ /^\s*$/
    result.name = options[:previous_route].name if result.name =~ /^\s*$/ && options[:previous_route]
    result.params = Dictionary[KeyHash[params || {}]].order_by_key
    result.url_pattern = "#{result.url_pattern}(.:format)" unless result.url_pattern =~ /\(\.:format\)/
  end

rescue Exception => e
  raise CannotNormalizeRoute.new(:self => self, :options => options)
end