Module: Baurets::Optionsful::Introspections

Defined in:
lib/baurets/optionsful/introspections.rb

Class Method Summary collapse

Class Method Details

.do_routing_introspectionObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/baurets/optionsful/introspections.rb', line 14

def self.do_routing_introspection
  returning Array.new do |routes|
    route_requirements = nil
    ActionController::Routing::Routes.named_routes.map.each do |named_route|
      name = named_route[0].to_s
      routes << [[name], ["GET", named_route[1].requirements[:action]], {:controller => named_route[1].requirements[:controller], :action => named_route[1].requirements[:action]}]
      #TODO ANY ?!?
    end
    ActionController::Routing::Routes.routes.each do |route|
      static_path = []
      route.segments.each do |segment|
        route_requirements = route.requirements #TODO validate 
        if segment.kind_of?(ActionController::Routing::StaticSegment)
          static_path << segment.value if (segment.respond_to?(:value) && segment.value != "/")
        elsif segment.kind_of?(ActionController::Routing::DynamicSegment)
          static_path << :dynamic unless (segment.respond_to?(:key) && segment.key == :format)   
        end
      end
      routes << [static_path, [route.conditions[:method].to_s.upcase, route_requirements[:action]], route_requirements] unless route.conditions[:method].nil?
    end
  end
end

.do_the_matches(routes, route_guess) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/baurets/optionsful/introspections.rb', line 52

def self.do_the_matches(routes, route_guess)
  allow = ""
  routes.each do |route|
    if route.first == route_guess
      allow += (route[1][0].to_s.upcase + "|") unless allow.include?(route[1][0].to_s.upcase)
    end
  end
  allow = allow.split("|").join(", ") 
end

.guess_route(routes, path) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/baurets/optionsful/introspections.rb', line 37

def self.guess_route(routes, path)
  guess = []
  parts = prepare_request_path(path)
  index = 0
  parts.each do |part|
    if is_part_static?(routes, index, part)
      guess << part
    else
      guess << :dynamic
    end
    index += 1
  end
  guess
end

.is_part_static?(routes, index, value) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
# File 'lib/baurets/optionsful/introspections.rb', line 5

def self.is_part_static?(routes, index, value)
  routes.each do |route|
    return true if route[0][index] == value
  end
  return false

end

.prepare_request_path(path) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/baurets/optionsful/introspections.rb', line 62

def self.prepare_request_path(path)
  path_parts = []
  path = path[0..(path.rindex('.')-1)] if path.include?('.')
  path_parts = path.split("/")
  path_parts.delete("")
  path_parts
end