Class: Ritsu::Utility::SimpleIO

Inherits:
Object
  • Object
show all
Defined in:
lib/ritsu/utility/simple_io.rb

Overview

This class is lifted from rubigen’s SimpleLogger

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input = $stdin, output = $stdout) ⇒ SimpleIO

Returns a new instance of SimpleIO.



10
11
12
13
14
15
# File 'lib/ritsu/utility/simple_io.rb', line 10

def initialize(input = $stdin, output = $stdout)
  @input = input
  @output = output
  @quiet = false
  @level = 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



60
61
62
# File 'lib/ritsu/utility/simple_io.rb', line 60

def method_missing(method, *args, &block)
  log(method.to_s, args.first, &block)
end

Instance Attribute Details

#inputObject

Returns the value of attribute input.



6
7
8
# File 'lib/ritsu/utility/simple_io.rb', line 6

def input
  @input
end

#outputObject

Returns the value of attribute output.



7
8
9
# File 'lib/ritsu/utility/simple_io.rb', line 7

def output
  @output
end

#quietObject

Returns the value of attribute quiet.



8
9
10
# File 'lib/ritsu/utility/simple_io.rb', line 8

def quiet
  @quiet
end

Instance Method Details

#ask_yes_no_all(message) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ritsu/utility/simple_io.rb', line 44

def ask_yes_no_all(message)
  @output.print("#{message} (yes/no/all): ")
  answer = @input.gets.strip
  case answer
  when /^y(es)?$/i
    return :yes
  when /^no?$/i
    return :no
  when /^a(ll)?$/i
    return :all
  else
    ask_yes_no_all(message)
  end
end

#indent(&block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/ritsu/utility/simple_io.rb', line 22

def indent(&block)
  @level += 1
  if block_given?
    begin
      block.call
    ensure
      outdent
    end
  end
end

#log(status, message, &block) ⇒ Object



17
18
19
20
# File 'lib/ritsu/utility/simple_io.rb', line 17

def log(status, message, &block)
  @output.print("%12s %s%s\n" % [status, ' ' * @level, message]) unless quiet
  indent(&block) if block_given?
end

#outdentObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/ritsu/utility/simple_io.rb', line 33

def outdent
  @level -= 1
  if block_given?
    begin
      block.call
    ensure
      indent
    end
  end
end