Class: Mustermann::Identity
- Defined in:
- lib/mustermann/identity.rb
Overview
Matches strings that are identical to the pattern.
Constant Summary
Constants included from Mustermann
CompileError, DEFAULT_TYPE, Error, ExpandError, ParseError
Instance Attribute Summary
Attributes inherited from Pattern
Instance Method Summary collapse
-
#===(string) ⇒ Boolean
Whether or not the pattern matches the given string.
-
#expand(behavior = nil, values = {}) ⇒ String
Identity patterns support expanding.
-
#peek_size(string) ⇒ Integer?
The number of characters that match.
-
#to_templates ⇒ Array<String>
URI templates support generating templates (the logic is quite complex, though).
Methods inherited from Pattern
#+, #==, #=~, #eql?, #hash, #initialize, #match, #named_captures, #names, new, #params, #peek, #peek_match, #peek_params, supported?, supported_options, #to_proc, #to_s, #|
Methods included from Mustermann
Constructor Details
This class inherits a constructor from Mustermann::Pattern
Instance Method Details
#===(string) ⇒ Boolean
Returns Whether or not the pattern matches the given string.
21 22 23 |
# File 'lib/mustermann/identity.rb', line 21 def ===(string) unescape(string) == @string end |
#expand(behavior = nil, values = {}) ⇒ String
Identity patterns support expanding.
This implementation does not use Expander internally to save memory and compilation time.
68 69 70 71 72 73 74 75 |
# File 'lib/mustermann/identity.rb', line 68 def (behavior = nil, values = {}) return to_s if values.empty? or behavior == :ignore raise ExpandError, "cannot expand with keys %p" % values.keys.sort if behavior == :raise raise ArgumentError, "unknown behavior %p" % behavior if behavior != :append params = values.map { |key, value| @@uri.escape(key.to_s) + "=" + @@uri.escape(value.to_s, /[^\w]/) } separator = @string.include?(??) ? ?& : ?? @string + separator + params.join(?&) end |
#peek_size(string) ⇒ Integer?
Returns the number of characters that match.
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/mustermann/identity.rb', line 28 def peek_size(string) return unless unescape(string).start_with? @string return @string.size if string.start_with? @string # optimization @string.each_char.with_index.inject(0) do |count, (char, index)| char_size = 1 escaped = @@uri.escape(char, /./) char_size = escaped.size if string[index, escaped.size].downcase == escaped.downcase count + char_size end end |
#to_templates ⇒ Array<String>
URI templates support generating templates (the logic is quite complex, though).
45 46 47 |
# File 'lib/mustermann/identity.rb', line 45 def to_templates [@@uri.escape(to_s)] end |