Module: EpubForge::CustomHelpers

Included in:
EpubForge
Defined in:
lib/epubforge/custom_helpers.rb

Instance Method Summary collapse

Instance Method Details

#ask(question, opts = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/epubforge/custom_helpers.rb', line 3

def ask question, opts = {}
  opts[:menu] ||= [["Y", "Yes"], ["N", "No"]]

  answer = nil

  while answer.nil?
    menu_string = opts[:menu].map{ |li| "#{li[0]}):\t#{li[1]}"}.join("\n\t")
    puts "#{question} :\n\t#{ menu_string }"
    line = Readline.readline(">> ",true).strip.upcase

    opts[:menu].each{ |li|
      if li[0].upcase == line.to_s
        answer = li
      end
    }

    puts "I don't understand that response" if answer.nil?
  end

  if opts[:return_value]
    answer[1]
  else
    answer[0].upcase
  end
end

#collect_stdout(dest = StringIO.new, &block) ⇒ Object

To see the STDOUT, simply call EpubForge.collect_stdout( STDOUT )



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/epubforge/custom_helpers.rb', line 30

def collect_stdout( dest = StringIO.new, &block )
  if dest == $stdout
    yield
  else
    raise ArgumentError.new("No block given.") unless block_given?
  
    prior_stdout = $stdout
    # @epf_prior_stdout_stack ||= []
    # @epf_prior_stdout_stack << $stdout
   
    $stdout = begin
                if dest.is_a?( String ) || dest.is_a?( Pathname )
                  File.open( dest, "a" )
                elsif dest.is_a?( IO ) || dest.is_a?( StringIO )
                  dest
                else
                  raise ArgumentError.new("collect_stdout cannot take a <#{dest.class.name}> as an argument.")
                end
              end

    $stdout.sync = true
    yield

    $stdout = prior_stdout
  
    dest.is_a?( StringIO ) ? dest.string : nil
  end
end