Class: Rack::Mount::Analysis::Frequency

Inherits:
Object
  • Object
show all
Extended by:
Mixover
Defined in:
lib/rack/mount/analysis/frequency.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods included from Mixover

include, new, new_with_module, new_without_module

Constructor Details

#initialize(*keys) ⇒ Frequency

Returns a new instance of Frequency.



8
9
10
11
# File 'lib/rack/mount/analysis/frequency.rb', line 8

def initialize(*keys)
  clear
  keys.each { |key| self << key }
end

Instance Method Details

#<<(key) ⇒ Object

Raises:

  • (ArgumentError)


19
20
21
22
23
# File 'lib/rack/mount/analysis/frequency.rb', line 19

def <<(key)
  raise ArgumentError unless key.is_a?(Hash)
  @raw_keys << key
  nil
end

#clearObject



13
14
15
16
17
# File 'lib/rack/mount/analysis/frequency.rb', line 13

def clear
  @raw_keys = []
  @key_frequency = Analysis::Histogram.new
  self
end

#possible_keysObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/rack/mount/analysis/frequency.rb', line 25

def possible_keys
  @possible_keys ||= begin
    @raw_keys.map do |key|
      key.inject({}) { |requirements, (method, requirement)|
        process_key(requirements, method, requirement)
        requirements
      }
    end
  end
end

#process_key(requirements, method, requirement) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rack/mount/analysis/frequency.rb', line 36

def process_key(requirements, method, requirement)
  if requirement.is_a?(Regexp)
    expression = Utils.parse_regexp(requirement)
    expression.reject! { |e| e.is_a?(Reginald::Anchor) }

    if expression.is_a?(Reginald::Expression) && expression.literal?
      return requirements[method] = expression.to_s
    end
  end

  requirements[method] = requirement
end

#reportObject



49
50
51
52
53
54
55
# File 'lib/rack/mount/analysis/frequency.rb', line 49

def report
  @report ||= begin
    possible_keys.each { |keys| keys.each_pair { |key, _| @key_frequency << key } }
    return [] if @key_frequency.count <= 1
    @key_frequency.select_upper
  end
end