Class: Ruboty::Variable::Actions::Variable

Inherits:
Actions::Base
  • Object
show all
Defined in:
lib/ruboty/variable/actions/variable.rb

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ Variable

Returns a new instance of Variable.



5
6
7
8
# File 'lib/ruboty/variable/actions/variable.rb', line 5

def initialize(message)
  super
  @var = Ruboty::Variable::Variable.new(message)
end

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'
    message.reply("Clear #{key} array")
  else
    message.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)
          message.reply("#{key} already included #{value}")
        else
          @var.array_push(key, value)
          message.reply("Push #{value} to #{key}")
        end
      end
    when nil
      message.reply("Undefined #{key}")
    else
      message.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)
          message.reply("Remove #{value} from #{key}")
        else
          message.reply("#{value} is not found in #{key}")
        end
      end
    when nil
      message.reply("Undefined #{key}")
    else
      message.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)
    message.reply("Deleted #{key}")
  else
    message.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)
    message.reply(@var.get(key))
  else
    message.reply("Undefined #{key}")
  end
end

#listObject



37
38
39
40
41
42
43
# File 'lib/ruboty/variable/actions/variable.rb', line 37

def list
  if @var.values.empty?
    message.reply('Variable is empty')
  else
    message.reply(variable_descriptions, code: true)
  end
end

#max_key_lengthObject



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_lengthObject



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)
    message.reply("Overwrite #{value} to #{key}")
  else
    message.reply("Set #{value} to #{key}")
  end

  @var.set(key, value)
end

#variable_descriptionsObject



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