Module: Quik::Wizard
- Included in:
- Builder
- Defined in:
- lib/quik/wizard.rb
Overview
use a different name e.g. WizardHelpers, FormHelpers, InputHelper, etc - why, why not??
Constant Summary collapse
- YES_REGEX =
support YAML true values - double check (YAML does NOT support t/f)
/y|yes|on|t|true/i
- NO_REGEX =
/n|no|off|f|false/i
Instance Method Summary collapse
- #ask(question, default = nil) ⇒ Object
-
#getstr ⇒ Object
use getstr to avoid conflict w/ gets (use better name? read_string, readline (already exists too), etc.?).
-
#no?(question) ⇒ Boolean
defaults to yes - why, why not??.
- #say(text) ⇒ Object
- #select(title, options) ⇒ Object
-
#yes?(question) ⇒ Boolean
defaults to yes - why, why not??.
Instance Method Details
#ask(question, default = nil) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/quik/wizard.rb', line 55 def ask( question, default=nil ) ## todo: strip trailing question mark (?) if present (gets auto-included) if default print( "Q: #{question}? [#{default}]: " ) else print( "Q: #{question}?: " ) end str = getstr if default && str.empty? ## todo: best way to check for empty string? str = default end str end |
#getstr ⇒ Object
use getstr to avoid conflict w/ gets (use better name? read_string, readline (already exists too), etc.?)
14 15 16 17 18 |
# File 'lib/quik/wizard.rb', line 14 def getstr ## use getstr to avoid conflict w/ gets (use better name? read_string, readline (already exists too), etc.?) ## note: gets will include trailing newline (user hits return to enter data) ## - use strip for now (remove leading and traling whitspaces) - might later just use chomp ? (jsut removes newlines) $QUIK_WIZARD_IN.gets.strip end |
#no?(question) ⇒ Boolean
defaults to yes - why, why not??
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/quik/wizard.rb', line 41 def no?( question ) ## defaults to yes - why, why not?? ## todo: strip trailing question mark (?) if present (gets auto-included) print( "Q: #{question} (y/n)? [n]: " ) str = getstr if str.empty? || str =~ NO_REGEX true elsif str =~ YES_REGEX false else ## warn: unknown value?? true end end |
#say(text) ⇒ Object
20 21 22 |
# File 'lib/quik/wizard.rb', line 20 def say( text ) puts text end |
#select(title, options) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/quik/wizard.rb', line 71 def select( title, ) puts( "Q: #{title}: " ) .each_with_index do |opt,i| puts " #{i+1} - #{opt}" end print( " Your choice (1-#{.size})? [1]: " ) str = getstr if str.empty? ## todo: best way to check for empty string? num = 0 ## default to first option for now else num = str.to_i ## note: defaults to 0 if cannot convert? num -= 1 if num > 0 ## note: "convert" from 1-based to 0-based for ary; if invalid entry; default to 0 end [ num ] end |
#yes?(question) ⇒ Boolean
defaults to yes - why, why not??
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/quik/wizard.rb', line 28 def yes?( question ) ## defaults to yes - why, why not?? ## todo: strip trailing question mark (?) if present (gets auto-included) print( "Q: #{question} (y/n)? [y]: " ) str = getstr if str.empty? || str =~ YES_REGEX true elsif str =~ NO_REGEX false else ## warn: unknown value?? true end end |