Class: IronHammer::Utils::FileSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/iron_hammer/utils/file_system.rb

Class Method Summary collapse

Class Method Details

.copy!(source, destination) ⇒ Object



15
16
17
# File 'lib/iron_hammer/utils/file_system.rb', line 15

def self.copy! source, destination
  File.directory?(source) ? TheFiler::copy_directory!(source, destination) : FileSystem::copy_file!(source, destination)
end

.copy_directory!(source, destination) ⇒ Object



10
11
12
13
# File 'lib/iron_hammer/utils/file_system.rb', line 10

def self.copy_directory! source, destination
  raise(ArgumentError.new 'source must be a directory') unless File.directory?(source)
  FileUtils.cp_r source, destination
end

.copy_file!(source, destination) ⇒ Object



4
5
6
7
8
# File 'lib/iron_hammer/utils/file_system.rb', line 4

def self.copy_file! source, destination
  raise(ArgumentError.new 'source must be a file') if File.directory?(source)
  FileSystem::create_path! File.dirname(destination)
  FileUtils.cp source, destination
end

.create_path!(*path) ⇒ Object



32
33
34
# File 'lib/iron_hammer/utils/file_system.rb', line 32

def self.create_path! *path
  FileUtils.mkpath(File.join(*path)) unless path.nil? || path.empty? || File.exists?(File.join(*path))
end

.read_file(*path) ⇒ Object



24
25
26
# File 'lib/iron_hammer/utils/file_system.rb', line 24

def self.read_file *path
  File.open(File.join(*path), 'r') { |file| content = file.read } unless path.nil? || path.empty?
end

.read_lines(*path) ⇒ Object



28
29
30
# File 'lib/iron_hammer/utils/file_system.rb', line 28

def self.read_lines *path
  File.open(File.join(*path), 'r') { |file| file.readlines } unless path.nil? || path.empty?
end

.write!(params = {}) ⇒ Object



19
20
21
22
# File 'lib/iron_hammer/utils/file_system.rb', line 19

def self.write! params={}
  FileSystem::create_path! path = params[:path]
  File.open(File.join(path, name = params[:name]), 'w') { |file| file.write(content = params[:content]) }
end