Class: KiTrello::Interactive
- Inherits:
-
Object
- Object
- KiTrello::Interactive
- Defined in:
- lib/ki_trello/interactive.rb
Instance Method Summary collapse
- #choose_from_many(things, question) ⇒ Object
-
#initialize(input = $stdin, output = $stderr) ⇒ Interactive
constructor
A new instance of Interactive.
-
#repeatedly_ask(question) ⇒ Object
“Resistance is futile!”, “We will get an answer!”.
-
#repeatedly_choose_from(things, question) ⇒ Object
“We have ways of making you talk!”.
- #select_board(prompt) ⇒ Object
- #select_item(items, prompt = nil, input = $stdin, output = $stdout, err = $stderr) ⇒ Object
- #select_list(board_id, prompt) ⇒ Object
- #simple_query(prompt) ⇒ Object
Constructor Details
#initialize(input = $stdin, output = $stderr) ⇒ Interactive
Returns a new instance of Interactive.
10 11 12 13 |
# File 'lib/ki_trello/interactive.rb', line 10 def initialize(input=$stdin, output=$stderr) @input = input @output = output end |
Instance Method Details
#choose_from_many(things, question) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ki_trello/interactive.rb', line 33 def choose_from_many(things, question) @output.puts "\n "+question.to_s things.each_with_index do |thing, index| @output.puts " #{index+1} ".green + " #{thing}" end index = ( (simple_query( "(#{(1..things.size)})").to_i) - 1 ) index = ( index >= 0 && index < things.size ) ? index : nil end |
#repeatedly_ask(question) ⇒ Object
“Resistance is futile!”, “We will get an answer!”
16 17 18 19 20 21 22 |
# File 'lib/ki_trello/interactive.rb', line 16 def repeatedly_ask(question) response = "" while response == "" response = simple_query("\n "+question) end response end |
#repeatedly_choose_from(things, question) ⇒ Object
“We have ways of making you talk!”
25 26 27 28 29 30 31 |
# File 'lib/ki_trello/interactive.rb', line 25 def repeatedly_choose_from(things, question) index = nil while index.nil? index = choose_from_many(things, question) end index end |
#select_board(prompt) ⇒ Object
65 66 67 68 |
# File 'lib/ki_trello/interactive.rb', line 65 def select_board(prompt) boards = MultiJson.load(KiTrello::Command.new.boards) select_item(boards, prompt) end |
#select_item(items, prompt = nil, input = $stdin, output = $stdout, err = $stderr) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/ki_trello/interactive.rb', line 53 def select_item(items, prompt=nil, input=$stdin, output=$stdout, err=$stderr) items = mapify(items) index = get_index(items, prompt) return index if index.nil? items[index]['id'] end |
#select_list(board_id, prompt) ⇒ Object
60 61 62 63 |
# File 'lib/ki_trello/interactive.rb', line 60 def select_list(board_id, prompt) lists = MultiJson.load(KiTrello::Command.new.lists(board_id)) select_item(lists, prompt) end |
#simple_query(prompt) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/ki_trello/interactive.rb', line 44 def simple_query(prompt) if prompt.nil? @output.print "\n> " else @output.print "\n #{prompt}: " end @input.gets.chomp end |