Class: File

Inherits:
Object show all
Defined in:
lib/cosmos/core_ext/file.rb

Overview

COSMOS specific additions to the Ruby File class

Direct Known Subclasses

Cosmos::BufferedFile

Constant Summary collapse

NON_ASCII_PRINTABLE =

Non printable ASCII characters

/[^\x21-\x7e\s]/

Class Method Summary collapse

Class Method Details

.build_timestamped_filename(tags = nil, extension = '.txt', time = Time.now.sys) ⇒ String

Builds a String for use in creating a file. The time is formatted as YYYY_MM_DD_HH_MM_SS. The tags and joined with an underscore and appended to the date before appending the extension.

For example:

File.build_timestamped_filename(['test','only'], '.bin', Time.now.sys)
# result is YYYY_MM_DD_HH_MM_SS_test_only.bin


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cosmos/core_ext/file.rb', line 46

def self.build_timestamped_filename(tags = nil, extension = '.txt', time = Time.now.sys)
  timestamp = sprintf("%04u_%02u_%02u_%02u_%02u_%02u", time.year, time.month, time.mday, time.hour, time.min, time.sec)
  tags ||= []
  tags.compact!
  combined_tags = tags.join("_")
  if combined_tags.length > 0
    filename = timestamp + "_" + combined_tags + extension
  else
    filename = timestamp + extension
  end
  return filename
end

.find_in_search_path(filename) ⇒ String



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cosmos/core_ext/file.rb', line 62

def self.find_in_search_path(filename)
  $:.each do |load_path|
    begin
      Find.find(load_path) do |path|
        Find.prune if path =~ /\.svn/
        return path if File.basename(path) == filename
      end
    rescue Errno::ENOENT
      # Ignore non-existent folders
      next
    end
  end
  return nil
end

.is_ascii?(filename) ⇒ Boolean



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cosmos/core_ext/file.rb', line 19

def self.is_ascii?(filename)
  return_value = true
  File.open(filename) do |file|
    while buf = file.read(1024)
      if buf =~ NON_ASCII_PRINTABLE
        return_value = false
        break
      end
    end
  end
  return return_value
end