Module: Lolcommits::Plugin::ConfigurationHelper

Included in:
Base
Defined in:
lib/lolcommits/plugin/configuration_helper.rb

Instance Method Summary collapse

Instance Method Details

#parse_user_input(str) ⇒ Object

handle for bools, strings, ints and blanks from user input



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/lolcommits/plugin/configuration_helper.rb', line 7

def parse_user_input(str)
  if 'true'.casecmp(str).zero?
    true
  elsif 'false'.casecmp(str).zero?
    false
  elsif str =~ /^[0-9]+$/
    str.to_i
  elsif str.strip.empty?
    nil
  else
    str
  end
end

#prompt_autocomplete_hash(prompt, items, name: 'name', value: 'value', suggest_words: 5) ⇒ Object

user input with autocomplete (via tab) through array of named values

e.g. prompt_autocomplete_hash(“Organization: ”, orgs)

where orgs are an array of hashes like so (with string keys): [

{ 'name' => 'some human readable name', 'value' => 1234 },

] User will be asked for Organization, can tab to autocomplete, and chosen value is returned.



32
33
34
35
36
37
# File 'lib/lolcommits/plugin/configuration_helper.rb', line 32

def prompt_autocomplete_hash(prompt, items, name: 'name', value: 'value', suggest_words: 5)
  words = items.map { |item| item[name] }.sort
  puts "e.g. #{words.take(suggest_words).join(', ')}" if suggest_words.positive?
  completed_input = gets_autocomplete(prompt, words)
  items.find { |item| item[name] == completed_input }[value]
end