Class: SimpleRedisOrm::Entry

Inherits:
Dry::Struct
  • Object
show all
Extended by:
Helpers::CoreCommands::ClassMethods
Includes:
Helpers::CoreCommands
Defined in:
lib/simple-redis-orm/entry.rb

Direct Known Subclasses

ApplicationEntry

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::CoreCommands::ClassMethods

read_by, read_hash, read_value

Methods included from Helpers::Serializer

#deserialize_hash_value, #deserialize_value, #serialize_value

Methods included from Helpers::CoreCommands

#exists, #exists?, #expire, #read, #read_hash, #read_subhash, #read_value, #set, #set_hash, #set_subhash, #ttl

Class Method Details

.create(id:, **attributes) ⇒ Object



20
21
22
# File 'lib/simple-redis-orm/entry.rb', line 20

def create(id:, **attributes)
  new(id: id, **attributes).save
end

.find(id) ⇒ Object



24
25
26
27
28
29
# File 'lib/simple-redis-orm/entry.rb', line 24

def find(id)
  value = read_by(id)
  return if value.nil?

  new(id: id, **value)
end

.new(id:, **attributes) ⇒ Object



15
16
17
18
# File 'lib/simple-redis-orm/entry.rb', line 15

def new(id:, **attributes)
  attrs_with_defaults = fill_in_missing_keys(**attributes)
  super(key: id, **attrs_with_defaults)
end

.redisObject



31
32
33
# File 'lib/simple-redis-orm/entry.rb', line 31

def redis
  @redis || raise(NotConnected, "#{name}.redis not set to a Redis.new connection pool")
end

.redis=(conn) ⇒ Object



35
36
37
# File 'lib/simple-redis-orm/entry.rb', line 35

def redis=(conn)
  @redis = ConnectionPoolProxy.proxy_if_needed(conn)
end

.without_redis_key_prefix(id) ⇒ Object



39
40
41
42
43
# File 'lib/simple-redis-orm/entry.rb', line 39

def without_redis_key_prefix(id)
  return if id.nil?

  id.gsub(/^(.*):/, '')
end

Instance Method Details

#attributesObject



57
58
59
# File 'lib/simple-redis-orm/entry.rb', line 57

def attributes
  super.except(:key)
end

#idObject



10
11
12
# File 'lib/simple-redis-orm/entry.rb', line 10

def id
  self.class.without_redis_key_prefix(key)
end

#redisObject



61
62
63
# File 'lib/simple-redis-orm/entry.rb', line 61

def redis
  self.class.redis
end

#saveObject



65
66
67
68
69
# File 'lib/simple-redis-orm/entry.rb', line 65

def save
  set_hash(attributes.except(:key))

  self
end