Class: Ruboty::Variable::Actions::Variable
- Inherits:
-
Actions::Base
- Object
- Actions::Base
- Ruboty::Variable::Actions::Variable
- Defined in:
- lib/ruboty/variable/actions/variable.rb
Instance Method Summary collapse
- #array_init(key) ⇒ Object
- #array_push(key, values) ⇒ Object
- #array_remove(key, values) ⇒ Object
- #delete(key) ⇒ Object
- #get(key) ⇒ Object
-
#initialize(message) ⇒ Variable
constructor
A new instance of Variable.
- #list ⇒ Object
- #max_key_length ⇒ Object
- #max_type_length ⇒ Object
- #set(key, value) ⇒ Object
- #variable_descriptions ⇒ Object
Constructor Details
Instance Method Details
#array_init(key) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/ruboty/variable/actions/variable.rb', line 45 def array_init(key) if @var.type(key) == 'array' .reply("Clear #{key} array") else .reply("Created #{key} empty array") end @var.array_init(key) end |
#array_push(key, values) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ruboty/variable/actions/variable.rb', line 54 def array_push(key, values) case @var.type(key) when 'array' values.split(/\s+/).each do |value| if @var.array_include?(key, value) .reply("#{key} already included #{value}") else @var.array_push(key, value) .reply("Push #{value} to #{key}") end end when nil .reply("Undefined #{key}") else .reply("#{key} is not array type") end end |
#array_remove(key, values) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/ruboty/variable/actions/variable.rb', line 72 def array_remove(key, values) case @var.type(key) when 'array' values.split(/\s+/).each do |value| if @var.array_include?(key, value) @var.array_remove(key, value) .reply("Remove #{value} from #{key}") else .reply("#{value} is not found in #{key}") end end when nil .reply("Undefined #{key}") else .reply("#{key} is not array type") end end |
#delete(key) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/ruboty/variable/actions/variable.rb', line 28 def delete(key) if @var.values.has_key?(key) @var.values.delete(key) .reply("Deleted #{key}") else .reply("Undefined #{key}") end end |
#get(key) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/ruboty/variable/actions/variable.rb', line 20 def get(key) if @var.values.has_key?(key) .reply(@var.get(key)) else .reply("Undefined #{key}") end end |
#list ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/ruboty/variable/actions/variable.rb', line 37 def list if @var.values.empty? .reply('Variable is empty') else .reply(variable_descriptions, code: true) end end |
#max_key_length ⇒ Object
100 101 102 |
# File 'lib/ruboty/variable/actions/variable.rb', line 100 def max_key_length @var.values.keys.map(&:size).max end |
#max_type_length ⇒ Object
104 105 106 |
# File 'lib/ruboty/variable/actions/variable.rb', line 104 def max_type_length @var.values.values.map {|x| x[:type].size}.max end |
#set(key, value) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/ruboty/variable/actions/variable.rb', line 10 def set(key, value) if @var.values.has_key?(key) .reply("Overwrite #{value} to #{key}") else .reply("Set #{value} to #{key}") end @var.set(key, value) end |
#variable_descriptions ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/ruboty/variable/actions/variable.rb', line 90 def variable_descriptions key_len = [max_key_length, 'key'.size].max type_len = [max_type_length, 'type'.size].max mes = "%-#{key_len}s %-#{type_len}s value\n" % %w(key type) mes << @var.values.map do |k, v| "%-#{key_len}s - %-#{type_len}s - #{v[:value]}" % [k, v[:type]] end.join("\n") end |