Class: Vedeu::Output::Compressor
- Inherits:
-
Object
- Object
- Vedeu::Output::Compressor
- Defined in:
- lib/vedeu/output/compressor.rb
Overview
During the conversion of a Vedeu::Views::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
- #output ⇒ Array<Array<Vedeu::Views::Char>> readonly protected
Class Method Summary collapse
Instance Method Summary collapse
- #colour_for(char) ⇒ String private
- #compress ⇒ String private
- #content ⇒ Object private
-
#initialize(output) ⇒ Vedeu::Output::Compressor
constructor
Returns a new instance of Vedeu::Output::Compressor.
- #render ⇒ String
- #style_for(char) ⇒ String private
- #uncompress ⇒ String private
Constructor Details
#initialize(output) ⇒ Vedeu::Output::Compressor
Returns a new instance of Vedeu::Output::Compressor.
22 23 24 25 26 |
# File 'lib/vedeu/output/compressor.rb', line 22 def initialize(output) @output = output @colour = '' @style = '' end |
Instance Attribute Details
#output ⇒ Array<Array<Vedeu::Views::Char>> (readonly, protected)
41 42 43 |
# File 'lib/vedeu/output/compressor.rb', line 41 def output @output end |
Class Method Details
.render(output) ⇒ String
14 15 16 |
# File 'lib/vedeu/output/compressor.rb', line 14 def self.render(output) new(output).render end |
Instance Method Details
#colour_for(char) ⇒ String (private)
78 79 80 81 82 83 |
# File 'lib/vedeu/output/compressor.rb', line 78 def colour_for(char) return '' if char.colour == @colour @colour = char.colour @colour.to_s end |
#compress ⇒ String (private)
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/vedeu/output/compressor.rb', line 52 def compress Vedeu.timer('Compression') do out = '' content.each do |cell| out << cell.position.to_s out << colour_for(cell) out << style_for(cell) out << cell.value end out end end |
#content ⇒ Object (private)
45 46 47 48 49 |
# File 'lib/vedeu/output/compressor.rb', line 45 def content return [] if output.nil? || output.empty? output.content.delete_if { |cell| cell.is_a?(Vedeu::Models::Cell) } end |
#render ⇒ String
Note:
Takes approximately ~25ms for 2100 chars. (2015-07-25)
31 32 33 34 35 |
# File 'lib/vedeu/output/compressor.rb', line 31 def render return compress if Vedeu::Configuration.compression? uncompress end |
#style_for(char) ⇒ String (private)
87 88 89 90 91 92 |
# File 'lib/vedeu/output/compressor.rb', line 87 def style_for(char) return '' if char.style == @style @style = char.style @style.to_s end |
#uncompress ⇒ String (private)
68 69 70 71 72 73 74 |
# File 'lib/vedeu/output/compressor.rb', line 68 def uncompress out = '' content.each { |cell| out << cell.to_s } out end |