Class: Tracker::Cli::View::Input

Inherits:
Object
  • Object
show all
Defined in:
lib/tracker/cli/view/input.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute_name, type: nil) ⇒ Input

Returns a new instance of Input.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tracker/cli/view/input.rb', line 7

def initialize(attribute_name, type: nil)
  type ||= :string
  
  loop do
    $stderr.print "#{attribute_name}#{' (y/n)' if type == :boolean}? "
    value = $stdin.gets.chomp
    $stderr.print "\n"
    
    if type == :string
      if value == ''
        $stderr.print "Cannot be blank.\n"
      else
        @value = value
      end
      
    elsif type == :boolean
      if value == 'y' || value == 'n'
        @value = value == 'y'
      else
        $stderr.print "Please provide either \"y\" or \"n\"\n"
      end
    end
    
    break unless @value.nil?
  end
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/tracker/cli/view/input.rb', line 5

def value
  @value
end