Class: IO::Nosey::NoseyParker

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Validation, Validation::Adjustment, Validation::Condition
Defined in:
lib/io/nosey/noseyparker.rb

Defined Under Namespace

Classes: InvalidInputError

Constant Summary collapse

AskOpts =
OptionalArgument.define {
  opt :input, condition: Regexp
  opt :parse, aliases: [:parser], condition: ->v{adjustable? v}
  opt :return, condition: ->v{conditionable? v}
  opt :default, condition: CAN(:to_str)
  opt :echo, condition: BOOLEAN?, default: true
  opt :error, condition: CAN(:to_str), default: 'Your answer is invalid.'
  opt :multi_line, condition: BOOLEAN?, default: false
}

Instance Method Summary collapse

Constructor Details

#initialize(input = $stdin, output = $stdout) ⇒ NoseyParker

Returns a new instance of NoseyParker.

Parameters:

  • input (IO, StringIO) (defaults to: $stdin)
  • output (IO, StringIO) (defaults to: $stdout)

22
23
24
# File 'lib/io/nosey/noseyparker.rb', line 22

def initialize(input=$stdin, output=$stdout)
  @input, @output = input, output
end

Instance Method Details

#agree?(prompt) ⇒ Boolean

Parameters:

  • prompt (String)

Returns:

  • (Boolean)

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/io/nosey/noseyparker.rb', line 96

def agree?(prompt)
  print "#{prompt} [y or n]"

  input = getch
  print "\n"
  
  case input
  when 'n', 'N'
    false
  when 'y', 'Y'
    true
  else
    raise InvalidInputError
  end
rescue InvalidInputError
  retry
end

#ask(prompt, options = {}) ⇒ Object

Parameters:

  • prompt (String)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :input (Regexp)
  • :parse (Proc) — default: also :parser
  • :return (Proc, Method, #===)
  • :default (String, #to_str)
  • :echo (Boolean)
  • :error (String, #to_str)
  • :multi_line (Boolean)

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/io/nosey/noseyparker.rb', line 48

def ask(prompt, options={})
  opts = AskOpts.parse options
  
  print prompt

  if opts.default?
    print "(default: #{opts.default})"
  end

  if opts.multi_line
    if opts.echo
      input = read
    else
      input = noecho(&:read)
      puts
    end
  else
    if opts.echo
      input = gets.chomp
    else
      input = noecho(&:gets).chomp
      puts
    end
  end

  if input.empty? and opts.default?
    input = opts.default
  end
  
  if opts.input? and !_valid?(opts.input, input)
    raise InvalidInputError, opts.error
  end
  
  if opts.parse?
    input = opts.parse.call input
  end
  
  if opts.return? and !_valid?(opts.return, input)
    raise InvalidInputError, opts.error
  end
  
  input
rescue InvalidError
  puts $!.message unless $!.message.empty?
  retry
end

#choose(prompt, choices) ⇒ Object

Returns a member of choices.

Parameters:

  • prompt (String)
  • choices (Hash, #each_pair)

    key: value, value: description

Returns:

  • a member of choices

Raises:

  • (ArgumentError)

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/io/nosey/noseyparker.rb', line 117

def choose(prompt, choices)
  raise ArgumentError unless valid_choices? choices
  puts prompt
  puts [:index, :value, :description].join("\t")
  
  pairs = {}
  index = 1
  choices.each_pair do |value, description|
    puts [index, value, description].join("\t")
    pairs[index] = value
    index += 1
  end
  
  number = ask 'Select index:',
    input: /\A(\d+)\z/,
    parse: PARSE(Integer),
    return: AND(Integer, 1..(index - 1))
  
  pairs[number]
end