Class: Usher::Grapher
- Inherits:
-
Object
- Object
- Usher::Grapher
- Defined in:
- lib/usher/grapher.rb
Instance Method Summary collapse
- #add_route(route) ⇒ Object
- #find_matching_path(params) ⇒ Object
-
#initialize ⇒ Grapher
constructor
A new instance of Grapher.
- #reset! ⇒ Object
- #significant_keys ⇒ Object
Constructor Details
#initialize ⇒ Grapher
Returns a new instance of Grapher.
4 5 6 |
# File 'lib/usher/grapher.rb', line 4 def initialize reset! end |
Instance Method Details
#add_route(route) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/usher/grapher.rb', line 15 def add_route(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 end end |
#find_matching_path(params) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/usher/grapher.rb', line 45 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?(set) @cache[set] = r return r end end end end end nil end |
#reset! ⇒ Object
8 9 10 11 12 13 |
# File 'lib/usher/grapher.rb', line 8 def reset! @significant_keys = nil @orders = Hash.new{|h,k| h[k] = Hash.new{|h2, k2| h2[k2] = []}} @key_count = Hash.new(0) @cache = {} end |
#significant_keys ⇒ Object
41 42 43 |
# File 'lib/usher/grapher.rb', line 41 def significant_keys @significant_keys ||= @key_count.keys.uniq end |