Sha256: f923db133187fef7cb235baf5ad07c86a61ff3cac23f01aacf1072399dd46b53

Contents?: true

Size: 1.46 KB

Versions: 7

Compression:

Stored size: 1.46 KB

Contents

require 'redis'
require 'redis/namespace'

module Izanami

  # Proxy around a Redis client.
  # It handles the configuration and the namespaces.
  class Mapper
    attr_reader :namespace, :options

    # New Mapper client.
    #
    # @param [Hash] options
    # @option options [String] :namespace the default namespace for all the keys.
    # @option options [Hash] :redis Redis client options
    def initialize(options = {})
      @options   = options.dup
      @redis     = @options.delete(:redis)
      @namespace = build_namespace(@options.delete(:namespace))
    end

    # Generate the namespace based in a key.
    #
    # @param [String] key the default mapper key.
    #
    # @return [String] the namespace.
    def build_namespace(key)
      key || 'izanami'
    end
    protected :build_namespace

    # Redis client, without the namespace.
    #
    # @return [Redis::Client]
    def redis
      @redis ||= initialize_redis
    end

    # Redis client, with the namespace.
    #
    # @return [Redis::Namespace]
    def client
      @client ||= initialize_client
    end

    def initialize_redis
      Redis.new(@options)
    end
    protected :initialize_redis

    def initialize_client
      Redis::Namespace.new(@namespace, redis: redis)
    end
    protected :initialize_client

    # Inspect the mapper
    def to_s(*)
      name = self.class.name
      info = redis.inspect

      "<#{name} connected to #{info} with @namespace=\"#{@namespace}\">"
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
izanami-0.20.0 lib/izanami/mapper.rb
izanami-0.19.0 lib/izanami/mapper.rb
izanami-0.18.0 lib/izanami/mapper.rb
izanami-0.17.0 lib/izanami/mapper.rb
izanami-0.16.0 lib/izanami/mapper.rb
izanami-0.15.0 lib/izanami/mapper.rb
izanami-0.14.0 lib/izanami/mapper.rb