class IO::Nosey::Parker
Constants
- AskOpts
Public Class Methods
adjustable?(object)
click to toggle source
# File lib/io/nosey/parker.rb, line 10 def self.adjustable?(object) case object when Proc object.arity == 1 else if object.respond_to?(:to_proc) object.to_proc.arity == 1 else false end end end
new(input: $stdin, output: $stdout)
click to toggle source
@param input [IO, StringIO] @param output [IO, StringIO]
# File lib/io/nosey/parker.rb, line 25 def initialize(input: $stdin, output: $stdout) @input, @output = input, output end
Public Instance Methods
agree?(prompt)
click to toggle source
@param prompt [String]
# File lib/io/nosey/parker.rb, line 88 def agree?(prompt) @output.print("#{prompt} [y or n]") input = @input.getch @output.print("\n") case input when 'n', 'N' false when 'y', 'Y' true else raise InvalidInputError end rescue InvalidInputError retry end
ask(prompt, **kw_args)
click to toggle source
@param prompt [String]
# File lib/io/nosey/parker.rb, line 40 def ask(prompt, **kw_args) opts = AskOpts.parse(kw_args) @output.print(prompt) if opts.default? @output.print("(default: #{opts.default})") end if opts.multi_line if opts.echo input = @input.read else input = @input.noecho(&:read) @output.puts end else if opts.echo input = @input.gets.chomp else input = @input.noecho(&:gets).chomp @output.puts end end if input.empty? && opts.default? input = opts.default end if opts.input? && !valid?(opts.input, input) raise InvalidInputError, opts.error end if opts.parse? input = opts.parse.call(input) end if opts.return? && !valid?(opts.return, input) raise InvalidInputError, opts.error end input rescue InvalidInputError @output.puts $!.message unless $!.message.empty? retry end
choose(prompt, choices)
click to toggle source
@param prompt [String] @param choices [Hash{Object => String}] key: value, value: description @return a member of choices
# File lib/io/nosey/parker.rb, line 109 def choose(prompt, choices) raise ArgumentError if choices.empty? @output.puts prompt @output.puts [:index, :value, :description].join("\t") pairs = {} index = 1 choices.each_pair do |value, description| @output.puts [index, value, description].join("\t") pairs[index] = value index += 1 end number = ask('Select index:', input: /\A(\d+)\z/, parse: ->v { Integer(v) }, return: Eqq.AND(Integer, 1..(index - 1))) pairs[number] end
Private Instance Methods
valid?(pattern, value)
click to toggle source
@param [Proc, Method, ===] pattern @param [Object] value
# File lib/io/nosey/parker.rb, line 135 def valid?(pattern, value) !!( case pattern when Proc instance_exec(value, &pattern) when Method pattern.call(value) else pattern === value end ) end