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.

Parameters:

  • path (Object) (defaults to: nil)

    String or Regular expression to match with

  • parameters (Hash) (defaults to: nil)

    mapping of parameter name to literal or regex to match with

  • fragment (Object) (defaults to: nil)

    String or Regular expression to match with

Raises:



11
12
13
14
15
16
# File 'lib/page_magic/matcher.rb', line 11

def initialize(path = nil, parameters: nil, fragment: nil)
  raise 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



46
47
48
49
50
51
# File 'lib/page_magic/matcher.rb', line 46

def <=>(other)
  results = [:path, :parameters, :fragment].collect do |component|
    compare(send(component), other.send(component))
  end
  results.find { |result| !result.zero? } || 0
end

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

check equality

Parameters:

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/page_magic/matcher.rb', line 56

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



19
20
21
# File 'lib/page_magic/matcher.rb', line 19

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



24
25
26
27
28
29
# File 'lib/page_magic/matcher.rb', line 24

def compute_uri
  path.to_s.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



32
33
34
# File 'lib/page_magic/matcher.rb', line 32

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



38
39
40
41
# File 'lib/page_magic/matcher.rb', line 38

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