Class: Vedeu::Output::Compressor Private
- Inherits:
-
Object
- Object
- Vedeu::Output::Compressor
- Includes:
- Common, Renderers::Options
- Defined in:
- lib/vedeu/output/compressor.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
During the conversion of a Vedeu::Cells::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
- #options ⇒ Hash<Symbol => void> included from Renderers::Options
- #output ⇒ Array<Array<Vedeu::Cells::Char>> readonly protected private
Class Method Summary collapse
Instance Method Summary collapse
-
#absent?(variable) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether a variable is nil or empty.
-
#become(klass, attributes) ⇒ Class
included
from Common
private
Converts one class into another.
-
#boolean(value) ⇒ Boolean
included
from Common
private
Returns a boolean indicating the value was a boolean.
-
#boolean?(value) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether the value is a Boolean.
- #cached ⇒ Array<void>|String private private
- #clear ⇒ void included from Renderers::Options
-
#colour_for(char) ⇒ String
private
private
Compress by not repeatedly sending the same colours for each character which has the same colours as the last character output.
- #compress ⇒ String private private
-
#compression ⇒ String
included
from Renderers::Options
private
Compresses the output depending on configuration.
-
#compression? ⇒ Boolean
included
from Renderers::Options
private
Returns a boolean indicating whether the content should be compressed if compression is available.
- #content ⇒ Array private private
- #default_template ⇒ String included from Renderers::Options private
-
#defaults ⇒ Hash<Symbol => void>
included
from Renderers::Options
private
The default values for a new instance of this class.
- #end_row_tag ⇒ String included from Renderers::Options private
- #end_tag ⇒ String included from Renderers::Options private
-
#escape?(value) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether the value is an escape sequence object (e.g. Cells::Escape.).
-
#falsy?(value) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether the value should be considered false.
-
#filename ⇒ String
included
from Renderers::Options
private
Return the filename given in the options, (or use the default), and append a timestamp if the :timestamp option was set to true.
-
#hash?(value) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether the value is a Hash.
-
#initialize(output, options = {}) ⇒ Vedeu::Output::Compressor
constructor
private
Returns a new instance of Vedeu::Output::Compressor.
-
#line_model? ⇒ Boolean
included
from Common
private
Returns a boolean indicating the model is a Views::Line.
-
#numeric?(value) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether the value is a Fixnum.
- #output? ⇒ Boolean included from Renderers::Options private
-
#position_for(char) ⇒ String
private
private
Compress by not repeatedly sending a position when only the x coordinate has changed; i.e.
-
#present?(variable) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether a variable has a useful value.
- #render ⇒ String private
-
#snake_case(klass) ⇒ String
included
from Common
private
Converts a class name to a lowercase snake case string.
- #start_row_tag ⇒ String included from Renderers::Options private
- #start_tag ⇒ String included from Renderers::Options private
-
#stream_model? ⇒ Boolean
included
from Common
private
Returns a boolean indicating the model is a Views::Stream.
-
#string?(value) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether the value is a Fixnum.
-
#style_for(char) ⇒ String
private
private
Compress by not repeatedly sending the same style(s) for each character which has the same style(s) as the last character output.
- #template ⇒ String included from Renderers::Options private
-
#timestamp ⇒ String
included
from Renderers::Options
private
Return a timestamp for use as part of the filename if the :timestamp option was set to true, otherwise an empty string.
- #timestamp? ⇒ Boolean included from Renderers::Options private
-
#truthy?(value) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether the value should be considered true.
- #uncompress ⇒ String private private
-
#view_model? ⇒ Boolean
included
from Common
private
Returns a boolean indicating the model is a Views::View.
- #write ⇒ Object included from Renderers::Options
-
#write_file ⇒ String
included
from Renderers::Options
private
Render the output (either content or clearing) to a file.
-
#write_file? ⇒ Boolean
included
from Renderers::Options
private
Returns a boolean indicating whether a file should be written.
Constructor Details
#initialize(output, options = {}) ⇒ Vedeu::Output::Compressor
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Vedeu::Output::Compressor.
31 32 33 34 35 36 |
# File 'lib/vedeu/output/compressor.rb', line 31 def initialize(output, = {}) @output = output @options = @colour = '' @style = '' end |
Instance Attribute Details
#options ⇒ Hash<Symbol => void> Originally defined in module Renderers::Options
#output ⇒ Array<Array<Vedeu::Cells::Char>> (readonly, protected)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
52 53 54 |
# File 'lib/vedeu/output/compressor.rb', line 52 def output @output end |
Class Method Details
.render(output, options = {}) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
21 22 23 |
# File 'lib/vedeu/output/compressor.rb', line 21 def self.render(output, = {}) new(output, ).render end |
Instance Method Details
#absent?(variable) ⇒ Boolean Originally defined in module Common
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a boolean indicating whether a variable is nil or empty.
#become(klass, attributes) ⇒ Class Originally defined in module Common
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Converts one class into another.
#boolean(value) ⇒ Boolean Originally defined in module Common
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a boolean indicating the value was a boolean.
#boolean?(value) ⇒ Boolean Originally defined in module Common
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a boolean indicating whether the value is a Boolean.
#cached ⇒ Array<void>|String (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/vedeu/output/compressor.rb', line 68 def cached cached_original = Vedeu::Output::CompressorCache.read(:original) cached_compressed = Vedeu::Output::CompressorCache.read(:compressed) if content.size == cached_original.size && content == cached_original cached_compressed else Vedeu::Output::CompressorCache.write(:original, content) Vedeu::Output::CompressorCache.write(:compressed, compress) compress end end |
#clear ⇒ void Originally defined in module Renderers::Options
This method returns an undefined value.
#colour_for(char) ⇒ String (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Compress by not repeatedly sending the same colours for each character which has the same colours as the last character output.
146 147 148 149 150 151 |
# File 'lib/vedeu/output/compressor.rb', line 146 def colour_for(char) return '' if char.colour == @colour @colour = char.colour @colour.to_s end |
#compress ⇒ String (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/vedeu/output/compressor.rb', line 84 def compress = "Compression for #{content.size} objects" @same = '' @compress ||= Vedeu.timer() do out = content.map do |cell| rendered = [ # position_for(cell), cell.position.to_s, colour_for(cell), style_for(cell), cell.value, ].join if @same == rendered next else @same = rendered @same end end.join Vedeu.log(type: :compress, message: "#{} -> #{out.size} characters") out end end |
#compression ⇒ String (private) Originally defined in module Renderers::Options
Compresses the output depending on configuration.
#compression? ⇒ Boolean (private) Originally defined in module Renderers::Options
Returns a boolean indicating whether the content should be compressed if compression is available.
#content ⇒ Array (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
57 58 59 60 61 62 63 64 65 |
# File 'lib/vedeu/output/compressor.rb', line 57 def content @content ||= if present?(output) output.content.reject(&:cell?) else [] end end |
#default_template ⇒ String (private) Originally defined in module Renderers::Options
#defaults ⇒ Hash<Symbol => void> (private) Originally defined in module Renderers::Options
The default values for a new instance of this class.
#end_row_tag ⇒ String (private) Originally defined in module Renderers::Options
#end_tag ⇒ String (private) Originally defined in module Renderers::Options
#escape?(value) ⇒ Boolean Originally defined in module Common
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a boolean indicating whether the value is an escape sequence object (e.g. Vedeu::Cells::Escape.)
#falsy?(value) ⇒ Boolean Originally defined in module Common
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a boolean indicating whether the value should be considered false.
#filename ⇒ String (private) Originally defined in module Renderers::Options
Return the filename given in the options, (or use the default), and append a timestamp if the :timestamp option was set to true.
#hash?(value) ⇒ Boolean Originally defined in module Common
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a boolean indicating whether the value is a Hash.
#line_model? ⇒ Boolean Originally defined in module Common
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a boolean indicating the model is a Views::Line.
#numeric?(value) ⇒ Boolean Originally defined in module Common
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a boolean indicating whether the value is a Fixnum.
#output? ⇒ Boolean (private) Originally defined in module Renderers::Options
#position_for(char) ⇒ String (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Compress by not repeatedly sending a position when only the x coordinate has changed; i.e. we are on the same line, just advancing a character.
132 133 134 135 136 137 138 |
# File 'lib/vedeu/output/compressor.rb', line 132 def position_for(char) return '' unless char.position return '' if char.position.y == @y @y = char.position.y char.position.to_s end |
#present?(variable) ⇒ Boolean Originally defined in module Common
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a boolean indicating whether a variable has a useful value.
#render ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
-
Takes approximately ~6ms for 2100 chars. (2015-11-25)
-
Takes approximately ~25ms for 2100 chars. (2015-07-25)
42 43 44 45 46 |
# File 'lib/vedeu/output/compressor.rb', line 42 def render return cached if compression? uncompress end |
#snake_case(klass) ⇒ String Originally defined in module Common
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Converts a class name to a lowercase snake case string.
#start_row_tag ⇒ String (private) Originally defined in module Renderers::Options
#start_tag ⇒ String (private) Originally defined in module Renderers::Options
#stream_model? ⇒ Boolean Originally defined in module Common
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a boolean indicating the model is a Views::Stream.
#string?(value) ⇒ Boolean Originally defined in module Common
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a boolean indicating whether the value is a Fixnum.
#style_for(char) ⇒ String (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Compress by not repeatedly sending the same style(s) for each character which has the same style(s) as the last character output.
159 160 161 162 163 164 |
# File 'lib/vedeu/output/compressor.rb', line 159 def style_for(char) return '' if char.style == @style @style = char.style @style.to_s end |
#template ⇒ String (private) Originally defined in module Renderers::Options
#timestamp ⇒ String (private) Originally defined in module Renderers::Options
Return a timestamp for use as part of the filename if the :timestamp option was set to true, otherwise an empty string.
#timestamp? ⇒ Boolean (private) Originally defined in module Renderers::Options
#truthy?(value) ⇒ Boolean Originally defined in module Common
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a boolean indicating whether the value should be considered true.
#uncompress ⇒ String (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
116 117 118 119 120 121 122 123 124 |
# File 'lib/vedeu/output/compressor.rb', line 116 def uncompress out = content.map(&:to_s).join Vedeu.log(type: :compress, message: "No compression: #{content.size} objects -> " \ "#{out.size} characters") out end |
#view_model? ⇒ Boolean Originally defined in module Common
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a boolean indicating the model is a Views::View.
#write ⇒ Object Originally defined in module Renderers::Options
#write_file ⇒ String (private) Originally defined in module Renderers::Options
Render the output (either content or clearing) to a file.
#write_file? ⇒ Boolean (private) Originally defined in module Renderers::Options
Returns a boolean indicating whether a file should be written.