Class: Vedeu::Output::Compressor

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, options = {}) ⇒ Vedeu::Output::Compressor

Returns a new instance of Vedeu::Output::Compressor.

Parameters:

Options Hash (options):

  • compression (Boolean)


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

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

Instance Attribute Details

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

Returns:



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

def output
  @output
end

Class Method Details

.render(output, options = {}) ⇒ String

Parameters:

Returns:

  • (String)


16
17
18
# File 'lib/vedeu/output/compressor.rb', line 16

def self.render(output, options = {})
  new(output, options).render
end

Instance Method Details

#absent?(variable) ⇒ Boolean Originally defined in module Common

Returns a boolean indicating whether a variable is nil or empty.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

#colour_for(char) ⇒ String (private)

Parameters:

Returns:

  • (String)


86
87
88
89
90
91
# File 'lib/vedeu/output/compressor.rb', line 86

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

  @colour = char.colour
  @colour.to_s
end

#compressString (private)

Returns:

  • (String)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/vedeu/output/compressor.rb', line 58

def compress
  Vedeu.timer('Compression'.freeze) 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

#contentArray (private)

Returns:

  • (Array)


51
52
53
54
55
# File 'lib/vedeu/output/compressor.rb', line 51

def content
  return [] if absent?(output)

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

#demodulize(klass) ⇒ String Originally defined in module Common

Removes the module part from the expression in the string.

Examples:

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

Parameters:

  • klass (Class|String)

Returns:

  • (String)

#present?(variable) ⇒ Boolean Originally defined in module Common

Returns a boolean indicating whether a variable has a useful value.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

#renderString

Note:

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

Returns:

  • (String)


36
37
38
39
40
# File 'lib/vedeu/output/compressor.rb', line 36

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

  uncompress
end

#snake_case(name) ⇒ String Originally defined in module Common

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"

Parameters:

  • name (String)

Returns:

  • (String)

#style_for(char) ⇒ String (private)

Parameters:

Returns:

  • (String)


95
96
97
98
99
100
# File 'lib/vedeu/output/compressor.rb', line 95

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

  @style = char.style
  @style.to_s
end

#uncompressString (private)

Returns:

  • (String)


74
75
76
77
78
79
80
81
82
# File 'lib/vedeu/output/compressor.rb', line 74

def uncompress
  out = ''

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

  out
end