Sha256: 83fe9745fcce8f6421211baa141b11955aaf7db099b0a1e22618f0cb5be2a347

Contents?: true

Size: 1.68 KB

Versions: 17

Compression:

Stored size: 1.68 KB

Contents

class Usher
  class Grapher

    def initialize
      reset!
    end

    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

    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

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

    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
    
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
joshbuddy-usher-0.5.2 lib/usher/grapher.rb
joshbuddy-usher-0.5.3 lib/usher/grapher.rb
joshbuddy-usher-0.5.4 lib/usher/grapher.rb
joshbuddy-usher-0.5.6 lib/usher/grapher.rb
joshbuddy-usher-0.5.7 lib/usher/grapher.rb
usher-0.5.13 lib/usher/grapher.rb
usher-0.5.12 lib/usher/grapher.rb
usher-0.5.11 lib/usher/grapher.rb
usher-0.5.10 lib/usher/grapher.rb
usher-0.5.8 lib/usher/grapher.rb
usher-0.5.7 lib/usher/grapher.rb
usher-0.5.6 lib/usher/grapher.rb
usher-0.5.5 lib/usher/grapher.rb
usher-0.5.4 lib/usher/grapher.rb
usher-0.5.3 lib/usher/grapher.rb
usher-0.5.2 lib/usher/grapher.rb
usher-0.5.1 lib/usher/grapher.rb