Class: Usher::Grapher

Inherits:
Object
  • Object
show all
Defined in:
lib/usher/grapher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(router) ⇒ Grapher

Returns a new instance of Grapher.



6
7
8
9
# File 'lib/usher/grapher.rb', line 6

def initialize(router)
  @router = router
  reset!
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



4
5
6
# File 'lib/usher/grapher.rb', line 4

def cache
  @cache
end

#key_countObject (readonly)

Returns the value of attribute key_count.



4
5
6
# File 'lib/usher/grapher.rb', line 4

def key_count
  @key_count
end

#ordersObject (readonly)

Returns the value of attribute orders.



4
5
6
# File 'lib/usher/grapher.rb', line 4

def orders
  @orders
end

#routerObject (readonly)

Returns the value of attribute router.



4
5
6
# File 'lib/usher/grapher.rb', line 4

def router
  @router
end

#routesObject (readonly)

Returns the value of attribute routes.



4
5
6
# File 'lib/usher/grapher.rb', line 4

def routes
  @routes
end

Instance Method Details

#add_route(route) ⇒ Object



19
20
21
# File 'lib/usher/grapher.rb', line 19

def add_route(route)
  routes << route
end

#find_matching_path(params) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/usher/grapher.rb', line 65

def find_matching_path(params)
  unless params.empty?
    process_routes
    set = params.keys & significant_keys
    if cached = cache[set] 
      return cached
    end
    set.size.downto(1) do |o|
      set.each do |k|
        orders[o][k].each do |r| 
          if r.can_generate_from_keys?(set)
            cache[set] = r
            return r
          elsif router.consider_destination_keys? && r.can_generate_from_params?(params)
            return r
          end
        end
      end
    end
  end
  nil
end

#process_routesObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/usher/grapher.rb', line 23

def process_routes
  return if @processed 
  routes.each do |route|
    route.paths.each do |path|
      if path.dynamic?
        path.dynamic_keys.each do |k|
          orders[path.dynamic_keys.size][k] << path
          key_count[k] += 1
        end

        dynamic_parts_with_defaults    = path.dynamic_parts.select{|part| part.default_value }.map{|dp| dp.name}
        dynamic_parts_without_defaults = path.dynamic_parts.select{|part| !part.default_value }.map{|dp| dp.name}

        (1...(2 ** (dynamic_parts_with_defaults.size))).each do |i|
          current_set = dynamic_parts_without_defaults.dup
          dynamic_parts_with_defaults.each_with_index do |dp, index|
            current_set << dp unless (index & i) == 0
          end

          current_set.each do |k|
            orders[current_set.size][k] << path
            key_count[k] += 1
          end
        end

      end

      if router.consider_destination_keys?
        path.route.destination_keys.each do |k|
          orders[path.route.destination_keys.size][k] << path
          key_count[k] += 1
        end
      end
    end
  end
  @processed = true
end

#reset!Object



11
12
13
14
15
16
17
# File 'lib/usher/grapher.rb', line 11

def reset!
  @significant_keys = nil
  @orders = Hash.new{|h,k| h[k] = Hash.new{|h2, k2| h2[k2] = []}}
  @key_count = Hash.new(0)
  @cache = {}
  @routes = []
end

#significant_keysObject



61
62
63
# File 'lib/usher/grapher.rb', line 61

def significant_keys
  @significant_keys ||= key_count.keys.uniq
end