Class: Usher::Grapher

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

Overview

Find nearest matching routes based on parameter keys.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(router) ⇒ Grapher

Returns a new instance of Grapher.

Parameters:

  • router

    An Usher instance you wish to create a grapher for.



8
9
10
11
# File 'lib/usher/grapher.rb', line 8

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

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



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

def cache
  @cache
end

#key_countObject (readonly)

Returns the value of attribute key_count.



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

def key_count
  @key_count
end

#ordersObject (readonly)

Returns the value of attribute orders.



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

def orders
  @orders
end

#routerObject (readonly)

Returns the value of attribute router.



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

def router
  @router
end

#significant_keysObject (readonly)

Returns the value of attribute significant_keys.



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

def significant_keys
  @significant_keys
end

Instance Method Details

#add_route(route) ⇒ Object

Add route for matching

Parameters:

  • route (Route)

    Add route for matching against



15
16
17
18
# File 'lib/usher/grapher.rb', line 15

def add_route(route)
  @cache.clear
  process_route(route)
end

#find_matching_path(params) ⇒ nil, Route

Finds a matching path based on params hash

Parameters:

  • params (Hash<Symbol, String>)

    A hash of parameters you wish to use in matching.

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/usher/grapher.rb', line 23

def find_matching_path(params)
  unless params.empty?
    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