Class: Bot::Context
- Inherits:
-
Object
show all
- Defined in:
- lib/bot/context.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(user, redis_client) ⇒ Context
Returns a new instance of Context.
7
8
9
10
|
# File 'lib/bot/context.rb', line 7
def initialize(user, redis_client)
@redis_client = redis_client
@user = user
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
12
13
14
15
16
17
18
|
# File 'lib/bot/context.rb', line 12
def method_missing(m, *args, &block)
if m =~ /.+=/
self.add(m.to_s.gsub('=', ''), *args)
else
self.get(m)
end
end
|
Instance Attribute Details
#redis_client ⇒ Object
Returns the value of attribute redis_client.
5
6
7
|
# File 'lib/bot/context.rb', line 5
def redis_client
@redis_client
end
|
#user ⇒ Object
Returns the value of attribute user.
5
6
7
|
# File 'lib/bot/context.rb', line 5
def user
@user
end
|
Instance Method Details
#add(field, content) ⇒ Object
32
33
34
|
# File 'lib/bot/context.rb', line 32
def add(field, content)
redis_client.hset context_key, field, content
end
|
#clear ⇒ Object
36
37
38
|
# File 'lib/bot/context.rb', line 36
def clear
redis_client.del context_key
end
|
#context_key ⇒ Object
40
41
42
|
# File 'lib/bot/context.rb', line 40
def context_key
"bot:context:#{user.id}"
end
|
#get(field) ⇒ Object
20
21
22
23
24
25
|
# File 'lib/bot/context.rb', line 20
def get(field)
data = redis_client.hget(context_key, field)
JSON.parse(data) if data.present?
rescue JSON::ParserError
data
end
|
#set(context) ⇒ Object
27
28
29
30
|
# File 'lib/bot/context.rb', line 27
def set(context)
self.clear
redis_client.hmset(context_key, context.map { |k, v| [k, v] }.flatten)
end
|