Class: TestBench::Output::Writer::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/test_bench/output/writer/buffer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contentsObject



7
8
9
# File 'lib/test_bench/output/writer/buffer.rb', line 7

def contents
  @contents ||= String.new
end

#limitObject

Returns the value of attribute limit.



5
6
7
# File 'lib/test_bench/output/writer/buffer.rb', line 5

def limit
  @limit
end

Class Method Details

.buildObject



12
13
14
15
16
# File 'lib/test_bench/output/writer/buffer.rb', line 12

def self.build
  instance = new
  instance.limit = 0
  instance
end

.configure(receiver, attr_name: nil) ⇒ Object



18
19
20
21
22
23
# File 'lib/test_bench/output/writer/buffer.rb', line 18

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

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

Instance Method Details

#flush(*devices) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/test_bench/output/writer/buffer.rb', line 47

def flush(*devices)
  devices.each do |device|
    device.write(contents)
  end

  contents.clear
end

#limit?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/test_bench/output/writer/buffer.rb', line 43

def limit?
  !limit.nil?
end

#receive(data) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/test_bench/output/writer/buffer.rb', line 25

def receive(data)
  bytes = data.bytesize

  if not limit.nil?
    final_size = contents.bytesize + bytes

    if final_size > limit
      bytes = limit - contents.bytesize

      data = data[0...bytes]
    end
  end

  contents << data

  bytes
end