class FMCache::IdKeyGen

Constants

DEFAULT_KEY_PREFIX

Public Class Methods

new(prefix) click to toggle source

@param [String, nil] prefix

# File lib/fmcache/id_key_gen.rb, line 6
def initialize(prefix)
  p = prefix || DEFAULT_KEY_PREFIX
  @prefix = "#{p}:"
end

Public Instance Methods

to_id(key) click to toggle source

@param [String] id @return [Integer]

# File lib/fmcache/id_key_gen.rb, line 31
def to_id(key)
  prefix_len = @prefix.size
  if key[0..(prefix_len-1)] == @prefix
    key[prefix_len..-1].to_i
  else
    raise "invalid key: #{key}"
  end
end
to_ids(keys) click to toggle source

@param [<String>] keys @return [<Integer>]

# File lib/fmcache/id_key_gen.rb, line 25
def to_ids(keys)
  keys.map { |key| to_id(key) }
end
to_key(id) click to toggle source

@param [Integer] id @return [String]

# File lib/fmcache/id_key_gen.rb, line 19
def to_key(id)
  "#{@prefix}#{id}"
end
to_keys(ids) click to toggle source

@param [<Integer>] ids @return [<String>]

# File lib/fmcache/id_key_gen.rb, line 13
def to_keys(ids)
  ids.map { |id| to_key(id) }
end