Class: TestBench::Output::Device

Inherits:
Object
  • Object
show all
Defined in:
lib/test_bench/output/device.rb,
lib/test_bench/output/device/null.rb,
lib/test_bench/output/device/substitute.rb

Defined Under Namespace

Modules: Defaults, Substitute Classes: Null

Constant Summary collapse

Error =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#raw_deviceObject



6
7
8
# File 'lib/test_bench/output/device.rb', line 6

def raw_device
  @raw_device ||= Substitute.build
end

Class Method Details

.build(target_device = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/test_bench/output/device.rb', line 11

def self.build(target_device=nil)
  target_device ||= Defaults.target_device

  raw_device = raw_device(target_device)

  instance = new
  instance.raw_device = raw_device
  instance
end

.configure(receiver, device: nil, attr_name: nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/test_bench/output/device.rb', line 21

def self.configure(receiver, device: nil, attr_name: nil)
  attr_name ||= :device

  if device.instance_of?(self)
    instance = device
  else
    target_device = device

    instance = build(target_device)
  end

  receiver.public_send(:"#{attr_name}=", instance)
end

.raw_device(target_device) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/test_bench/output/device.rb', line 35

def self.raw_device(target_device)
  case target_device
  in :stdout
    STDOUT
  in :stderr
    STDERR
  in :null
    Null.instance
  in Symbol
    raise Error, "Incorrect output device: #{target_device.inspect}"
  else
    target_device
  end
end

Instance Method Details

#tty?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/test_bench/output/device.rb', line 56

def tty?
  raw_device.tty?
end

#write(text) ⇒ Object



50
51
52
53
54
# File 'lib/test_bench/output/device.rb', line 50

def write(text)
  raw_device.write(text)

  text.bytesize
end