Class: Vedeu::Compressor

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/output/compressor.rb

Overview

During the conversion of a Vedeu::Char object into a string of escape sequences, this class removes multiple occurences of the same escape sequence, resulting in a smaller payload being sent to the renderer.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ Vedeu::Compressor

Parameters:



10
11
12
13
14
# File 'lib/vedeu/output/compressor.rb', line 10

def initialize(output)
  @output = output
  @colour = ''
  @style  = ''
end

Instance Attribute Details

#outputArray<Array<Vedeu::Char>> (readonly, protected)

Returns:



39
40
41
# File 'lib/vedeu/output/compressor.rb', line 39

def output
  @output
end

Instance Method Details

#colour_for(char) ⇒ String (private)

Parameters:

Returns:

  • (String)


45
46
47
48
49
50
# File 'lib/vedeu/output/compressor.rb', line 45

def colour_for(char)
  return '' if char.colour == @colour

  @colour = char.colour
  @colour.to_s
end

#renderString

Note:

Takes approximately ~70ms for 2100 chars. (2015-05-24)

Returns:

  • (String)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/vedeu/output/compressor.rb', line 18

def render
  Array(output).flatten.map do |char|
    if char.is_a?(Vedeu::Char)
      out = ''
      out << char.position.to_s
      out << colour_for(char)
      out << style_for(char)
      out << char.value
      out

    else
      char.to_s

    end
  end.join
end

#style_for(char) ⇒ String (private)

Parameters:

Returns:

  • (String)


54
55
56
57
58
59
# File 'lib/vedeu/output/compressor.rb', line 54

def style_for(char)
  return '' if char.style == @style

  @style = char.style
  @style.to_s
end