Class: Stratagem::Model::Component::Route

Inherits:
Base show all
Defined in:
lib/stratagem/model/components/route.rb

Constant Summary

Constants included from ParseUtil

ParseUtil::RUBY_OUTPUT_REGEX, ParseUtil::RUBY_REGEX

Instance Attribute Summary collapse

Attributes inherited from Base

#app_model, #klass, #parse_tree, #source

Instance Method Summary collapse

Methods inherited from Base

load_all, logger, #logger, #name

Methods included from ParseUtil

find_classes, #gsub_ruby_blocks, qualified_class_name, #ruby_blocks, #ruby_output_blocks

Constructor Details

#initialize(route) ⇒ Route

Returns a new instance of Route.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/stratagem/model/components/route.rb', line 6

def initialize(route)
  @route = route
  if (Stratagem.rails_3?)
    @path = route.path
    @controller_path = route.requirements[:controller]
    @action_name = route.requirements[:action]
    @verb = (route.verb || :GET).downcase.to_sym
  elsif (Stratagem.rails_2?)
    @path = route.segments.join.sub(/[\?\/]$/, '')
    @controller_path = route.parameter_shell[:controller]
    @action_name = route.parameter_shell[:action]
    @verb = route.conditions[:method] || :get
  end
  
  @segment_keys = route.segment_keys
  @action_name = @action_name.to_sym if @action_name
  if (@controller_path)
    @controller_name = @controller_path.gsub('/','::').split('::').map {|part| part.camelcase }.join('::')
    @controller_name << 'Controller'
  end
end

Instance Attribute Details

#action_nameObject (readonly)

Returns the value of attribute action_name.



4
5
6
# File 'lib/stratagem/model/components/route.rb', line 4

def action_name
  @action_name
end

#controller_nameObject (readonly)

Returns the value of attribute controller_name.



4
5
6
# File 'lib/stratagem/model/components/route.rb', line 4

def controller_name
  @controller_name
end

#controller_pathObject (readonly)

Returns the value of attribute controller_path.



4
5
6
# File 'lib/stratagem/model/components/route.rb', line 4

def controller_path
  @controller_path
end

#invalidObject

Returns the value of attribute invalid.



3
4
5
# File 'lib/stratagem/model/components/route.rb', line 3

def invalid
  @invalid
end

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/stratagem/model/components/route.rb', line 4

def path
  @path
end

#routeObject (readonly)

Returns the value of attribute route.



4
5
6
# File 'lib/stratagem/model/components/route.rb', line 4

def route
  @route
end

#segment_keysObject (readonly)

Returns the value of attribute segment_keys.



4
5
6
# File 'lib/stratagem/model/components/route.rb', line 4

def segment_keys
  @segment_keys
end

#verbObject (readonly)

Returns the value of attribute verb.



4
5
6
# File 'lib/stratagem/model/components/route.rb', line 4

def verb
  @verb
end

Instance Method Details

#==(other) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/stratagem/model/components/route.rb', line 56

def ==(other)
  if (other.kind_of?(ActionController::Routing::Route))
    self.route == other
  elsif (other.kind_of?(Stratagem::Model::Component::Route))
    self.route == other.route
  else
    raise "unknown comparison to #{other.class.name}"
  end
end

#controllerObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/stratagem/model/components/route.rb', line 40

def controller
  if (controller_name)
    begin
      controller_class = controller_name.constantize
      Stratagem::Model::Application.instance.controllers.find {|c| c.klass.name == controller_class.name }
    rescue
      puts "unable to determine controller: #{route.requirements[:controller]}"
      puts $!.message
      nil
    end
  else
    puts "ERROR: no controller name"
    nil
  end
end

#exportObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/stratagem/model/components/route.rb', line 66

def export
  {
    :external_id => self.object_id,
    :path => path,
    :verb => verb,
    :controller_external_id => controller ? controller.object_id : nil,
    :action => action_name,
    :valid_route => invalid.nil? ? true : !invalid
  }
end

#responds_to?(path, request_method) ⇒ Boolean

Returns:

  • (Boolean)


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

def responds_to?(path, request_method)
  if (Stratagem.rails_3?)
    begin
      route.set.recognize_path(path, :method => request_method) == route.requirements
    rescue ActionController::RoutingError
      puts "ROUTE ERROR: #{$!.message}"
    end
  elsif (Stratagem.rails_2?)
    route.recognize(path, {:method => request_method}) 
  end
end