Class: Usher::Grapher
- Inherits:
-
Object
- Object
- Usher::Grapher
- Defined in:
- lib/usher/grapher.rb
Overview
Find nearest matching routes based on parameter keys.
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#key_count ⇒ Object
readonly
Returns the value of attribute key_count.
-
#orders ⇒ Object
readonly
Returns the value of attribute orders.
-
#router ⇒ Object
readonly
Returns the value of attribute router.
-
#significant_keys ⇒ Object
readonly
Returns the value of attribute significant_keys.
Instance Method Summary collapse
-
#add_route(route) ⇒ Object
Add route for matching.
-
#find_matching_path(params) ⇒ nil, Route
Finds a matching path based on params hash.
-
#initialize(router) ⇒ Grapher
constructor
A new instance of Grapher.
Constructor Details
#initialize(router) ⇒ Grapher
Returns a new instance of Grapher.
8 9 10 11 |
# File 'lib/usher/grapher.rb', line 8 def initialize(router) @router = router reset! end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
5 6 7 |
# File 'lib/usher/grapher.rb', line 5 def cache @cache end |
#key_count ⇒ Object (readonly)
Returns the value of attribute key_count.
5 6 7 |
# File 'lib/usher/grapher.rb', line 5 def key_count @key_count end |
#orders ⇒ Object (readonly)
Returns the value of attribute orders.
5 6 7 |
# File 'lib/usher/grapher.rb', line 5 def orders @orders end |
#router ⇒ Object (readonly)
Returns the value of attribute router.
5 6 7 |
# File 'lib/usher/grapher.rb', line 5 def router @router end |
#significant_keys ⇒ Object (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
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
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 |