Class: Bot::Context
- Inherits:
-
Object
show all
- Defined in:
- lib/bot/context.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(id, redis_client) ⇒ Context
Returns a new instance of Context.
7
8
9
10
|
# File 'lib/bot/context.rb', line 7
def initialize(id, redis_client)
@redis_client = redis_client
@id = id
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
#id ⇒ Object
Returns the value of attribute id.
5
6
7
|
# File 'lib/bot/context.rb', line 5
def id
@id
end
|
#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
|
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:#{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
|