class PageMagic::Mapping

models mapping used to relate pages to uris

Attributes

fragment[R]
parameters[R]
path[R]

Public Class Methods

new(path = nil, parameters: {}, fragment: nil) click to toggle source

@param [Object] path String or Regular expression to match with @param [Hash] parameters mapping of parameter name to literal or regex to match with @param [Object] fragment String or Regular expression to match with @raise [MatcherInvalidException] if at least one component is not specified

# File lib/page_magic/mapping.rb, line 15
def initialize(path = nil, parameters: {}, fragment: nil)
  raise MatcherInvalidException unless path || parameters || fragment

  @path = Comparator.for(path)
  @parameters = Comparator.for(parameters)
  @fragment = Comparator.for(fragment)
end

Public Instance Methods

<=>(other) click to toggle source

compare this matcher against another @param [Mapping] other @return [Fixnum] -1 = smaller, 0 = equal to, 1 = greater than

# File lib/page_magic/mapping.rb, line 51
def <=>(other)
  path_comparison = path <=> other.path
  return path_comparison unless path_comparison.zero?

  parameter_comparison = parameters <=> other.parameters
  return parameter_comparison unless parameter_comparison.zero?

  fragment <=> other.fragment
end
==(other) click to toggle source

check equality @param [Mapping] other @return [Boolean]

# File lib/page_magic/mapping.rb, line 64
def ==(other)
  return false unless other.is_a?(Mapping)

  path == other.path && parameters == other.parameters && fragment == other.fragment
end
Also aliased as: eql?
can_compute_uri?() click to toggle source

@return [Boolean] true if no component contains a Regexp

# File lib/page_magic/mapping.rb, line 24
def can_compute_uri?
  !fragment.fuzzy? && !path.fuzzy? && !parameters.fuzzy?
end
compute_uri() click to toggle source

@return [String] uri represented by this mapping

# File lib/page_magic/mapping.rb, line 29
def compute_uri
  path.to_s.dup.tap do |uri|
    uri << "?#{parameters.comparator.to_query}" unless parameters.empty?
    uri << "##{fragment}" if fragment.present?
  end
end
eql?(other)
Alias for: ==
hash() click to toggle source

@return [Fixnum] hash for instance

# File lib/page_magic/mapping.rb, line 37
def hash
  [path, parameters, fragment].hash
end
match?(uri) click to toggle source

@param [String] uri @return [Boolean] returns true if the uri is matched against this matcher

# File lib/page_magic/mapping.rb, line 43
def match?(uri)
  uri = URI(uri)
  path.match?(uri.path) && parameters.match?(parameters_hash(uri.query)) && fragment.match?(uri.fragment)
end

Private Instance Methods

parameters_hash(string) click to toggle source
# File lib/page_magic/mapping.rb, line 74
def parameters_hash(string)
  CGI.parse(string.to_s.downcase).collect { |key, value| [key.downcase, value.first] }.to_h
end