Class: Mustermann::PatternCache Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Note:

Mustermann::Pattern.new (which is used by new) will reuse instances that have not yet been garbage collected. You only need an extra cache if you do not keep a reference to the patterns around.

A simple, persistent cache for creating repositories.

Examples:

require 'mustermann/pattern_cache'
cache = Mustermann::PatternCache.new

# use this instead of Mustermann.new
pattern = cache.create_pattern("/:name", type: :rails)

Instance Method Summary collapse

Constructor Details

#initialize(**pattern_options) ⇒ PatternCache

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of PatternCache.

Parameters:



24
25
26
27
28
# File 'lib/mustermann/pattern_cache.rb', line 24

def initialize(**pattern_options)
  @cached          = Set.new
  @mutex           = Mutex.new
  @pattern_options = pattern_options
end

Instance Method Details

#clearObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Removes all pattern instances from the cache.



41
42
43
# File 'lib/mustermann/pattern_cache.rb', line 41

def clear
  @mutex.synchronize { @cached.clear }
end

#create_pattern(string, **pattern_options) ⇒ Mustermann::Pattern

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns pattern corresponding to string.

Parameters:

  • input (String, Pattern, Regexp, Symbol, #to_pattern, Array<String, Pattern, Regexp, Symbol, #to_pattern>)

    The representation of the pattern

  • options (Hash)

    The options hash

Returns:

Raises:

  • (TypeError)

    if the passed object cannot be converted to a pattern

  • (ArgumentError)

    if the type is not supported

  • (ArgumentError)

    if some option is not supported

  • (Mustermann::Error)

    if the pattern can’t be generated from the string

See Also:



34
35
36
37
38
# File 'lib/mustermann/pattern_cache.rb', line 34

def create_pattern(string, **pattern_options)
  pattern = Mustermann.new(string, **pattern_options, **@pattern_options)
  @mutex.synchronize { @cached.add(pattern) } unless @cached.include? pattern
  pattern
end

#sizeInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns number of currently cached patterns.

Returns:

  • (Integer)

    number of currently cached patterns



46
47
48
# File 'lib/mustermann/pattern_cache.rb', line 46

def size
  @mutex.synchronize { @cached.size }
end