ruboty-ruby_persistence 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ruboty/handlers/ruby_persistence.rb +27 -9
- data/lib/ruboty/ruby_persistence/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 14d35d6042c421a1d15051f95a3391052538fc6d
|
4
|
+
data.tar.gz: ce10f722debf3f1ed76765735b5f40b9bc3ac432
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61a455cb07e949ab6286b806cc276496aa3f88408ee02d39fac1715080e73d6815a6db92c06bf8d711506e2f545746ae36f2f243702d52170bd9e7fed1afc9bd
|
7
|
+
data.tar.gz: 8ef775aabf4f7f7aafab8a8c5ecaf6e499f249168fbcc1c1a45584bf8708de96faf712a085aa441102cf8fea346d5d0ae43b3af21010c7b69801967a4be5d8b9
|
@@ -17,15 +17,18 @@ module Ruboty
|
|
17
17
|
name: 'unset',
|
18
18
|
)
|
19
19
|
|
20
|
+
def initialize(*)
|
21
|
+
super
|
22
|
+
restore
|
23
|
+
end
|
24
|
+
|
20
25
|
def set(message)
|
21
26
|
name = message[:name]
|
22
27
|
|
23
|
-
if variables
|
24
|
-
set_and_persist(message)
|
25
|
-
elsif (method = predefined_method(name))
|
28
|
+
if !variables.key?(name) && (method = predefined_method(name))
|
26
29
|
message.reply("Error: `#{name}` is predefined by `#{method.owner}`")
|
27
30
|
else
|
28
|
-
|
31
|
+
define_and_persist_variable(message)
|
29
32
|
end
|
30
33
|
end
|
31
34
|
|
@@ -33,6 +36,7 @@ module Ruboty
|
|
33
36
|
name = message[:name]
|
34
37
|
|
35
38
|
if variables[name]
|
39
|
+
undefine_variable(name)
|
36
40
|
variables.delete(name)
|
37
41
|
message.reply("`#{name}` is now unset")
|
38
42
|
else
|
@@ -42,22 +46,36 @@ module Ruboty
|
|
42
46
|
|
43
47
|
private
|
44
48
|
|
49
|
+
def restore
|
50
|
+
variables.each do |name, value|
|
51
|
+
define_variable(name, value)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
45
55
|
def predefined_method(name)
|
46
56
|
context.method(name)
|
47
57
|
rescue NameError
|
48
58
|
nil
|
49
59
|
end
|
50
60
|
|
51
|
-
def
|
61
|
+
def define_and_persist_variable(message)
|
52
62
|
name = message[:name]
|
53
|
-
|
54
|
-
|
55
|
-
variables[name] =
|
56
|
-
message.reply("`#{name}` is now `#{
|
63
|
+
value = context.instance_eval(message[:code])
|
64
|
+
define_variable(name, value)
|
65
|
+
variables[name] = value
|
66
|
+
message.reply("`#{name}` is now `#{value.inspect}`")
|
57
67
|
rescue Exception => exception
|
58
68
|
message.reply("#{exception.class}: #{exception}")
|
59
69
|
end
|
60
70
|
|
71
|
+
def define_variable(name, value)
|
72
|
+
context.__send__(:define_method, name) { value }
|
73
|
+
end
|
74
|
+
|
75
|
+
def undefine_variable(name)
|
76
|
+
context.singleton_class.__send__(:undef_method, name)
|
77
|
+
end
|
78
|
+
|
61
79
|
def variables
|
62
80
|
robot.brain.data[NAMESPACE] ||= {}
|
63
81
|
end
|