Class: PageMagic::Comparator::ParameterMap

Inherits:
PageMagic::Comparator show all
Defined in:
lib/page_magic/comparator/parameter_map.rb

Overview

class Map - used to model parameter matching requirements

Instance Attribute Summary

Attributes inherited from PageMagic::Comparator

#comparator, #fuzzy

Instance Method Summary collapse

Methods inherited from PageMagic::Comparator

#==, for, #fuzzy?, #to_s

Constructor Details

#initialize(map) ⇒ ParameterMap

Returns a new instance of ParameterMap.



7
8
9
10
11
12
13
14
# File 'lib/page_magic/comparator/parameter_map.rb', line 7

def initialize(map)
  comparator = normalise(map).keys.each_with_object({}) do |key, params|
    params[key] = Comparator.for(map[key])
  end

  fuzzy = comparator.values.any?(&:fuzzy?)
  super(comparator, fuzzy)
end

Instance Method Details

#<=>(other) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/page_magic/comparator/parameter_map.rb', line 16

def <=>(other)
  return 0 if empty? && other.empty?
  return 1 if other.empty?
  if (comparator.keys.size <=> other.comparator.keys.size).zero?
    return literal_matchers.size <=> other.literal_matchers.size
  end

  0
end

#empty?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/page_magic/comparator/parameter_map.rb', line 26

def empty?
  comparator.empty?
end

#literal_matchersObject



30
31
32
# File 'lib/page_magic/comparator/parameter_map.rb', line 30

def literal_matchers
  comparator.values.find_all { |matcher| !matcher.fuzzy? }
end

#match?(params) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
# File 'lib/page_magic/comparator/parameter_map.rb', line 34

def match?(params)
  params_copy = normalise(params)
  comparator.each do |key, value|
    param = params_copy[key]
    return false unless value&.match?(param)
  end
  true
end