class Ruboty::Variable::Actions::List

Public Instance Methods

call() click to toggle source
# File lib/ruboty/variable/actions/list.rb, line 5
def call
  if var.data.empty?
    message.reply(empty_message)
  else
    message.reply(variable_descriptions, code: true)
  end
end
empty_message() click to toggle source
# File lib/ruboty/variable/actions/list.rb, line 13
def empty_message
  'Variable is empty'
end
max_key_length() click to toggle source
# File lib/ruboty/variable/actions/list.rb, line 30
def max_key_length
  var.data.keys.map(&:size).max
end
max_type_length() click to toggle source
# File lib/ruboty/variable/actions/list.rb, line 34
def max_type_length
  var.data.values.map {|x| x[:type].size}.max
end
variable_descriptions() click to toggle source
# File lib/ruboty/variable/actions/list.rb, line 17
def variable_descriptions
  key_len = [max_key_length, 'key'.size].max
  type_len = [max_type_length, 'type'.size].max

  header = "%-#{key_len}s   %-#{type_len}s   value\n" % %w(key type)

  body = var.data.map do |k, v|
    "%-#{key_len}s - %-#{type_len}s - #{v[:value]}" % [k, v[:type]]
  end.join("\n")

  header << body
end