Class: PageMagic::Mapping

Inherits:
Object show all
Defined in:
lib/page_magic/mapping.rb

Overview

models mapping used to relate pages to uris

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, parameters: {}, fragment: nil) ⇒ Mapping

Returns a new instance of Mapping.

Parameters:

  • path (Object) (defaults to: nil)

    String or Regular expression to match with

  • parameters (Hash) (defaults to: {})

    mapping of parameter name to literal or regex to match with

  • fragment (Object) (defaults to: nil)

    String or Regular expression to match with

Raises:



15
16
17
18
19
20
21
# 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

Instance Attribute Details

#fragmentObject (readonly)

Returns the value of attribute fragment.



9
10
11
# File 'lib/page_magic/mapping.rb', line 9

def fragment
  @fragment
end

#parametersObject (readonly)

Returns the value of attribute parameters.



9
10
11
# File 'lib/page_magic/mapping.rb', line 9

def parameters
  @parameters
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/page_magic/mapping.rb', line 9

def path
  @path
end

Instance Method Details

#<=>(other) ⇒ Fixnum

compare this matcher against another

Parameters:

Returns:

  • (Fixnum)

    -1 = smaller, 0 = equal to, 1 = greater than



51
52
53
54
55
56
57
58
59
# 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) ⇒ Boolean Also known as: eql?

check equality

Parameters:

Returns:

  • (Boolean)


64
65
66
67
68
# 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

#can_compute_uri?Boolean

Returns true if no component contains a Regexp.

Returns:

  • (Boolean)

    true if no component contains a Regexp



24
25
26
# File 'lib/page_magic/mapping.rb', line 24

def can_compute_uri?
  !fragment.fuzzy? && !path.fuzzy? && !parameters.fuzzy?
end

#compute_uriString

Returns uri represented by this mapping.

Returns:

  • (String)

    uri represented by this mapping



29
30
31
32
33
34
# 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

#hashFixnum

Returns hash for instance.

Returns:

  • (Fixnum)

    hash for instance



37
38
39
# File 'lib/page_magic/mapping.rb', line 37

def hash
  [path, parameters, fragment].hash
end

#match?(uri) ⇒ Boolean

Returns true if the uri is matched against this matcher

Parameters:

  • uri (String)

Returns:

  • (Boolean)

    returns true if the uri is matched against this matcher



43
44
45
46
# 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