Class: PageMagic::Matcher

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

Overview

models mapping used to relate pages to uris

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, parameters: nil, fragment: nil) ⇒ Matcher

Returns a new instance of Matcher.

Raises:



8
9
10
11
12
13
# File 'lib/page_magic/matcher.rb', line 8

def initialize(path = nil, parameters: nil, fragment: nil)
  fail MatcherInvalidException unless path || parameters || fragment
  @path = path
  @parameters = parameters
  @fragment = fragment
end

Instance Attribute Details

#fragmentObject (readonly)

Returns the value of attribute fragment.



5
6
7
# File 'lib/page_magic/matcher.rb', line 5

def fragment
  @fragment
end

#parametersObject (readonly)

Returns the value of attribute parameters.



5
6
7
# File 'lib/page_magic/matcher.rb', line 5

def parameters
  @parameters
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/page_magic/matcher.rb', line 5

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



43
44
45
46
47
# File 'lib/page_magic/matcher.rb', line 43

def <=>(other)
  [:path, :parameters, :fragment].inject(0) do |result, component|
    result == 0 ? compare(send(component), other.send(component)) : result
  end
end

#==(other) ⇒ Boolean Also known as: eql?

check equality

Parameters:

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/page_magic/matcher.rb', line 52

def ==(other)
  return false unless other.is_a?(Matcher)
  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



16
17
18
# File 'lib/page_magic/matcher.rb', line 16

def can_compute_uri?
  !fuzzy?(fragment) && !fuzzy?(path) && !fuzzy?(parameters)
end

#compute_uriString

Returns uri represented by this mapping.

Returns:

  • (String)

    uri represented by this mapping



21
22
23
24
25
26
# File 'lib/page_magic/matcher.rb', line 21

def compute_uri
  "#{path}".tap do |uri|
    uri << "?#{parameters.to_query}" if parameters
    uri << "##{fragment}" if fragment
  end
end

#hashFixnum

Returns hash for instance.

Returns:

  • (Fixnum)

    hash for instance



29
30
31
# File 'lib/page_magic/matcher.rb', line 29

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



35
36
37
38
# File 'lib/page_magic/matcher.rb', line 35

def match?(uri)
  uri = URI(uri)
  path_valid?(uri.path) && query_string_valid?(uri.query) && fragment_valid?(uri.fragment)
end