ruboty-variable 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8adb51f3704dc280a600d1bb32f00fbfe31baacd
4
- data.tar.gz: 052e82ab6d3d6520a36243806d0928ea86dc7c99
3
+ metadata.gz: e0fff3ceeaa8addea381f98b4f36d85a681205d4
4
+ data.tar.gz: e921ed243f4c2d2593f88719215b32dfcf6116de
5
5
  SHA512:
6
- metadata.gz: 87d0ae994f66e1435f76da36e0ec8c805838e31a061989bf6b19d47b782b9ca97a04e6dc2bdf83fcf0396bbcde79f1bf39a67ada888c299956251786b965ebcd
7
- data.tar.gz: ff8d78d6ffb73d60b24be87ff963c8311a7b369db8dff2b6a94645e19d60989c468f5eebe83d5aaf091bdd39271f0f3f4dca1e3da1f4a57d05117f4468a0fee9
6
+ metadata.gz: d04da308acd3c10628dca0bd70c92663a148528410b05a74f009ee4d78f94101bb1a02b57853bb07bcded1c08debc6208386eeb32b4c2954c3f596e7af11c10b
7
+ data.tar.gz: 155c1f7d1606b2b3c41ddc6423d6971965d597158e4b7301144c3054dc0f9cb7cdcc7d9b6af563941bdc8f5791c93a5ff7b62229a6a1998235c69a89139aeaed
data/README.md CHANGED
@@ -1,56 +1,139 @@
1
- # Ruboty::Variable
2
-
3
- An Ruboty Handler + Actions to ruboty-variable.
4
-
5
- [Ruboty](https://github.com/r7kamura/ruboty) is Chat bot framework. Ruby + Bot = Ruboty
6
-
7
- ## Installation
8
-
9
- Add this line to your application's Gemfile:
10
-
11
- ```ruby
12
- gem 'ruboty-variable'
13
- ```
14
-
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- ## Commands
20
-
21
- |Command|Pattern|Description|
22
- |:--|:--|:--|
23
- |[set_value](#usage)|/var (?\<key\>\\S+?) (?\<value\>\\S+?)z/|Set value|
24
- |[get_value](#usage)|/var (?\<key\>\\S+?)z/|Get value|
25
-
26
- ## Usage
27
- * Set value and get value
28
-
29
- ~~~
30
- > ruboty var hoge 3
31
- Set 3 to hoge
32
- > ruboty var hoge
33
- 3
34
- > ruboty var piyo
35
- Undefined piyo
36
- ~~~
37
-
38
- ## ENV
39
-
40
- |Name|Description|
41
- |:--|:--|
42
- |||
43
-
44
- ## Dependency
45
-
46
- |Name|Description|
47
- |:--|:--|
48
- |||
49
-
50
- ## Contributing
51
-
52
- 1. Fork it ( https://github.com/snakazawa/ruboty-variable/fork )
53
- 2. Create your feature branch (`git checkout -b my-new-feature`)
54
- 3. Commit your changes (`git commit -am 'Add some feature'`)
55
- 4. Push to the branch (`git push origin my-new-feature`)
56
- 5. Create a new Pull Request
1
+ # Ruboty::Variable
2
+
3
+ An Ruboty Handler + Actions to ruboty-variable.
4
+
5
+ [Ruboty](https://github.com/r7kamura/ruboty) is Chat bot framework. Ruby + Bot = Ruboty
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ruboty-variable'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ ## Commands
20
+
21
+ |Command|Pattern|Description|
22
+ |:--|:--|:--|
23
+ |[set_value](#set_value)|/var set (?\<key\>\\S+?) (?\<value\>\\S+?)\z/|Set value|
24
+ |[get_value](#get_value)|/var get (?\<key\>\\S+?)\z/|Get value|
25
+ |[delete_value](#delete_value)|/var delete (?\<key\>\\S+?)\z/|Delete value|
26
+ |[list_values](#list_values)|/var list\z/|Show values list|
27
+ |[array_init](#array_init)|/var array init (?\<key\>\\S+?)\z/|Create empty array or clear array|
28
+ |[array_push](#array_push)|/var array push (?\<key\>\\S+?) (?\<values\>.+?)\z/|Push values to array|
29
+ |[array_remove](#array_remove)|/var array remove (?\<key\>\\S+?) (?\<values\>.+?)\z/|Remove values from array|
30
+
31
+ ## Usage
32
+ ### set_value
33
+ * Set value
34
+
35
+ ~~~
36
+ > ruboty var set hoge 3
37
+ Set 3 to hoge
38
+ ~~~
39
+
40
+ ### get_value
41
+ * Get value
42
+
43
+ ~~~
44
+ > ruboty var set hoge 3
45
+ Set 3 to hoge
46
+ > ruboty var get hoge
47
+ 3
48
+ ~~~
49
+
50
+ ### delete_value
51
+ * Delete value
52
+
53
+ ~~~
54
+ > ruboty var set hoge 3
55
+ Set 3 to hoge
56
+ > ruboty var delete hoge
57
+ Deleted hoge
58
+ ~~~
59
+
60
+ ### list_values
61
+ * Show values list
62
+
63
+ ~~~
64
+ > ruboty var set hoge 3
65
+ Set 3 to hoge
66
+ > ruboty var set piyo piyo_value
67
+ Set piyo_value to piyo
68
+ > ruboty var list
69
+ key, type value
70
+ hoge - string - 3
71
+ piyo - string - piyo_value
72
+ ~~~
73
+
74
+ ### array_init
75
+ * Create empty array or clear array
76
+
77
+ ~~~
78
+ > ruboty var array init ary
79
+ Created ary empty array
80
+ > ruboty var list
81
+ key, type value
82
+ ary - array - []
83
+ ~~~
84
+
85
+ ### array_push
86
+ * Push values to array
87
+
88
+ ~~~
89
+ > ruboty var array init ary
90
+ Created ary empty array
91
+ > ruboty var array push ary one
92
+ Push one to ary
93
+ > ruboty var array push ary two three
94
+ Push two to ary
95
+ Push three to ary
96
+ > ruboty var list
97
+ key, type value
98
+ ary - array - ["one", "two", "three"]
99
+ ~~~
100
+
101
+ ### array_remove
102
+ * Remove values from array
103
+
104
+ ~~~
105
+ > ruboty var array init ary
106
+ Created ary empty array
107
+ > ruboty var array push one two three
108
+ Undefined one
109
+ > ruboty var array push ary one two three
110
+ Push one to ary
111
+ Push two to ary
112
+ Push three to ary
113
+ > ruboty var array remove ary one two
114
+ ary already included one
115
+ ary already included two
116
+ > ruboty var list
117
+ key, type value
118
+ ary - array - ["one", "two", "three"]
119
+ ~~~
120
+
121
+ ## ENV
122
+
123
+ |Name|Description|
124
+ |:--|:--|
125
+ |||
126
+
127
+ ## Dependency
128
+
129
+ |Name|Description|
130
+ |:--|:--|
131
+ |||
132
+
133
+ ## Contributing
134
+
135
+ 1. Fork it ( https://github.com/snakazawa/ruboty-variable/fork )
136
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
137
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
138
+ 4. Push to the branch (`git push origin my-new-feature`)
139
+ 5. Create a new Pull Request
@@ -3,15 +3,40 @@ require "ruboty/variable/actions/variable"
3
3
  module Ruboty
4
4
  module Handlers
5
5
  class Variable < Base
6
- on /var (?<key>\S+?) (?<value>\S+?)\z/, name: 'set_value', description: 'Set value'
7
- on /var (?<key>\S+?)\z/, name: 'get_value', description: 'Get value'
6
+ on /var set (?<key>\S+?) (?<value>\S+?)\z/, name: 'set_value', description: 'Set value'
7
+ on /var get (?<key>\S+?)\z/, name: 'get_value', description: 'Get value'
8
+ on /var delete (?<key>\S+?)\z/, name: 'delete_value', description: 'Delete value'
9
+ on /var list\z/, name: 'list_values', description: 'Show values list'
10
+ on /var array init (?<key>\S+?)\z/, name: 'array_init', description: 'Create empty array or clear array'
11
+ on /var array push (?<key>\S+?) (?<values>.+?)\z/, name: 'array_push', description: 'Push values to array'
12
+ on /var array remove (?<key>\S+?) (?<values>.+?)\z/, name: 'array_remove', description: 'Remove values from array'
8
13
 
9
14
  def get_value(message)
10
- Ruboty::Variable::Actions::Variable.new(message).get_value(message[:key])
15
+ Ruboty::Variable::Actions::Variable.new(message).get(message[:key])
11
16
  end
12
17
 
13
18
  def set_value(message)
14
- Ruboty::Variable::Actions::Variable.new(message).set_value(message[:key], message[:value])
19
+ Ruboty::Variable::Actions::Variable.new(message).set(message[:key], message[:value])
20
+ end
21
+
22
+ def delete_value(message)
23
+ Ruboty::Variable::Actions::Variable.new(message).delete(message[:key])
24
+ end
25
+
26
+ def list_values(message)
27
+ Ruboty::Variable::Actions::Variable.new(message).list()
28
+ end
29
+
30
+ def array_init(message)
31
+ Ruboty::Variable::Actions::Variable.new(message).array_init(message[:key])
32
+ end
33
+
34
+ def array_push(message)
35
+ Ruboty::Variable::Actions::Variable.new(message).array_push(message[:key], message[:values])
36
+ end
37
+
38
+ def array_remove(message)
39
+ Ruboty::Variable::Actions::Variable.new(message).array_push(message[:key], message[:values])
15
40
  end
16
41
  end
17
42
  end
@@ -2,28 +2,107 @@ module Ruboty
2
2
  module Variable
3
3
  module Actions
4
4
  class Variable < Ruboty::Actions::Base
5
- NAMESPACE = 'variable'
5
+ def initialize(message)
6
+ super
7
+ @var = Ruboty::Variable::Variable.new(message)
8
+ end
6
9
 
7
- def set_value(key, value)
8
- if values.has_key?(key)
10
+ def set(key, value)
11
+ if @var.values.has_key?(key)
9
12
  message.reply("Overwrite #{value} to #{key}")
10
13
  else
11
14
  message.reply("Set #{value} to #{key}")
12
15
  end
13
16
 
14
- values[key] = value
17
+ @var.set(key, value)
15
18
  end
16
19
 
17
- def get_value(key)
18
- if values.has_key?(key)
19
- message.reply(values[key])
20
+ def get(key)
21
+ if @var.values.has_key?(key)
22
+ message.reply(@var.get(key))
20
23
  else
21
24
  message.reply("Undefined #{key}")
22
25
  end
23
26
  end
24
27
 
25
- def values
26
- message.robot.brain.data[NAMESPACE] ||= {}
28
+ def delete(key)
29
+ if @var.values.has_key?(key)
30
+ @var.values.delete(key)
31
+ message.reply("Deleted #{key}")
32
+ else
33
+ message.reply("Undefined #{key}")
34
+ end
35
+ end
36
+
37
+ def list
38
+ if @var.values.empty?
39
+ message.reply('Variable is empty')
40
+ else
41
+ message.reply(variable_descriptions, code: true)
42
+ end
43
+ end
44
+
45
+ def array_init(key)
46
+ if @var.type(key) == 'array'
47
+ message.reply("Clear #{key} array")
48
+ else
49
+ message.reply("Created #{key} empty array")
50
+ end
51
+ @var.array_init(key)
52
+ end
53
+
54
+ def array_push(key, values)
55
+ case @var.type(key)
56
+ when 'array'
57
+ values.split(/\s+/).each do |value|
58
+ if @var.array_include?(key, value)
59
+ message.reply("#{key} already included #{value}")
60
+ else
61
+ @var.array_push(key, value)
62
+ message.reply("Push #{value} to #{key}")
63
+ end
64
+ end
65
+ when nil
66
+ message.reply("Undefined #{key}")
67
+ else
68
+ message.reply("#{key} is not array type")
69
+ end
70
+ end
71
+
72
+ def array_remove(key, values)
73
+ case @var.type(key)
74
+ when 'array'
75
+ values.split(/\s+/).each do |value|
76
+ if @var.array_include?(key, value)
77
+ @var.array_remove(key, value)
78
+ message.reply("Remove #{value} from #{key}")
79
+ else
80
+ message.reply("#{value} is not found in #{key}")
81
+ end
82
+ end
83
+ when nil
84
+ message.reply("Undefined #{key}")
85
+ else
86
+ message.reply("#{key} is not array type")
87
+ end
88
+ end
89
+
90
+ def variable_descriptions
91
+ key_len = [max_key_length, 'key'.size].max
92
+ type_len = [max_type_length, 'type'.size].max
93
+
94
+ mes = "%-#{key_len}s %-#{type_len}s value\n" % %w(key, type)
95
+ mes << @var.values.map do |k, v|
96
+ "%-#{key_len}s - %-#{type_len}s - #{v[:value]}" % [k, v[:type]]
97
+ end.join("\n")
98
+ end
99
+
100
+ def max_key_length
101
+ @var.values.keys.map(&:size).max
102
+ end
103
+
104
+ def max_type_length
105
+ @var.values.values.map {|x| x[:type].size}.max
27
106
  end
28
107
  end
29
108
  end
@@ -1,5 +1,5 @@
1
1
  module Ruboty
2
2
  module Variable
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -3,6 +3,50 @@ require "ruboty/handlers/variable"
3
3
 
4
4
  module Ruboty
5
5
  module Variable
6
- # Your code goes here...
6
+ class Variable
7
+ NAMESPACE = 'variable'
8
+
9
+ def initialize(message)
10
+ @values = message.robot.brain.data[NAMESPACE] ||= {}
11
+ end
12
+
13
+ def set(key, value)
14
+ values[key] = {
15
+ :type => 'string',
16
+ :value => value
17
+ }
18
+ end
19
+
20
+ def get(key)
21
+ values[key][:value] if values.has_key?(key)
22
+ end
23
+
24
+ def type(key)
25
+ values[key][:type] if values.has_key?(key)
26
+ end
27
+
28
+ def array_init(key)
29
+ values[key] = {
30
+ :type => 'array',
31
+ :value => []
32
+ }
33
+ end
34
+
35
+ def array_push(key, value)
36
+ values[key][:value] << value unless array_include?(key, value)
37
+ end
38
+
39
+ def array_remove(key, value)
40
+ values[key][:value].delete(value) if array_include?(key, value)
41
+ end
42
+
43
+ def array_include?(key, value)
44
+ values[key][:value].include?(value) if type(key) == 'array'
45
+ end
46
+
47
+ def values
48
+ @values
49
+ end
50
+ end
7
51
  end
8
52
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruboty-variable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - snakazawa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-28 00:00:00.000000000 Z
11
+ date: 2015-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruboty