Sha256: 87268eab40f42f8ea4fe670016adcdd6a47430826a8f8d974a9d2449178d300e

Contents?: true

Size: 926 Bytes

Versions: 9

Compression:

Stored size: 926 Bytes

Contents

require 'tmpdir'
require 'fileutils'

class TraceTree
  class << self
    def tmp
      Dir.tmpdir
    end
  end

  class TmpFile

    DefaultName = 'trace_tree.html'

    def initialize path
      path = recognize_dir path
      @tmp = custom path
    end

    def puts *content
      File.open @tmp, 'a' do |f|
        f.puts *content
      end
    end

    def path
      @tmp
    end

    private

    def recognize_dir path
      case path
      when true
        DefaultName
      when String
        path.split '/'
      else
        path
      end
    end

    def custom path
      path = Array(path).map(&:to_s)
      path[-1] = time + path[-1]
      path = [Dir.tmpdir] + path
      ensure_parent path
      File.join *path
    end

    def time
      Time.now.strftime '%Y%m%d_%H%M%S_%L_'
    end

    def ensure_parent path_arr
      dir = path_arr[0..-2]
      FileUtils.mkdir_p File.join *dir
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
trace_tree-0.2.21 lib/trace_tree/tmp_file.rb
trace_tree-0.2.20 lib/trace_tree/tmp_file.rb
trace_tree-0.2.19 lib/trace_tree/tmp_file.rb
trace_tree-0.2.18 lib/trace_tree/tmp_file.rb
trace_tree-0.2.17 lib/trace_tree/tmp_file.rb
trace_tree-0.2.16 lib/trace_tree/tmp_file.rb
trace_tree-0.2.15 lib/trace_tree/tmp_file.rb
trace_tree-0.2.14 lib/trace_tree/tmp_file.rb
trace_tree-0.2.13 lib/trace_tree/tmp_file.rb