Class: Mousevc::Input

Inherits:
Object
  • Object
show all
Defined in:
lib/mousevc/input.rb

Overview

A globally accessible class to allow access to user input and provide input specific notices.

Constant Summary collapse

@@notice =
nil
@@data =
nil
@@prompts =
{:default => '>'}
@@appearance =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#appearanceSymbol



30
# File 'lib/mousevc/input.rb', line 30

@@appearance = nil

#dataString (readonly)



18
# File 'lib/mousevc/input.rb', line 18

@@data = nil

#noticeString



12
# File 'lib/mousevc/input.rb', line 12

@@notice = nil

#promptsHash (readonly)



24
# File 'lib/mousevc/input.rb', line 24

@@prompts = {:default => '>'}

Class Method Details

.appearanceSymbol



35
36
37
# File 'lib/mousevc/input.rb', line 35

def self.appearance
  @@appearance
end

.appearance=(value) ⇒ Object

Set the default appearance for the prompt



44
45
46
# File 'lib/mousevc/input.rb', line 44

def self.appearance=(value)
  @@appearance = value
end

.clear(*args) ⇒ Object

Clears all or a list of class variables by setting them to nil



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mousevc/input.rb', line 53

def self.clear(*args)
  if args.empty?
    args = [
      :notice,
      :data,
      :prompts,
      :appearance
    ]
  end
  to_defaults(args)
end

.clear?Boolean

Check if the user is tring to clear the input data



146
147
148
# File 'lib/mousevc/input.rb', line 146

def self.clear?
  ['c', 'clear'].include?(@@data)
end

.dataString

Get the current value of user input retrieved via Input.prompt



110
111
112
# File 'lib/mousevc/input.rb', line 110

def self.data
  @@data
end

.noticeString

Get the notice message



92
93
94
# File 'lib/mousevc/input.rb', line 92

def self.notice
  @@notice
end

.notice=(value) ⇒ Object

Set the notice message. This is a good place to put error messages corresponding to user input



101
102
103
# File 'lib/mousevc/input.rb', line 101

def self.notice=(value)
  @@notice = value
end

.notice?Boolean

Check if a notice message exists



119
120
121
# File 'lib/mousevc/input.rb', line 119

def self.notice?
  ! @@notice.to_s.empty?
end

.prompt(appearance = nil) ⇒ Object

Note:

Calling this method will stop all execution until the user submits input

Prompts the user for input using gets.strip Once input is submitted it will be available via Input.data



74
75
76
77
78
# File 'lib/mousevc/input.rb', line 74

def self.prompt(appearance=nil)
  appearance = pick(appearance)
  print "\n#{appearance} "
  @@data = $stdin.gets.to_s.strip
end

.promptsHash



83
84
85
# File 'lib/mousevc/input.rb', line 83

def self.prompts
  @@prompts
end

.quit?Boolean

Check if the user is trying to quit the application



137
138
139
# File 'lib/mousevc/input.rb', line 137

def self.quit?
  ['q', 'quit', 'exit'].include?(@@data)
end

.reset?Boolean

Check if the user is trying to reset the application



128
129
130
# File 'lib/mousevc/input.rb', line 128

def self.reset?
  ['r', 'reset'].include?(@@data)
end