Class: EurekaBot::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/eureka_bot/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, redis: nil) ⇒ Session

Returns a new instance of Session.



5
6
7
8
# File 'lib/eureka_bot/session.rb', line 5

def initialize(key, redis: nil)
  @key   = key
  @redis = redis
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



3
4
5
# File 'lib/eureka_bot/session.rb', line 3

def key
  @key
end

#redisObject (readonly)

Returns the value of attribute redis.



3
4
5
# File 'lib/eureka_bot/session.rb', line 3

def redis
  @redis
end

Instance Method Details

#dataObject



14
15
16
# File 'lib/eureka_bot/session.rb', line 14

def data
  @data ||= get || {}
end

#data=(v) ⇒ Object



18
19
20
21
# File 'lib/eureka_bot/session.rb', line 18

def data=(v)
  @data = v
  set
end

#delObject



32
33
34
# File 'lib/eureka_bot/session.rb', line 32

def del
  redis.del(key)
end

#getObject



23
24
25
26
# File 'lib/eureka_bot/session.rb', line 23

def get
  got = redis.get(key)
  got.present? ? ActiveSupport::JSON.decode(got).deep_symbolize_keys : nil
end

#setObject



28
29
30
# File 'lib/eureka_bot/session.rb', line 28

def set
  redis.set(key, data.to_json) == 'OK'
end

#update(**attributes) ⇒ Object



10
11
12
# File 'lib/eureka_bot/session.rb', line 10

def update(**attributes)
  self.data = data.merge(attributes.deep_symbolize_keys).compact
end