ruboty-variable 0.0.4 → 0.1.0

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: fb8e5f840f7ccaebfce105e659c9ea6beda3971c
4
- data.tar.gz: 65edd0653a07a661bdeb26bac79910e9248b6edf
3
+ metadata.gz: d609c506eaa0a6f86ad30db21ced82e2389710e5
4
+ data.tar.gz: 1f96504e71d3bb97dc5767f78519a434d9ccc341
5
5
  SHA512:
6
- metadata.gz: f23b3bee227208142e4d9b3086effadc1fbbaf7a30f061b2d80fa748b99ff28cf83c1df3e55505cff44abcaeefbed044c77cc4d690c9eafe2cafca2634276ff5
7
- data.tar.gz: bf4d81183ba16c8c706bd14fbf4ed3a3ea770694b861487e8963e04f190475c99de806a3c01e50ecc2f741c5c952da4e85f11dc724daa8e348e9fcb27aa0cf66
6
+ metadata.gz: 27a0a6d3dc19e263d39c22607eb58668579058750d2387987e15b67532bb2c54ff8c017feb3a61391d555ceb82eb0f6000aaab4cb18990ff9e36bd98bffa38dd
7
+ data.tar.gz: 71beb09f1e8c92a14dc4ddd0e379ec37f5a9aa009e1be71c5483f350bf3043624d7646036576025ddd17d52dba7ea00b58699ddcb5a8675699a3d68780905144
@@ -1,4 +1,11 @@
1
1
  require "ruboty/variable/actions/variable"
2
+ require "ruboty/variable/actions/set"
3
+ require "ruboty/variable/actions/get"
4
+ require "ruboty/variable/actions/delete"
5
+ require "ruboty/variable/actions/list"
6
+ require "ruboty/variable/actions/array_init"
7
+ require "ruboty/variable/actions/array_push"
8
+ require "ruboty/variable/actions/array_remove"
2
9
 
3
10
  module Ruboty
4
11
  module Handlers
@@ -12,84 +19,31 @@ module Ruboty
12
19
  on /var array remove (?<key>\S+?) (?<values>.+?)\z/, name: 'array_remove', description: 'Remove values from array'
13
20
 
14
21
  def get_value(message)
15
- Ruboty::Variable::Actions::Variable.new(message).get(message[:key])
22
+ Ruboty::Variable::Actions::Get.new(message).call(message[:key])
16
23
  end
17
24
 
18
25
  def set_value(message)
19
- Ruboty::Variable::Actions::Variable.new(message).set(message[:key], message[:value])
26
+ Ruboty::Variable::Actions::Set.new(message).call(message[:key], message[:value])
20
27
  end
21
28
 
22
29
  def delete_value(message)
23
- Ruboty::Variable::Actions::Variable.new(message).delete(message[:key])
30
+ Ruboty::Variable::Actions::Delete.new(message).call(message[:key])
24
31
  end
25
32
 
26
33
  def list_values(message)
27
- Ruboty::Variable::Actions::Variable.new(message).list()
34
+ Ruboty::Variable::Actions::List.new(message).call
28
35
  end
29
36
 
30
37
  def array_init(message)
31
- Ruboty::Variable::Actions::Variable.new(message).array_init(message[:key])
38
+ Ruboty::Variable::Actions::ArrayInit.new(message).call(message[:key])
32
39
  end
33
40
 
34
41
  def array_push(message)
35
- Ruboty::Variable::Actions::Variable.new(message).array_push(message[:key], message[:values])
42
+ Ruboty::Variable::Actions::ArrayPush.new(message).call(message[:key], message[:values])
36
43
  end
37
44
 
38
45
  def array_remove(message)
39
- Ruboty::Variable::Actions::Variable.new(message).array_push(message[:key], message[:values])
40
- end
41
-
42
- on /var test\z/, name: 'test_values', hide: true
43
-
44
- def test_values(message)
45
- var = Ruboty::Variable::Actions::Variable.new(message)
46
- puts '# set'
47
- var.set("hoge", "hoge_value")
48
- var.set("123457", "999")
49
- var.set("aohef23", "awef8a23a3a32")
50
- var.set("string", "string_value")
51
-
52
- puts "\n# get"
53
- var.get("hoge")
54
- var.get("piyo")
55
-
56
- puts "\n# list"
57
- var.list
58
-
59
- puts "\n# delete"
60
- var.delete("hoge")
61
- var.delete("hoge")
62
- var.list
63
-
64
- puts "\n# init array"
65
- var.array_init("ary")
66
- var.array_init("string")
67
- var.array_init("string")
68
- var.list
69
-
70
- puts "\n# push array"
71
- var.array_push("ary", "one")
72
- var.array_push("ary", "two")
73
- var.array_push("ary", "two")
74
- var.list
75
-
76
- puts "\n# push array to empty array"
77
- var.array_push("hoge", "kkkk")
78
- var.array_push("zxcv", "pppp")
79
-
80
- puts "\n# push values to array"
81
- var.array_push("ary", "three four five")
82
- var.list
83
-
84
- puts "\n# remove values from array"
85
- var.array_remove("ary", "one three")
86
- var.list
87
-
88
- puts "\n# remove error"
89
- var.array_remove("ary", "one three")
90
- var.array_remove("noary", "one")
91
- var.array_remove("123457", "one")
92
- var.list
46
+ Ruboty::Variable::Actions::ArrayRemove.new(message).call(message[:key], message[:values])
93
47
  end
