Sha256: 56ff38bde0bd8d16868d6acc3dbe1377d5c01869b2e95729874490618c5aeadc

Contents?: true

Size: 1.58 KB

Versions: 14

Compression:

Stored size: 1.58 KB

Contents

module Vedeu

  module Renderers

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

      # 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.
      # @return [Vedeu::Renderers::File]
      def initialize(options = {})
        @options = options || {}
      end

      # @param output [Array<Array<Vedeu::Char>>]
      # @return [String]
      def render(output)
        ::File.write(filename, output) if write_file?

        output
      end

      private

      # @return [String]
      def filename
        if timestamp?
          "#{options[:filename]}_#{timestamp}"

        else
          options[:filename]

        end
      end

      # @return [Float]
      def timestamp
        @timestamp ||= Time.now.to_f
      end

      # @return [Boolean]
      def timestamp?
        return true if options[:timestamp]

        false
      end

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

      # Combines the options provided at instantiation with the default values.
      #
      # @return [Hash]
      def options
        defaults.merge!(@options)
      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

14 entries across 14 versions & 1 rubygems

Version Path
vedeu-0.5.4 lib/vedeu/output/renderers/file.rb
vedeu-0.5.3 lib/vedeu/output/renderers/file.rb
vedeu-0.5.2 lib/vedeu/output/renderers/file.rb
vedeu-0.5.1 lib/vedeu/output/renderers/file.rb
vedeu-0.5.0 lib/vedeu/output/renderers/file.rb
vedeu-0.4.65 lib/vedeu/output/renderers/file.rb
vedeu-0.4.64 lib/vedeu/output/renderers/file.rb
vedeu-0.4.63 lib/vedeu/output/renderers/file.rb
vedeu-0.4.62 lib/vedeu/output/renderers/file.rb
vedeu-0.4.61 lib/vedeu/output/renderers/file.rb
vedeu-0.4.60 lib/vedeu/output/renderers/file.rb
vedeu-0.4.59 lib/vedeu/output/renderers/file.rb
vedeu-0.4.58 lib/vedeu/output/renderers/file.rb
vedeu-0.4.57 lib/vedeu/output/renderers/file.rb