Module: Eco::Data::Files::ClassMethods

Included in:
Eco::Data::Files
Defined in:
lib/eco/data/files/helpers.rb

Instance Method Summary collapse

Instance Method Details

#copy_file(source_file, dest_file, time_stamp: false) ⇒ Object



97
98
99
100
# File 'lib/eco/data/files/helpers.rb', line 97

def copy_file(source_file, dest_file, time_stamp: false)
  dest_file = timestamp_file(dest_file) if time_stamp
  File.write(dest_file, File.read(source_file))
end

#create_directory(path, includes_file: false) ⇒ Object



53
54
55
# File 'lib/eco/data/files/helpers.rb', line 53

def create_directory(path, includes_file: false)
  Directory.create(path, includes_file: includes_file)
end

#csv_files(folder = ".", regexp: nil, older_than: nil) ⇒ Object



138
139
140
# File 'lib/eco/data/files/helpers.rb', line 138

def csv_files(folder = ".", regexp: nil, older_than: nil)
  folder_files(folder, "*.csv", regexp: regexp, older_than: older_than).sort
end

#dir_exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/eco/data/files/helpers.rb', line 84

def dir_exists?(path)
  Dir.exist?(path) || Dir.exist?(File.expand_path(path))
end

#encoding(path) ⇒ Object



115
116
117
# File 'lib/eco/data/files/helpers.rb', line 115

def encoding(path)
  has_bom?(path) ? "bom" : "utf-8"
end

#file_basename(fullname) ⇒ Object



67
68
69
# File 'lib/eco/data/files/helpers.rb', line 67

def file_basename(fullname)
  File.basename(fullname, File.extname(fullname))
end

#file_empty?(path) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
105
# File 'lib/eco/data/files/helpers.rb', line 102

def file_empty?(path)
  return true if !File.file?(path)
  File.zero?(path)
end

#file_exists?(file) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
# File 'lib/eco/data/files/helpers.rb', line 79

def file_exists?(file)
  return false if !file
  return File.exists?(file) || File.exists?(File.expand_path(file))
end

#file_fullpath(fullname) ⇒ Object



75
76
77
# File 'lib/eco/data/files/helpers.rb', line 75

def file_fullpath(fullname)
  file_path(File.expand_path(fullname))
end

#file_name(fullname) ⇒ Object



63
64
65
# File 'lib/eco/data/files/helpers.rb', line 63

def file_name(fullname)
  File.basename(fullname)
end

#file_path(fullname) ⇒ Object



71
72
73
# File 'lib/eco/data/files/helpers.rb', line 71

def file_path(fullname)
  File.dirname(fullname)
end

#folder_files(folder = ".", pattern = "*", regexp: nil, older_than: nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/eco/data/files/helpers.rb', line 125

def folder_files(folder = ".", pattern = "*", regexp: nil, older_than: nil)
  target = File.join(File.expand_path(folder), pattern)
  Dir[target].tap do |dir_files|
    dir_files.select! {|f| File.file?(f)}
    if older_than
      dir_files.select! {|f| File.mtime(f) < (Time.now - (60*60*24*older_than))}
    end
    if regexp && regexp.is_a?(Regexp)
      dir_files.select! {|f| File.basename(f).match(regexp)}
    end
  end.sort
end

#has_bom?(path) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
110
111
112
113
# File 'lib/eco/data/files/helpers.rb', line 107

def has_bom?(path)
  return false if !path || file_empty?(path)
  File.open(path, "rb") do |f|
    bytes = f.read(3)
    return bytes.unpack("C*") == [239, 187, 191]
  end
end

#script_subfolderObject



119
120
121
122
123
# File 'lib/eco/data/files/helpers.rb', line 119

def script_subfolder
  basename = File.basename($0, File.extname($0))
  path = File.dirname($0)
  File.join(path, basename)
end

#split(path) ⇒ Object



57
58
59
60
61
# File 'lib/eco/data/files/helpers.rb', line 57

def split(path)
  dir_path, file = File.split(path)
  dir_path = dir_path.gsub(File::SEPARATOR, File::ALT_SEPARATOR || File::SEPARATOR)
  dir_path.split(File::ALT_SEPARATOR || File::SEPARATOR).push(file)
end

#timestamp(timestamp_pattern = DEFAULT_TIMESTAMP_PATTERN) ⇒ Object



88
89
90
# File 'lib/eco/data/files/helpers.rb', line 88

def timestamp(timestamp_pattern = DEFAULT_TIMESTAMP_PATTERN)
  Time.now.strftime(timestamp_pattern)
end

#timestamp_file(filename, timestamp_pattern = DEFAULT_TIMESTAMP_PATTERN) ⇒ Object



92
93
94
95
# File 'lib/eco/data/files/helpers.rb', line 92

def timestamp_file(filename, timestamp_pattern = DEFAULT_TIMESTAMP_PATTERN)
  file_pattern = Eco::Data::Files::FilePattern.new(filename)
  file_pattern.resolve(start: timestamp(timestamp_pattern) + '_')
end