class Rigit::Prompt

Handles prompt request for user input in batch. This is a wrapper around TTY::Prompt that gets all the params (typically from the rig config file), and asks the user for input one by one, while considering prefilled values.

Attributes

input[R]
params[R]

Public Class Methods

new(params) click to toggle source
# File lib/rigit/prompt.rb, line 11
def initialize(params)
  @params = params
end

Public Instance Methods

get_input(prefill={}) click to toggle source

Asks the user for input. If a prefill hash is provided, it will be used for values, and skip asking the user to provide answers for the ones that are prefilled.

# File lib/rigit/prompt.rb, line 18
def get_input(prefill={})
  @input = {}
  params.each do |key, spec|
    next if skip_by_condition? spec
    @input[key] = prefill.has_key?(key) ? prefill[key] : ask(spec)
  end
  @input
end

Private Instance Methods

ask(param) click to toggle source
# File lib/rigit/prompt.rb, line 29
def ask(param)
  text = param.prompt
  default = param.default

  case param[:type]
  when 'yesno'
    prompt.yes?(text, default: default) ? 'yes' : 'no'
  when 'text'
    prompt.ask text, default: default
  when 'select'
    prompt.select text, param.list, symbols: { marker: '>' }
  when 'ruby'
    instance_eval param.code
  else
    raise ConfigError, "Unknown type '#{param[:type]}'"
  end
end
prompt() click to toggle source
# File lib/rigit/prompt.rb, line 53
def prompt
  @prompt ||= TTY::Prompt.new
end
skip_by_condition?(spec) click to toggle source
# File lib/rigit/prompt.rb, line 47
def skip_by_condition?(spec)
  return unless spec.has_key? :condition
  key, value = spec.condition.split '='
  input[key.to_sym] != value
end