Class: TestBench::Output::Writer

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

Direct Known Subclasses

Substitute::Writer

Defined Under Namespace

Modules: Defaults, Style, Styling, Substitute Classes: Buffer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bufferObject



30
31
32
# File 'lib/test_bench/output/writer.rb', line 30

def buffer
  @buffer ||= Buffer.new
end

#column_sequenceObject



25
26
27
# File 'lib/test_bench/output/writer.rb', line 25

def column_sequence
  @column_sequence ||= 0
end

#deviceObject



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

def device
  @device ||= Device::Substitute.build
end

#digestObject



15
16
17
# File 'lib/test_bench/output/writer.rb', line 15

def digest
  @digest ||= Digest.new
end

#sequenceObject



20
21
22
# File 'lib/test_bench/output/writer.rb', line 20

def sequence
  @sequence ||= 0
end

#styling_policyObject Also known as: styling



9
10
11
# File 'lib/test_bench/output/writer.rb', line 9

def styling_policy
  @styling_policy ||= Styling.default
end

Class Method Details

.build(device = nil, styling: nil) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/test_bench/output/writer.rb', line 39

def self.build(device=nil, styling: nil)
  device ||= Defaults.device

  instance = new
  instance.device = device
  instance.styling_policy = styling
  instance.configure
  instance
end

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



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/test_bench/output/writer.rb', line 49

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

  if not writer.nil?
    instance = writer
  else
    instance = build(device, styling:)
  end

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

Instance Method Details

#configureObject



35
36
37
# File 'lib/test_bench/output/writer.rb', line 35

def configure
  Buffer.configure(self)
end

#current?(sequence) ⇒ Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/test_bench/output/writer.rb', line 150

def current?(sequence)
  sequence >= self.sequence
end

#flushObject



130
131
132
# File 'lib/test_bench/output/writer.rb', line 130

def flush
  buffer.flush(device)
end


97
98
99
100
101
102
103
# File 'lib/test_bench/output/writer.rb', line 97

def print(text)
  self.column_sequence += text.length

  write(text)

  self
end

#puts(text = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/test_bench/output/writer.rb', line 65

def puts(text=nil)
  if not text.nil?
    text = text.chomp

    print(text)
  end

  style(:reset)

  if tty?
    write("\e[0K")
  end

  write("\n")

  self.column_sequence = 0
end

#style(style, *additional_styles) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/test_bench/output/writer.rb', line 83

def style(style, *additional_styles)
  styles = [style, *additional_styles]

  control_codes = styles.map do |style|
    Style.control_code(style)
  end

  if styling?
    write("\e[#{control_codes.join(';')}m")
  end

  self
end

#styling?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/test_bench/output/writer.rb', line 154

def styling?
  Styling.styling?(styling_policy, tty?)
end

#syncObject



61
62
63
# File 'lib/test_bench/output/writer.rb', line 61

def sync
  @sync.nil? ? @sync = true : @sync
end

#sync=(sync) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/test_bench/output/writer.rb', line 134

def sync=(sync)
  @sync = sync

  if sync
    flush
  end
end

#tty?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/test_bench/output/writer.rb', line 126

def tty?
  device.tty?
end

#write(data) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/test_bench/output/writer.rb', line 105

def write(data)
  if sync
    bytes_written = write!(data)
  else
    bytes_written = buffer.receive(data)
  end

  self.sequence += bytes_written

  data = data[0...bytes_written]
  digest.update(data)

  bytes_written
end

#write!(data) ⇒ Object



120
121
122
123
124
# File 'lib/test_bench/output/writer.rb', line 120

def write!(data)
  device.write(data)

  data.bytesize
end

#written?(data = nil) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
145
146
147
148
# File 'lib/test_bench/output/writer.rb', line 142

def written?(data=nil)
  if data.nil?
    sequence > 0
  else
    digest.digest?(data)
  end
end