Class: PageMagic::Mapping
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: {}, fragment: nil) ⇒ Mapping
constructor
A new instance of Mapping.
-
#match?(uri) ⇒ Boolean
Returns true if the uri is matched against this matcher.
Constructor Details
#initialize(path = nil, parameters: {}, fragment: nil) ⇒ Mapping
Returns a new instance of Mapping.
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
#fragment ⇒ Object (readonly)
Returns the value of attribute fragment.
9 10 11 |
# File 'lib/page_magic/mapping.rb', line 9 def fragment @fragment end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
9 10 11 |
# File 'lib/page_magic/mapping.rb', line 9 def parameters @parameters end |
#path ⇒ Object (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
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
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.
24 25 26 |
# File 'lib/page_magic/mapping.rb', line 24 def can_compute_uri? !fragment.fuzzy? && !path.fuzzy? && !parameters.fuzzy? end |
#compute_uri ⇒ String
Returns 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 |
#hash ⇒ Fixnum
Returns 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
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 |