Class: Mustermann::Identity
- Defined in:
- lib/mustermann/identity.rb
Overview
Matches strings that are identical to the pattern.
Constant Summary
Constants inherited from Pattern
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.
18 19 20 |
# File 'lib/mustermann/identity.rb', line 18 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.
65 66 67 68 69 70 71 72 73 |
# File 'lib/mustermann/identity.rb', line 65 def (behavior = nil, values = {}) values, behavior = behavior, nil if behavior.kind_of?(Hash) 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.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/mustermann/identity.rb', line 25 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).
42 43 44 |
# File 'lib/mustermann/identity.rb', line 42 def to_templates [@@uri.escape(to_s)] end |