class Redstruct::Factory

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

Attributes

connection[R]

@return [Redstruct::ConnectionProxy] connection proxy used to execute commands

namespace[R]

@return [String] namespace used to prefix the keys of all objects created by this factory

Public Class Methods

new(connection: nil, namespace: nil) click to toggle source

@param [Redstruct::ConnectionProxy] connection connection to use for all objects built by the factory @param [String] namespace optional; all objects built from the factory will have their keys prefixed with this @raise [ArgumentError] raised if connection is not nil, and not a Redstruct::ConnectionProxy @return [Redstruct::Factory]

# 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

Public Instance Methods

delete(options = {}) click to toggle source

Deletes all keys created by the factory. By defaults will iterate at most of 500 million keys @param [Hash] options accepts the options as given in each @see Redstruct::Factory#each

# 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) click to toggle source

Returns a factory for a sub namespace. @example Given a factory `f` with namespace fact:first

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

@return [Factory] namespaced factory

# File lib/redstruct/factory.rb, line 72
def factory(namespace)
  return self.class.new(connection: @connection, namespace: prefix(namespace))
end
inspectable_attributes() click to toggle source

@!visibility private

# File lib/redstruct/factory.rb, line 120
def inspectable_attributes
  return { namespace: @namespace, connection: @connection }
end
lock(resource, **options) click to toggle source

Creates a lock for the given resource within this factory @see Redstruct::Lock#new @return [Redstruct::Lock] lock for the given resource within this factory

# File lib/redstruct/factory.rb, line 86
def lock(resource, **options)
  return Redstruct::Lock.new(resource, factory: self, **options)
end
prefix(key) click to toggle source

Returns a namespaced version of the key (unless already namespaced) @param [String] key the key to namespace @return [String] namespaced version of the key (or the key itself if already namespaced)

# 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) click to toggle source

Creates using this factory's connection @see Redstruct::Script#new @return [Redstruct::Script] script sharing the factory connection

# 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) click to toggle source

Use redis-rb scan_each method to iterate over particular keys @ @return [Enumerator] base enumerator to iterate of the namespaced keys

# File lib/redstruct/factory.rb, line 53
def to_enum(match: '*', count: 10)
  return @connection.scan_each(match: prefix(match), count: count)
end