Module: Askable::ClassMethods

Defined in:
lib/mixins/askable.rb

Instance Method Summary collapse

Instance Method Details

#ask(msg = "", opts = {}, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mixins/askable.rb', line 4

def ask(msg="", opts={}, &block)
  begin
    answer = Question.new(msg, STDIN).answer
    
    unless opts[:no_value]
      if answer.nil? || answer == ""
        colored_say("You must enter a value\nTry again\n")
        ask(msg, &block)
      end
    end
  rescue Exception => e
    colored_say e.inspect
    exit 0
  end
  answer
end

#ask_with_help(opts = {}, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mixins/askable.rb', line 21

def ask_with_help(opts={}, &block)
  help_str = opts[:help]
  message = opts[:message]

  colored_say("#{message} (h for help)")
  o = ask("? ")
  
  if %w(h H).include?(o)
    colored_say help_str
    ask_with_help(opts, &block)
  else
    block.call(o) if block
  end
  o
end

#choose(str, choices = {}, opts = {}) {|answer| ... } ⇒ Object

Yields:

  • (answer)


58
59
60
61
62
63
# File 'lib/mixins/askable.rb', line 58

def choose(str, choices={}, opts={}, &block)
  colored_say(str)
  answer = ask(choices)
  yield answer if block
  answer
end

#colored_ask(str, o = {}) ⇒ Object



37
38
39
# File 'lib/mixins/askable.rb', line 37

def colored_ask(str, o={})
  ask(substitute_color_tags(str), o)
end

#colored_print(str) ⇒ Object



54
55
56
# File 'lib/mixins/askable.rb', line 54

def colored_print(str)
  print substitute_color_tags(str)
end

#colored_say(str, o = {}) ⇒ Object



49
50
51
52
# File 'lib/mixins/askable.rb', line 49

def colored_say(str, o={})
  out = substitute_color_tags(str)
  o[:no_newline] ? print(out) : puts(out)
end

#rescued_ask(m, r) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/mixins/askable.rb', line 41

def rescued_ask(m, r)
  begin
    t = colored_ask m
  rescue Exception => e
    say r
  end
end

#substitute_color_tags(data) ⇒ Object



73
74
75
# File 'lib/mixins/askable.rb', line 73

def substitute_color_tags(data)
  Colors.process(data)
end

#waitObject



65
66
67
68
69
70
71
# File 'lib/mixins/askable.rb', line 65

def wait
   begin        
     STDIN.readline unless auto_install # -y passed
   rescue Interrupt
     exit 2
   end
end