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:



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

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.



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

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:



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

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



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

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