Class: Redstruct::Factory

Inherits:
Object
  • Object
show all
Extended by:
Deserialization
Includes:
Creation, Utils::Inspectable
Defined in:
lib/redstruct/factory.rb,
lib/redstruct/factory/creation.rb,
lib/redstruct/factory/deserialization.rb

Overview

Main interface of the gem; this class should be used to build all Redstruct objects, even when deserializing them.

Defined Under Namespace

Modules: Creation, Deserialization

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Inspectable

#inspect, #to_s

Methods included from Creation

#counter, #factory, #hash, #list, #lock, #queue, #script, #set, #sorted_set, #string, #struct

Constructor Details

#initialize(connection: nil, pool: nil, namespace: nil) ⇒ Factory

Parameters:

  • connection (Redstruct::Connection) (defaults to: nil)

    connection to use for all objects built by the factory

  • pool (ConnectionPool) (defaults to: nil)

    pool to use to build a connection from if no connection param given

  • namespace (::String) (defaults to: nil)

    all objects build from the factory will have their keys namespaced under this one



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/redstruct/factory.rb', line 15

def initialize(connection: nil, pool: nil, namespace: nil)
  namespace ||= Redstruct.config.namespace

  if connection.nil?
    pool ||= Redstruct.config.connection_pool
    raise(Redstruct::Error, 'A connection pool is required to create a factory, but none was given') if pool.nil?
    connection = Redstruct::Connection.new(pool)
  end

  @connection = connection
  @namespace = namespace
  @script_cache = {}.tap { |hash| hash.extend(MonitorMixin) }
end

Instance Attribute Details

#connectionConnection (readonly)

Returns The connection proxy to use when executing commands. Shared by all factory produced objects.

Returns:

  • (Connection)

    The connection proxy to use when executing commands. Shared by all factory produced objects.



9
10
11
# File 'lib/redstruct/factory.rb', line 9

def connection
  @connection
end

Instance Method Details

#inspectable_attributesObject

Helper method for serialization



39
40
41
# File 'lib/redstruct/factory.rb', line 39

def inspectable_attributes
  return { namespace: @namespace, script_cache: @script_cache.keys }
end

#isolate(key) ⇒ String

Returns a namespaced version of the key (unless already namespaced)

Parameters:

  • key (String)

    the key to isolate/namespace

Returns:

  • (String)

    namespaced version of the key (or the key itself if already namespaced)



32
33
34
# File 'lib/redstruct/factory.rb', line 32

def isolate(key)
  return @namespace.nil? || key.start_with?(@namespace) ? key : "#{@namespace}:#{key}"
end