Class: Vedeu::Output::Compressor Private

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

Options Hash (options):



31
32
33
34
35
36
# File 'lib/vedeu/output/compressor.rb', line 31

def initialize(output, options = {})
  @output  = output
  @options = options
  @colour  = ''
  @style   = ''
end

Instance Attribute Details

#optionsHash<Symbol => void> Originally defined in module Renderers::Options

Returns:

  • (Hash<Symbol => void>)

#outputArray<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.

Returns:



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.

Parameters:

Returns:

  • (String)


21
22
23
# File 'lib/vedeu/output/compressor.rb', line 21

def self.render(output, options = {})
  new(output, options).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.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

#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.

Parameters:

  • klass (Class)

    The class to become an instance of.

  • attributes (Hash)

    The attributes of klass.

Returns:

  • (Class)

    Returns a new instance of klass.

#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.

Parameters:

  • value (void)

Returns:

#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.

Parameters:

Returns:

#cachedArray<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.

Returns:

  • (Array<void>|String)


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

#clearvoid 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.

Parameters:

Returns:

  • (String)


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

#compressString (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.

Returns:

  • (String)


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
  message = "Compression for #{content.size} objects"

  @same = ''
  @compress ||= Vedeu.timer(message) 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: "#{message} -> #{out.size} characters")

    out
  end
end

#compressionString (private) Originally defined in module Renderers::Options

Compresses the output depending on configuration.

Returns:

  • (String)

#compression?Boolean (private) Originally defined in module Renderers::Options

Returns a boolean indicating whether the content should be compressed if compression is available.

Returns:

#contentArray (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.

Returns:

  • (Array)


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_templateString (private) Originally defined in module Renderers::Options

Returns:

  • (String)

#defaultsHash<Symbol => void> (private) Originally defined in module Renderers::Options

The default values for a new instance of this class.

Returns:

  • (Hash<Symbol => void>)

#end_row_tagString (private) Originally defined in module Renderers::Options

Returns:

  • (String)

#end_tagString (private) Originally defined in module Renderers::Options

Returns:

  • (String)

#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.)

Returns:

#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.

Parameters:

  • value (void)

Returns:

#filenameString (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.

Returns:

  • (String)

#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.

Parameters:

  • value (Hash|void)

Returns:

#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.

Returns:

#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.

Parameters:

  • value (Fixnum|void)

Returns:

#output?Boolean (private) Originally defined in module Renderers::Options

Returns:

#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.

Parameters:

Returns:

  • (String)


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.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

#renderString

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.

Note:
  • Takes approximately ~6ms for 2100 chars. (2015-11-25)

  • Takes approximately ~25ms for 2100 chars. (2015-07-25)

Returns:

  • (String)


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.

Examples:

snake_case(MyClassName) # => "my_class_name"
snake_case(NameSpaced::ClassName)
# => "name_spaced/class_name"

snake_case('MyClassName') # => "my_class_name"
snake_case(NameSpaced::ClassName)
# => "name_spaced/class_name"

Parameters:

  • klass (Module|Class|String)

Returns:

  • (String)

#start_row_tagString (private) Originally defined in module Renderers::Options

Returns:

  • (String)

#start_tagString (private) Originally defined in module Renderers::Options

Returns:

  • (String)

#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.

Returns:

#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.

Parameters:

  • value (String|void)

Returns:

#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.

Parameters:

Returns:

  • (String)


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

#templateString (private) Originally defined in module Renderers::Options

Returns:

  • (String)

#timestampString (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.

Returns:

  • (String)

#timestamp?Boolean (private) Originally defined in module Renderers::Options

Returns:

#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.

Parameters:

  • value (void)

Returns:

#uncompressString (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.

Returns:

  • (String)


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.

Returns:

#writeObject Originally defined in module Renderers::Options

Raises:

  • (Vedeu::Error::NotImplemented)

    When a subclass of the current class actually implements the method. Usually an indicator that the subclass should be used instead of the current class.

#write_fileString (private) Originally defined in module Renderers::Options

Render the output (either content or clearing) to a file.

Returns:

  • (String)

#write_file?Boolean (private) Originally defined in module Renderers::Options

Returns a boolean indicating whether a file should be written.

Returns: