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.
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
#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
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
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.
19 20 21 |
# File 'lib/page_magic/matcher.rb', line 19 def can_compute_uri? !fuzzy?(fragment) && !fuzzy?(path) && !fuzzy?(parameters) end |
#compute_uri ⇒ String
Returns 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 |
#hash ⇒ Fixnum
Returns 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
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 |