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



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

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



16
17
18
# File 'lib/eco/data/files/helpers.rb', line 16

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

#dir_exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#encoding(path) ⇒ Object



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

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

#file_basename(fullname) ⇒ Object



30
31
32
# File 'lib/eco/data/files/helpers.rb', line 30

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

#file_empty?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#file_exists?(file) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/eco/data/files/helpers.rb', line 42

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

#file_fullpath(fullname) ⇒ Object



38
39
40
# File 'lib/eco/data/files/helpers.rb', line 38

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

#file_name(fullname) ⇒ Object



26
27
28
# File 'lib/eco/data/files/helpers.rb', line 26

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

#file_path(fullname) ⇒ Object



34
35
36
# File 'lib/eco/data/files/helpers.rb', line 34

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

#has_bom?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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

#split(path) ⇒ Object



20
21
22
23
24
# File 'lib/eco/data/files/helpers.rb', line 20

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



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

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

#timestamp_file(filename, timestamp_pattern = DEFAULT_TIMESTAMP_PATTERN) ⇒ Object



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

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