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

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

Constant Summary

Constants included from Encoding

Encoding::BOM_BYTES

Instance Attribute Summary

Attributes included from Language::AuxiliarLogger

#logger

Instance Method Summary collapse

Methods included from Encoding

#encoding, #get_file_content_with_encoding, #has_bom?, #remove_bom, #scoped_encoding

Methods included from Language::AuxiliarLogger

#log

Instance Method Details

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



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

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



46
47
48
# File 'lib/eco/data/files/helpers.rb', line 46

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

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



127
128
129
# File 'lib/eco/data/files/helpers.rb', line 127

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)


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

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

#file_basename(fullname) ⇒ Object



60
61
62
# File 'lib/eco/data/files/helpers.rb', line 60

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

#file_empty?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

def file_empty?(path)
  return true unless File.file?(path)

  File.empty?(path)
end

#file_exists?(file) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/eco/data/files/helpers.rb', line 72

def file_exists?(file)
  return false unless file

  File.exist?(file) || File.exist?(File.expand_path(file))
end

#file_fullpath(fullname) ⇒ Object



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

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

#file_name(fullname) ⇒ Object



56
57
58
# File 'lib/eco/data/files/helpers.rb', line 56

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

#file_path(fullname) ⇒ Object



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

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

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/eco/data/files/helpers.rb', line 108

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! do |f|
        File.mtime(f) < (Time.now - (60*60*24*older_than))
      end
    end

    if regexp.is_a?(Regexp)
      dir_files.select! do |f|
        File.basename(f).match(regexp)
      end
    end
  end.sort
end

#script_subfolderObject



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

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

#split(path) ⇒ Object



50
51
52
53
54
# File 'lib/eco/data/files/helpers.rb', line 50

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, date = Time.now) ⇒ Object



82
83
84
# File 'lib/eco/data/files/helpers.rb', line 82

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

#timestamp_file(filename, timestamp_pattern = DEFAULT_TIMESTAMP_PATTERN) ⇒ Object



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

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