Sha256: 078d850f9abc4dc52e0fd64a8a5c755024f05173513251833cde1349e482f75d

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

module Vedeu

  module Renderers

    # Converts a grid of {Vedeu::Char} objects into a stream of escape sequences
    # and content suitable for a terminal, and writes them to a file in the /tmp
    # directory.
    #
    # @api private
    class File

      # @param output [Array<Array<Vedeu::Char>>]
      # @param options [Hash]
      # @return [String]
      def self.render(output, options = {})
        new(output, options).render
      end

      # Returns a new instance of Vedeu::Renderers::File.
      #
      # @param output [Array<Array<Vedeu::Char>>]
      # @param options [Hash]
      # @return [Vedeu::Renderers::File]
      def initialize(output, options = {})
        @output  = output
        @options = options
      end

      # @return [String]
      def render
        ::File.open(path, 'w') { |f| f.write(parsed) }

        parsed
      end

      protected

      # @!attribute [r] output
      # @return [Array<Array<Vedeu::Char>>]
      attr_reader :output
      alias_method :parsed, :output

      private

      # @return [String]
      def path
        "/tmp/#{filename}"
      end

      # @return [String]
      def filename
        if timestamp?
          "out_#{timestamp}"

        else
          'out'

        end
      end

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

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

        false
      end

      # @return [Hash]
      def options
        defaults.merge!(@options)
      end

      # @return [Hash]
      def defaults
        {
          timestamp: false,
        }
      end

    end # File

  end # Renderers

end # Vedeu

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vedeu-0.4.40 lib/vedeu/output/renderers/file.rb