94
48
  end
95
49
  end
@@ -11,40 +11,40 @@ module Ruboty
11
11
  end
12
12
 
13
13
  def set(key, value)
14
- values[key] = {
14
+ data[key] = {
15
15
  :type => 'string',
16
16
  :value => value
17
17
  }
18
18
  end
19
19
 
20
20
  def get(key)
21
- values[key][:value] if values.has_key?(key)
21
+ data[key][:value] if data.has_key?(key)
22
22
  end
23
23
 
24
24
  def type(key)
25
- values[key][:type] if values.has_key?(key)
25
+ data[key][:type] if data.has_key?(key)
26
26
  end
27
27
 
28
28
  def array_init(key)
29
- values[key] = {
29
+ data[key] = {
30
30
  :type => 'array',
31
31
  :value => []
32
32
  }
33
33
  end
34
34
 
35
35
  def array_push(key, value)
36
- values[key][:value] << value unless array_include?(key, value)
36
+ data[key][:value] << value unless array_include?(key, value)
37
37
  end
38
38
 
39
39
  def array_remove(key, value)
40
- values[key][:value].delete(value) if array_include?(key, value)
40
+ data[key][:value].delete(value) if array_include?(key, value)
41
41
  end
42
42
 
43
43
  def array_include?(key, value)
44
- values[key][:value].include?(value) if type(key) == 'array'
44
+ data[key][:value].include?(value) if type(key) == 'array'
45
45
  end
46
46
 
47
- def values
47
+ def data
48
48
  @values
49
49
  end
50
50
  end
@@ -0,0 +1,28 @@
1
+ module Ruboty
2
+ module Variable
3
+ module Actions
4
+ class ArrayInit < Ruboty::Variable::Actions::Variable
5
+ def call(key)
6
+ was_type = var.type(key)
7
+
8
+ var.array_init(key)
9
+
10
+ if was_type == 'array'
11
+ message.reply(clear_message(key))
12
+ else
13
+ message.reply(create_message(key))
14
+ end
15
+ end
16
+
17
+ def create_message(key)
18
+ "Created #{key} empty array"
19
+ end
20
+
21
+ def clear_message(key)
22
+ "Clear #{key} array"
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+
@@ -0,0 +1,44 @@
1
+ module Ruboty
2
+ module Variable
3
+ module Actions
4
+ class ArrayPush < Ruboty::Variable::Actions::Variable
5
+ def call(key, values)
6
+ case var.type(key)
7
+ when 'array'
8
+ values.split(/\s+/).each {|value| push(key, value)}
9
+ when nil
10
+ message.reply(undefined_message(key))
11
+ else
12
+ message.reply(type_error_message(key))
13
+ end
14
+ end
15
+
16
+ def push(key, value)
17
+ if var.array_include?(key, value)
18
+ message.reply(included_message(key, value))
19
+ else
20
+ var.array_push(key, value)
21
+ message.reply(push_message(key, value))
22
+ end
23
+ end
24
+
25
+ def included_message(key, value)
26
+ "#{key} already included #{value}"
27
+ end
28
+
29
+ def push_message(key, value)
30
+ "Push #{value} to #{key}"
31
+ end
32
+
33
+ def type_error_message(key)
34
+ "#{key} is not array type"
35
+ end
36
+
37
+ def undefined_message(key)
38
+ "Undefined #{key}"
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+
@@ -0,0 +1,44 @@
1
+ module Ruboty
2
+ module Variable
3
+ module Actions
4
+ class ArrayRemove < Ruboty::Variable::Actions::Variable
5
+ def call(key, values)
6
+ case var.type(key)
7
+ when 'array'
8
+ values.split(/\s+/).each {|value| remove(key, value)}
9
+ when nil
10
+ message.reply(undefined_message(key))
11
+ else
12
+ message.reply(type_error_message(key))
13
+ end
14
+ end
15
+
16
+ def remove(key, value)
17
+ if var.array_include?(key, value)
18
+ var.array_remove(key, value)
19
+ message.reply(remove_message(key, value))
20
+ else
21
+ message.reply(not_found_message(key, value))
22
+ end
23
+ end
24
+
25
+ def remove_message(key, value)
26
+ "Remove #{value} from #{key}"
27
+ end
28
+
29
+ def not_found_message(key, value)
30
+ "#{value} is not found in #{key}"
31
+ end
32
+
33
+ def type_error_message(key)
34
+ "#{key} is not array type"
35
+ end
36
+
37
+ def undefined_message(key)
38
+ "Undefined #{key}"
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+
@@ -0,0 +1,25 @@
1
+ module Ruboty
2
+ module Variable
3
+ module Actions
4
+ class Delete < Ruboty::Variable::Actions::Variable
5
+ def call(key)
6
+ if var.data.has_key?(key)
7
+ var.data.delete(key)
8
+ message.reply(delete_message(key))
9
+ else
10
+ message.reply(undefined_message(key))
11
+ end
12
+ end
13
+
14
+ def delete_message(key)
15
+ "Deleted #{key}"
16
+ end
17
+
18
+ def undefined_message(key)
19
+ "Undefined #{key}"
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,20 @@
1
+ module Ruboty
2
+ module Variable
3
+ module Actions
4
+ class Get < Ruboty::Variable::Actions::Variable
5
+ def call(key)
6
+ if var.data.has_key?(key)
7
+ message.reply(var.get(key))
8
+ else
9
+ message.reply(undefined_message(key))
10
+ end
11
+ end
12
+
13
+ def undefined_message(key)
14
+ "Undefined #{key}"
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,40 @@
1
+ module Ruboty
2
+ module Variable
3
+ module Actions
4
+ class List < Ruboty::Variable::Actions::Variable
5
+ def call
6
+ if var.data.empty?
7
+ message.reply(empty_message)
8
+ else
9
+ message.reply(variable_descriptions, code: true)
10
+ end
11
+ end
12
+
13
+ def empty_message
14
+ 'Variable is empty'
15
+ end
16
+
17
+ def variable_descriptions
18
+ key_len = [max_key_length, 'key'.size].max
19
+ type_len = [max_type_length, 'type'.size].max
20
+
21
+ header = "%-#{key_len}s %-#{type_len}s value\n" % %w(key type)
22
+
23
+ body = var.data.map do |k, v|
24
+ "%-#{key_len}s - %-#{type_len}s - #{v[:value]}" % [k, v[:type]]
25
+ end.join("\n")
26
+
27
+ header << body
28
+ end
29
+
30
+ def max_key_length
31
+ var.data.keys.map(&:size).max
32
+ end
33
+
34
+ def max_type_length
35
+ var.data.values.map {|x| x[:type].size}.max
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,30 @@
1
+ module Ruboty
2
+ module Variable
3
+ module Actions
4
+ class Set < Ruboty::Variable::Actions::Variable
5
+ def call(key, value)
6
+ had_key = var.data.has_key?(key)
7
+
8
+ var.set(key, value)
9
+
10
+ message.reply(
11
+ if had_key
12
+ overwrite_message(key, value)
13
+ else
14
+ set_message(key, value)
15
+ end
16
+ )
17
+ end
18
+
19
+ def set_message(key, value)
20
+ "Set #{value} to #{key}"
21
+ end
22
+
23
+ def overwrite_message(key, value)
24
+ "Overwrite #{value} to #{key}"
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+
@@ -7,102 +7,8 @@ module Ruboty
7
7
  @var = Ruboty::Variable::Variable.new(message)
8
8
  end
9
9
 
10
- def set(key, value)
11
- if @var.values.has_key?(key)
12
- message.reply("Overwrite #{value} to #{key}")
13
- else
14
- message.reply("Set #{value} to #{key}")
15
- end
16
-
17
- @var.set(key, value)
18
- end
19
-
20
- def get(key)
21
- if @var.values.has_key?(key)
22
- message.reply(@var.get(key))
23
- else
24
- message.reply("Undefined #{key}")
25
- end
26
- end
27
-
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
10
+ protected def var
11
+ @var
106
12
  end
107
13
  end
108
14
  end
@@ -1,5 +1,5 @@
1
1
  module Ruboty
2
2
  module Variable
3
- VERSION = "0.0.4"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruboty-variable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - snakazawa
@@ -66,6 +66,13 @@ files:
66
66
  - Rakefile
67
67
  - lib/ruboty/handlers/variable.rb
68
68
  - lib/ruboty/variable.rb
69
+ - lib/ruboty/variable/actions/array_init.rb
70
+ - lib/ruboty/variable/actions/array_push.rb
71
+ - lib/ruboty/variable/actions/array_remove.rb
72
+ - lib/ruboty/variable/actions/delete.rb
73
+ - lib/ruboty/variable/actions/get.rb
74
+ - lib/ruboty/variable/actions/list.rb
75
+ - lib/ruboty/variable/actions/set.rb
69
76
  - lib/ruboty/variable/actions/variable.rb
70
77
  - lib/ruboty/variable/version.rb
71
78
  - ruboty-variable.gemspec