Class: Redis::Promise::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/redis/promise/resolver.rb

Overview

An object used to resolve or reject a redis promise.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis, id: SecureRandom.uuid_v4, namespace: 'global', key: "promise:#{namespace}:#{id}") ⇒ Resolver

: (Redis, ?id: String, ?namespace: String, ?key: String) -> void



16
17
18
19
# File 'lib/redis/promise/resolver.rb', line 16

def initialize(redis, id: SecureRandom.uuid_v4, namespace: 'global', key: "promise:#{namespace}:#{id}")
  @redis = redis.dup #: Redis
  @key = key
end

Instance Attribute Details

#keyObject (readonly)

: String



13
14
15
# File 'lib/redis/promise/resolver.rb', line 13

def key
  @key
end

#redisObject (readonly)

: Redis



10
11
12
# File 'lib/redis/promise/resolver.rb', line 10

def redis
  @redis
end

Instance Method Details

#reject(err, expire: nil) ⇒ Object

Reject the promise with the given value. The value gets serialized to JSON using ‘to_json`.

There is an optional argument ‘expire:` that determines the amount of seconds after which the key-value will get automatically deleted from redis.

: (top, ?expire: Integer?) -> void



40
41
42
43
# File 'lib/redis/promise/resolver.rb', line 40

def reject(err, expire: nil)
  serialized = { err: err }.to_json
  push(serialized, expire)
end

#resolve(value, expire: nil) ⇒ Object

Resolve the promise with the given value. The value gets serialized to JSON using ‘to_json`.

There is an optional argument ‘expire:` that determines the amount of seconds after which the key-value will get automatically deleted from redis.

: (top, ?expire: Integer?) -> void



28
29
30
31
# File 'lib/redis/promise/resolver.rb', line 28

def resolve(value, expire: nil)
  serialized = { value: value }.to_json
  push(serialized, expire)
end