Class: Vedeu::Renderers::File

Inherits:
Object
  • Object
show all
Includes:
Options
Defined in:
lib/vedeu/output/renderers/file.rb

Overview

Writes the given output to a file.

Direct Known Subclasses

HTML, JSON

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Vedeu::Renderers::File

Returns a new instance of Vedeu::Renderers::File.

Options Hash (options):

  • filename (String)

    Provide a filename for the output. Defaults to ‘out’.

  • timestamp (Boolean)

    Append a timestamp to the filename.

  • write_file (Boolean)

    Whether to write the file to the given filename.



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

def initialize(options = {})
  @options = options || {}
end

Instance Attribute Details

#optionsHash<Symbol => void> (private) Originally defined in module Options

Combines the options provided at instantiation with the default values.

Instance Method Details

#compress?Boolean Originally defined in module Options

#defaultsHash (private)

Returns the default options/attributes for this class.



88
89
90
91
92
93
94
95
# File 'lib/vedeu/output/renderers/file.rb', line 88

def defaults
  {
    compression: Vedeu::Configuration.compression?,
    filename:    'out',
    timestamp:   false,
    write_file:  true,
  }
end

#filenameString (private)

Return the filename given in the options, (or use the default), and append a timestamp if the :timestamp option was set to true.



60
61
62
# File 'lib/vedeu/output/renderers/file.rb', line 60

def filename
  options[:filename] + timestamp
end

#out(output) ⇒ String (private)

Compresses the output depending on configuration.



45
46
47
48
49
50
51
52
53
# File 'lib/vedeu/output/renderers/file.rb', line 45

def out(output)
  if compress?
    Vedeu::Output::Compressor.render(output, options)

  else
    output

  end
end

#render(output = '', opts = {}) ⇒ String Also known as: clear

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



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

def render(output = '', opts = {})
  @options = options.merge!(opts)

  ::File.write(filename, out(output)) if write_file?

  out(output)
end

#timestampFloat (private)

Return a timestamp for use as part of the filename if the :timestamp option was set to true, otherwise an empty string.



68
69
70
71
72
73
74
75
76
# File 'lib/vedeu/output/renderers/file.rb', line 68

def timestamp
  if options[:timestamp]
    "_#{Vedeu.clock_time}".freeze

  else
    ''.freeze

  end
end

#write_file?Boolean (private)

Returns a boolean indicating whether a file should be written.



81
82
83
# File 'lib/vedeu/output/renderers/file.rb', line 81

def write_file?
  options[:write_file]
end