Class: Redstruct::Factory
- Inherits:
-
Object
- Object
- Redstruct::Factory
- Includes:
- Utils::Inspectable, Utils::Iterable
- Defined in:
- lib/redstruct/factory.rb,
lib/redstruct/factory/object.rb
Overview
Main interface of the gem; this class should be used to build all Redstruct objects, even when deserializing them.
Defined Under Namespace
Classes: Object
Instance Attribute Summary collapse
-
#connection ⇒ Redstruct::ConnectionProxy
readonly
Connection proxy used to execute commands.
-
#namespace ⇒ String
readonly
Namespace used to prefix the keys of all objects created by this factory.
Factory methods collapse
-
#factory(namespace) ⇒ Factory
Returns a factory for a sub namespace.
-
#lock(resource, **options) ⇒ Redstruct::Lock
Creates a lock for the given resource within this factory.
-
#script(script, **options) ⇒ Redstruct::Script
Creates using this factory’s connection.
Instance Method Summary collapse
-
#delete(options = {}) ⇒ Object
Deletes all keys created by the factory.
- #initialize(connection: nil, namespace: nil) ⇒ Redstruct::Factory constructor
-
#inspectable_attributes ⇒ Object
# @!visibility private.
-
#prefix(key) ⇒ String
Returns a namespaced version of the key (unless already namespaced).
-
#to_enum(match: '*', count: 10) ⇒ Enumerator
Use redis-rb scan_each method to iterate over particular keys @.
Methods included from Utils::Iterable
Methods included from Utils::Inspectable
Constructor Details
#initialize(connection: nil, namespace: nil) ⇒ Redstruct::Factory
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
#connection ⇒ Redstruct::ConnectionProxy (readonly)
Returns connection proxy used to execute commands.
24 25 26 |
# File 'lib/redstruct/factory.rb', line 24 def connection @connection end |
#namespace ⇒ String (readonly)
Returns 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
60 61 62 63 64 |
# File 'lib/redstruct/factory.rb', line 60 def delete( = {}) return each({ match: '*', count: 500, max_iterations: 1_000_000, batch_size: 500 }.merge()) do |keys| @connection.del(*keys) end end |
#factory(namespace) ⇒ Factory
Returns a factory for a sub namespace.
72 73 74 |
# File 'lib/redstruct/factory.rb', line 72 def factory(namespace) return self.class.new(connection: @connection, namespace: prefix(namespace)) end |
#inspectable_attributes ⇒ Object
# @!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
86 87 88 |
# File 'lib/redstruct/factory.rb', line 86 def lock(resource, **) return Redstruct::Lock.new(resource, factory: self, **) end |
#prefix(key) ⇒ String
Returns a namespaced version of the key (unless 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
79 80 81 |
# File 'lib/redstruct/factory.rb', line 79 def script(script, **) return Redstruct::Script.new(script: script, connection: @connection, **) end |
#to_enum(match: '*', count: 10) ⇒ Enumerator
Use redis-rb scan_each method to iterate over particular 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 |