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.
5
6
7
8
|
# File 'lib/bot/context.rb', line 5
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
10
11
12
13
14
15
16
|
# File 'lib/bot/context.rb', line 10
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.
3
4
5
|
# File 'lib/bot/context.rb', line 3
def redis_client
@redis_client
end
|
#user ⇒ Object
Returns the value of attribute user.
3
4
5
|
# File 'lib/bot/context.rb', line 3
def user
@user
end
|
Instance Method Details
#add(field, content) ⇒ Object
30
31
32
|
# File 'lib/bot/context.rb', line 30
def add(field, content)
$redis.hset context_key, field, content
end
|
#clear ⇒ Object
34
35
36
|
# File 'lib/bot/context.rb', line 34
def clear
$redis.del context_key
end
|
#context_key ⇒ Object
38
39
40
|
# File 'lib/bot/context.rb', line 38
def context_key
"bot:context:#{user.id}"
end
|
#get(field) ⇒ Object
18
19
20
21
22
23
|
# File 'lib/bot/context.rb', line 18
def get(field)
data = $redis.hget(context_key, field)
JSON.parse(data) if data.present?
rescue JSON::ParserError
data
end
|
#set(context) ⇒ Object
25
26
27
28
|
# File 'lib/bot/context.rb', line 25
def set(context)
self.clear
$redis.hmset(context_key, context.map { |k, v| [k, v] }.flatten)
end
|