Class: Redstruct::Factory

Inherits:
Object
  • Object
show all
Includes:
Utils::Inspectable, Utils::Iterable
Defined in:
lib/redstruct/factory.rb

Overview

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

Instance Attribute Summary collapse

Factory methods collapse

Instance Method Summary collapse

Constructor Details

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

Parameters:

  • connection (Redstruct::ConnectionProxy)

    connection to use for all objects built by the factory

  • namespace (String)

    optional; all objects built from the factory will have their keys prefixed with this

Raises:

  • (ArgumentError)

    raised if connection is not nil, and not a Redstruct::ConnectionProxy



30
31
32
33
34
35
36
37
38
# File 'lib/redstruct/factory.rb', line 30

def initialize(connection: nil, namespace: nil)
  namespace ||= Redstruct.config.default_namespace
  connection ||= Redstruct::ConnectionProxy.new(Redstruct.config.default_connection)

  raise ArgumentError, 'connection should be a Redstruct::ConnectionProxy' unless connection.is_a?(Redstruct::ConnectionProxy)

  @connection = connection
  @namespace = namespace.to_s
end

Instance Attribute Details

#connectionRedstruct::ConnectionProxy (readonly)

Returns connection proxy used to execute commands

Returns:



24
25
26
# File 'lib/redstruct/factory.rb', line 24

def connection
  @connection
end

#namespaceString (readonly)

Returns namespace used to prefix the keys of all objects created by this factory

Returns:

  • (String)

    namespace used to prefix the keys of all objects created by this factory



21
22
23
# File 'lib/redstruct/factory.rb', line 21

def namespace
  @namespace
end

Instance Method Details

#delete(options = {}) ⇒ Object

Deletes all keys created by the factory. By defaults will iterate at most of 500 million keys

Parameters:

  • options (Hash) (defaults to: {})

    accepts the options as given in each

See Also:

  • #each


60
61
62
63
64
# File 'lib/redstruct/factory.rb', line 60

def delete(options = {})
  return each({ match: '*', count: 500, max_iterations: 1_000_000, batch_size: 500 }.merge(options)) do |keys|
    @connection.del(*keys)
  end
end

#factory(namespace) ⇒ Factory

Returns a factory for a sub namespace.

Examples:

Given a factory `f` with namespace fact:first

f.factory('second') # => Redstruct::Factory: namespace: <"fact:first:second">>

Returns:



72
73
74
# File 'lib/redstruct/factory.rb', line 72

def factory(namespace)
  return self.class.new(connection: @connection, namespace: prefix(namespace))
end

#inspectable_attributesObject

# @!visibility private



120
121
122
# File 'lib/redstruct/factory.rb', line 120

def inspectable_attributes
  return { namespace: @namespace, connection: @connection }
end

#lock(resource, **options) ⇒ Redstruct::Lock

Creates a lock for the given resource within this factory

Returns:

See Also:

  • Lock#new


86
87
88
# File 'lib/redstruct/factory.rb', line 86

def lock(resource, **options)
  return Redstruct::Lock.new(resource, factory: self, **options)
end

#prefix(key) ⇒ String

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

Parameters:

  • key (String)

    the key to namespace

Returns:

  • (String)

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



43
44
45
46
47
48
# File 'lib/redstruct/factory.rb', line 43

def prefix(key)
  prefixed = key
  prefixed = "#{@namespace}:#{key}" unless @namespace.empty? || key.start_with?("#{@namespace}:")

  return prefixed
end

#script(script, **options) ⇒ Redstruct::Script

Creates using this factory's connection

Returns:

See Also:

  • Script#new


79
80
81
# File 'lib/redstruct/factory.rb', line 79

def script(script, **options)
  return Redstruct::Script.new(script: script, connection: @connection, **options)
end

#to_enum(match: '*', count: 10) ⇒ Enumerator

Use redis-rb scan_each method to iterate over particular keys @

Returns:

  • (Enumerator)

    base enumerator to iterate of the namespaced keys



53
54
55
# File 'lib/redstruct/factory.rb', line 53

def to_enum(match: '*', count: 10)
  return @connection.scan_each(match: prefix(match), count: count)
end