Class: PageMagic::Matcher
- Inherits:
-
Object
- Object
- PageMagic::Matcher
- Defined in:
- lib/page_magic/matcher.rb
Overview
models mapping used to relate pages to uris
Instance Attribute Summary collapse
-
#fragment ⇒ Object
readonly
Returns the value of attribute fragment.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#<=>(other) ⇒ Fixnum
compare this matcher against another.
-
#==(other) ⇒ Boolean
(also: #eql?)
check equality.
-
#can_compute_uri? ⇒ Boolean
True if no component contains a Regexp.
-
#compute_uri ⇒ String
Uri represented by this mapping.
-
#hash ⇒ Fixnum
Hash for instance.
-
#initialize(path = nil, parameters: nil, fragment: nil) ⇒ Matcher
constructor
A new instance of Matcher.
-
#match?(uri) ⇒ Boolean
Returns true if the uri is matched against this matcher.
Constructor Details
#initialize(path = nil, parameters: nil, fragment: nil) ⇒ Matcher
Returns a new instance of Matcher.
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
#fragment ⇒ Object (readonly)
Returns the value of attribute fragment.
5 6 7 |
# File 'lib/page_magic/matcher.rb', line 5 def fragment @fragment end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
5 6 7 |
# File 'lib/page_magic/matcher.rb', line 5 def parameters @parameters end |
#path ⇒ Object (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
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
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.
16 17 18 |
# File 'lib/page_magic/matcher.rb', line 16 def can_compute_uri? !fuzzy?(fragment) && !fuzzy?(path) && !fuzzy?(parameters) end |
#compute_uri ⇒ String
Returns 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 |
#hash ⇒ Fixnum
Returns 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
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 |