Class: Rack::Mount::Analysis::Splitting

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/mount/analysis/splitting.rb

Defined Under Namespace

Classes: Key

Constant Summary collapse

NULL =
"\0"

Instance Method Summary collapse

Constructor Details

#initialize(*keys) ⇒ Splitting

Returns a new instance of Splitting.



29
30
31
32
# File 'lib/rack/mount/analysis/splitting.rb', line 29

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

Instance Method Details

#<<(key) ⇒ Object

Raises:

  • (ArgumentError)


40
41
42
43
44
# File 'lib/rack/mount/analysis/splitting.rb', line 40

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

#clearObject



34
35
36
37
38
# File 'lib/rack/mount/analysis/splitting.rb', line 34

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

#expire!Object



65
66
67
# File 'lib/rack/mount/analysis/splitting.rb', line 65

def expire!
  @possible_keys = @report = nil
end

#possible_keysObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/rack/mount/analysis/splitting.rb', line 46

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rack/mount/analysis/splitting.rb', line 69

def process_key(requirements, method, requirement)
  separators = separators(method)
  if requirement.is_a?(Regexp) && separators.any?
    generate_split_keys(requirement, separators).each_with_index do |value, index|
      requirements[Key.new(method, index, Regexp.union(*separators))] = value
    end
  else
    if requirement.is_a?(Regexp)
      expression = Utils.parse_regexp(requirement)

      if expression.is_a?(Regin::Expression) && expression.anchored_to_line?
        expression = Regin::Expression.new(expression.reject { |e| e.is_a?(Regin::Anchor) })
        return requirements[method] = expression.to_s if expression.literal?
      end
    end

    requirements[method] = requirement
  end
end

#reportObject



57
58
59
60
61
62
63
# File 'lib/rack/mount/analysis/splitting.rb', line 57

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