Class: Mustermann::Identity
- Defined in:
- lib/mustermann/identity.rb
Overview
Matches strings that are identical to the pattern.
Constant Summary
Constants included from Mustermann
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
#=~, #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.
19 20 21 |
# File 'lib/mustermann/identity.rb', line 19 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.
66 67 68 69 70 71 72 73 |
# File 'lib/mustermann/identity.rb', line 66 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.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/mustermann/identity.rb', line 26 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).
43 44 45 |
# File 'lib/mustermann/identity.rb', line 43 def to_templates [@@uri.escape(to_s)] end |