Module: Documentrix::Documents::Cache::Common

Includes:
Utils::Math
Included in:
SQLiteCache, MemoryCache, RedisCache
Defined in:
lib/documentrix/documents/cache/common.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Math

#convert_to_vector, #cosine_similarity, #norm

Instance Attribute Details

#prefixObject

current prefix defined for the cache



12
13
14
# File 'lib/documentrix/documents/cache/common.rb', line 12

def prefix
  @prefix
end

Instance Method Details

#collections(prefix) ⇒ Array<Symbol>

Returns an array of collection names that match the given prefix.

Parameters:

  • prefix (String)

    a string to search for in collection names

Returns:

  • (Array<Symbol>)

    an array of matching collection names



18
19
20
21
22
23
24
25
# File 'lib/documentrix/documents/cache/common.rb', line 18

def collections(prefix)
  unique = Set.new
  full_each do |key, _|
    key =~ /\A#{prefix}(.+)-/ or next
    unique << $1
  end
  unique.map(&:to_sym)
end

#initialize(prefix:) ⇒ Object

The initialize method sets up the Documentrix::Documents::Cache instance's by setting its prefix attribute to the given value.

Parameters:

  • prefix (String)

    the string to be used as the prefix for this cache



8
9
10
# File 'lib/documentrix/documents/cache/common.rb', line 8

def initialize(prefix:)
  self.prefix = prefix
end

#pre(key) ⇒ String

Returns a string representing the given key prefixed with the defined prefix.

Parameters:

  • key (String)

    the key to join with the prefix

Returns:

  • (String)

    the joined string of prefix and key



32
33
34
# File 'lib/documentrix/documents/cache/common.rb', line 32

def pre(key)
  [ @prefix, key ].join
end

#unpre(key) ⇒ String

Returns a string with the prefix removed from the given key.

Parameters:

  • key (String)

    the input string containing the prefix.

Returns:

  • (String)

    the input string without the prefix.



40
41
42
# File 'lib/documentrix/documents/cache/common.rb', line 40

def unpre(key)
  key.sub(/\A#@prefix/, '')
end