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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: de217b9be23db369bc8ff9a5aae2ade6b54bba64
4
- data.tar.gz: 79a01481307d1a5648c8b9ac33a68616705fa664
3
+ metadata.gz: 14d35d6042c421a1d15051f95a3391052538fc6d
4
+ data.tar.gz: ce10f722debf3f1ed76765735b5f40b9bc3ac432
5
5
  SHA512:
6
- metadata.gz: c72db9e113af16f96e94877026f50e71e7dd3fc973fc0a8a1de861cd69ac2620478d04edaff8804be66e3583b52f9a011d48b4fe7854f3a464a4ff8564ce6213
7
- data.tar.gz: 0f5a2870e619ba67721b31647db8e574ceccc54ad72fa2dfae29fc9e8006def4363c6e4a3200e9607b7dcfb19b809227ddf71a0c26a7064437e848057a928e04
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[name]
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
- set_and_persist(message)
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 set_and_persist(message)
61
+ def define_and_persist_variable(message)
52
62
  name = message[:name]
53
- result = context.instance_eval(message[:code])
54
- context.__send__(:define_method, name) { result }
55
- variables[name] = result
56
- message.reply("`#{name}` is now `#{result.inspect}`")
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
@@ -1,5 +1,5 @@
1
1
  module Ruboty
2
2
  module RubyPersistence
3
- VERSION = "0.1.3"
3
+ VERSION = "0.1.4"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruboty-ruby_persistence
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuji Nakayama