Class: Vedeu::Output::Compressor Private

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

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.

Options Hash (options):

  • compression (Boolean)


28
29
30
31
32
33
# File 'lib/vedeu/output/compressor.rb', line 28

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

Instance Attribute Details

#outputArray<Array<Vedeu::Views::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.



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

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.



18
19
20
# File 'lib/vedeu/output/compressor.rb', line 18

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.

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



113
114
115
116
117
118
# File 'lib/vedeu/output/compressor.rb', line 113

def colour_for(char)
  return ''.freeze 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.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/vedeu/output/compressor.rb', line 60

def compress
  Vedeu.timer("Compression for #{content.size} objects".freeze) do
    out = ''

    content.each do |cell|
      out << position_for(cell)
      out << colour_for(cell)
      out << style_for(cell)
      out << cell.value
    end

    Vedeu.log(type:    :output,
              message: "Compression: #{content.size} objects -> " \
                       "#{out.size} characters".freeze)

    out
  end
end

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



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

def content
  return [] if absent?(output)

  @content ||= output.content.reject(&:cell?)
end

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

Removes the module part from the expression in the string.

Examples:

demodulize('Vedeu::SomeModule::SomeClass') # => "SomeClass"

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



100
101
102
103
104
105
# File 'lib/vedeu/output/compressor.rb', line 100

def position_for(char)
  return ''.freeze 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.

#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 ~25ms for 2100 chars. (2015-07-25)



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

def render
  return compress if Vedeu::Configuration.compression?

  uncompress
end

#snake_case(name) ⇒ 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"

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



126
127
128
129
130
131
# File 'lib/vedeu/output/compressor.rb', line 126

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

  @style = char.style
  @style.to_s
end

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



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/vedeu/output/compressor.rb', line 80

def uncompress
  out = ''

  content.each do |cell|
    out << cell.to_s
  end

  Vedeu.log(type:    :output,
            message: "No compression: #{content.size} objects -> " \
                     "#{out.size} characters".freeze)

  out
end