Module: WrapIO

Defined in:
lib/wrapio.rb,
lib/wrapio/fake.rb,
lib/wrapio/capture.rb,
lib/wrapio/version.rb

Overview

A small module that allows performs both capturing output from STDOUT and faking input to STDIN.

Defined Under Namespace

Classes: Capture, Fake

Constant Summary collapse

VERSION =

The curent version

"0.0.1"
@@debug =

Returns true if debugging is enabled.

Returns:

  • (Boolean)

    true if debugging is enabled

false
@@delimiters =

Internal markers for debug logging

{
	:input => 'Fake#gets',
	:output => 'Capture#output'
}
@@default_input =

Returns the default input if not specified in WrapIO#of.

Returns:

  • (String, Array)

    the default input if not specified in WrapIO#of

''

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#debugBoolean

Returns true if debugging is enabled.

Returns:

  • (Boolean)

    true if debugging is enabled



16
# File 'lib/wrapio.rb', line 16

@@debug = false

#default_inputString, Array

Returns the default input if not specified in WrapIO#of.

Returns:

  • (String, Array)

    the default input if not specified in WrapIO#of



30
# File 'lib/wrapio.rb', line 30

@@default_input = ''

Class Method Details

.debugBoolean

Returns true if debugging is enabled.

Returns:

  • (Boolean)

    true if debugging is enabled



35
36
37
# File 'lib/wrapio.rb', line 35

def self.debug
	@@debug
end

.debug=(value) ⇒ Object

Enable or disables debugging

Parameters:

  • value (Boolean)

    true if debugging is enabled



43
44
45
# File 'lib/wrapio.rb', line 43

def self.debug=(value)
	@@debug = value
end

.default_inputString, Array

Returns the default input if not specified in WrapIO#of.

Returns:

  • (String, Array)

    the default input if not specified in WrapIO#of



50
51
52
# File 'lib/wrapio.rb', line 50

def self.default_input
	@@default_input
end

.default_input=(value) ⇒ Object

Set the default input if not specified in WrapIO#of

Parameters:

  • value (String, Array)

    the default input if not specified in WrapIO#of



58
59
60
# File 'lib/wrapio.rb', line 58

def self.default_input=(value)
	@@default_input = value
end

.log(data, type, index = nil) ⇒ Object

Used within module classes to output debug data

Parameters:

  • data (String)

    the string to output

  • type (Symbol)

    the type of data e.g. :input or :output

  • index (Integer) (defaults to: nil)

    optionally provide an index of the data



88
89
90
# File 'lib/wrapio.rb', line 88

def self.log(data, type, index=nil)
	puts delimit(data, type, index.to_s)
end

.of(input = nil) { ... } ⇒ String

The main action of the WrapIO module As the semantic reading of the module name and function implies. It wraps the input and output of the given block, making the input passable as a parameter and the output accessible as a return value.

Parameters:

  • input (String, Array<String>) (defaults to: nil)

    the input or array of inputs

Yields:

  • the block of code from which to wrap I/O

Returns:

  • (String)

    the captured output from STDOUT



72
73
74
75
76
77
78
79
# File 'lib/wrapio.rb', line 72

def self.of(input=nil)
	input = WrapIO.default_input.map{|i| i.to_s.dup} unless input
	Capture.output do
		Fake.input(input) do
			yield
		end
	end
end