Class: Mustermann::Identity

Inherits:
Pattern
  • Object
show all
Defined in:
lib/mustermann/identity.rb

Overview

Matches strings that are identical to the pattern.

Examples:

Mustermann.new('/:foo', type: :identity) === '/bar' # => false

See Also:

Constant Summary

Constants inherited from Pattern

Pattern::PATTERN_METHODS

Constants included from Mustermann

DEFAULT_TYPE, VERSION

Instance Method Summary collapse

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

[], new, register

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.

Parameters:

  • string (String)

    The string to match against

Returns:

  • (Boolean)

    Whether or not the pattern matches the given string

See Also:


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.

Examples:

Expanding a pattern

pattern = Mustermann.new('/:name(.:ext)?')
pattern.expand(name: 'hello')             # => "/hello"
pattern.expand(name: 'hello', ext: 'png') # => "/hello.png"

Checking if a pattern supports expanding

if pattern.respond_to? :expand
  pattern.expand(name: "foo")
else
  warn "does not support expanding"
end

Parameters:

  • behavior (Symbol) (defaults to: nil)

    What to do with additional key/value pairs not present in the values hash. Possible options: :raise, :ignore, :append.

  • values (Hash{Symbol: #to_s, Array<#to_s>}) (defaults to: {})

    Values to use for expansion.

Returns:

  • (String)

    expanded string

Raises:

  • (NotImplementedError)

    raised if expand is not supported.

  • (Mustermann::ExpandError)

    raised if a value is missing or unknown

See Also:


65
66
67
68
69
70
71
72
73
# File 'lib/mustermann/identity.rb', line 65

def expand(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.

Parameters:

  • string (String)

    The string to match against

Returns:

  • (Integer, nil)

    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_templatesArray<String>

URI templates support generating templates (the logic is quite complex, though).

Examples:

generating templates

Mustermann.new("/:name").to_templates                   # => ["/{name}"]
Mustermann.new("/:foo(@:bar)?/*baz").to_templates       # => ["/{foo}@{bar}/{+baz}", "/{foo}/{+baz}"]
Mustermann.new("/{name}", type: :template).to_templates # => ["/{name}"]

generating templates from composite patterns

pattern  = Mustermann.new('/:name')
pattern |= Mustermann.new('/{name}', type: :template)
pattern |= Mustermann.new('/example/*nested')
pattern.to_templates # => ["/{name}", "/example/{+nested}"]

Checking if a pattern supports expanding

if pattern.respond_to? :to_templates
  pattern.to_templates
else
  warn "does not support template generation"
end

Returns:

  • (Array<String>)

    list of URI templates

See Also:


42
43
44
# File 'lib/mustermann/identity.rb', line 42

def to_templates
  [@@uri.escape(to_s)]
end