Class: Ritsu::Utility::SimpleIO
- Inherits:
-
Object
- Object
- Ritsu::Utility::SimpleIO
- Defined in:
- lib/ritsu/utility/simple_io.rb
Overview
This class is lifted from rubigen’s SimpleLogger
Instance Attribute Summary collapse
-
#input ⇒ Object
Returns the value of attribute input.
-
#output ⇒ Object
Returns the value of attribute output.
-
#quiet ⇒ Object
Returns the value of attribute quiet.
Instance Method Summary collapse
- #ask_yes_no_all(message) ⇒ Object
- #indent(&block) ⇒ Object
-
#initialize(input = $stdin, output = $stdout) ⇒ SimpleIO
constructor
A new instance of SimpleIO.
- #log(status, message, &block) ⇒ Object
- #outdent ⇒ Object
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
#input ⇒ Object
Returns the value of attribute input.
6 7 8 |
# File 'lib/ritsu/utility/simple_io.rb', line 6 def input @input end |
#output ⇒ Object
Returns the value of attribute output.
7 8 9 |
# File 'lib/ritsu/utility/simple_io.rb', line 7 def output @output end |
#quiet ⇒ Object
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() @output.print("#{} (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() 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, , &block) @output.print("%12s %s%s\n" % [status, ' ' * @level, ]) unless quiet indent(&block) if block_given? end |
#outdent ⇒ Object
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 |