Sha256: ea1d462e6148da9320f9291b5a5d6c3bf43ba1295325b9adb082d38a61439b0e

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

module Vedeu

  module Renderers

    # Writes the given output to a file.
    #
    class File

      include Vedeu::Renderers::RendererOptions

      # Returns a new instance of Vedeu::Renderers::File.
      #
      # @param options [Hash]
      # @option options filename [String] Provide a filename for the
      #   output. Defaults to 'out'.
      # @option options timestamp [Boolean] Append a timestamp to the
      #   filename.
      # @option options write_file [Boolean] Whether to write the file
      #   to the given filename.
      # @return [Vedeu::Renderers::File]
      def initialize(options = {})
        @options = options || {}
      end

      # Render a cleared output.
      #
      # @return [String]
      def clear(output = '')
        ::File.write(filename, output) if write_file?

        output
      end

      # @param output [Vedeu::Models::Page]
      # @return [String]
      def render(output)
        ::File.write(filename, output) if write_file?

        output
      end

      private

      # @return [String]
      def filename
        options[:filename] + "_#{timestamp}"
      end

      # @return [Float]
      def timestamp
        Time.now.to_f if options[:timestamp]
      end

      # @return [Boolean]
      def write_file?
        options[:write_file]
      end

      # Returns the default options/attributes for this class.
      #
      # @return [Hash]
      def defaults
        {
          filename:   'out',
          timestamp:  false,
          write_file: true,
        }
      end

    end # File

  end # Renderers

end # Vedeu

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vedeu-0.6.15 lib/vedeu/output/renderers/file.rb
vedeu-0.6.14 lib/vedeu/output/renderers/file.rb
vedeu-0.6.13 lib/vedeu/output/renderers/file.rb