Class: MoCo::FileUtil

Inherits:
File
  • Object
show all
Defined in:
lib/moco/file_util.rb

Class Method Summary collapse

Class Method Details

.normalized_extension(file) ⇒ Object



8
9
10
11
12
13
# File 'lib/moco/file_util.rb', line 8

def self.normalized_extension(file)
  file = file.to_s
  extension = extname(file)
  extension = basename(file) if extension.empty?
  extension.delete('.').strip
end

.relative_path(from_file, to_file) ⇒ Object



37
38
39
40
41
# File 'lib/moco/file_util.rb', line 37

def self.relative_path(from_file, to_file)
  from = Pathname.new(from_file)
  to = Pathname.new(to_file)
  to.relative_path_from(from.dirname).to_s
end

.replace_directory(file, dir) ⇒ Object



25
26
27
28
29
30
# File 'lib/moco/file_util.rb', line 25

def self.replace_directory(file, dir)
  return file unless dir
  file = basename(file)
  file = join(dir, file) unless dir.empty?
  file
end

.replace_extension(file, ext) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/moco/file_util.rb', line 15

def self.replace_extension(file, ext)
  return file unless ext
  file = file.chomp(extname(file))
  unless ext.empty?
    ext = '.' << ext unless ext.start_with?('.')
    file << ext unless file.end_with?(ext)
  end
  file
end

.short_path(path) ⇒ Object



32
33
34
35
# File 'lib/moco/file_util.rb', line 32

def self.short_path(path)
  home = File.expand_path('~')
  path.sub(home, '~')
end

.up_to_date?(file, compared_to_file) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/moco/file_util.rb', line 43

def self.up_to_date?(file, compared_to_file)
  exist?(file) && mtime(file) >= mtime(compared_to_file)
end

.write(file, text) ⇒ Object



47
48
49
50
# File 'lib/moco/file_util.rb', line 47

def self.write(file, text)
  FileUtils.makedirs(dirname(file))
  open(file, 'w') { |f| f.write(text) }
end