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.

Parameters:

  • options (Hash) (defaults to: {})

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.

Returns:

  • (Hash<Symbol => void>)

Instance Method Details

#clear(output = '', opts = {}) ⇒ String

Render a cleared output.

Parameters:

Returns:

  • (String)


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

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

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

  out(output)
end

#compress?Boolean Originally defined in module Options

Returns:

  • (Boolean)

#defaultsHash (private)

Returns the default options/attributes for this class.

Returns:

  • (Hash)


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

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

#filenameString (private)

Returns:

  • (String)


66
67
68
# File 'lib/vedeu/output/renderers/file.rb', line 66

def filename
  options[:filename] + timestamp
end

#out(output) ⇒ String (private)

Compresses the output depending on configuration.

Parameters:

Returns:

  • (String)


55
56
57
58
59
60
61
62
63
# File 'lib/vedeu/output/renderers/file.rb', line 55

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

  else
    output

  end
end

#render(output = '', opts = {}) ⇒ String

Parameters:

Returns:

  • (String)


41
42
43
44
45
46
47
# File 'lib/vedeu/output/renderers/file.rb', line 41

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

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

  out(output)
end

#timestampFloat (private)

Returns:

  • (Float)


71
72
73
74
75
76
77
78
79
# File 'lib/vedeu/output/renderers/file.rb', line 71

def timestamp
  if options[:timestamp]
    "_#{Time.now.to_f}".freeze

  else
    ''.freeze

  end
end

#write_file?Boolean (private)

Returns:

  • (Boolean)


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

def write_file?
  options[:write_file]
end