Class: Pow::File

Inherits:
Base
  • Object
show all
Defined in:
lib/pow/file.rb

Overview

Pow object representing a file. Inherits from Pow::Base

Instance Attribute Summary

Attributes inherited from Base

#path

Instance Method Summary collapse

Methods inherited from Base

#/, #<=>, #==, #=~, #[], #accessed_at, #changed_at, #create_directory, #create_file, #directory?, #eql?, #exists?, #extension, #file?, #modified_at, #name, open, #parent, #permissions, #permissions=, #rename_to, #size, #to_s, working_directory

Constructor Details

#initialize(path, mode = "r+", &block) ⇒ File

:nodoc:



5
6
7
8
# File 'lib/pow/file.rb', line 5

def initialize(path, mode="r+", &block) #:nodoc:
  super
  open(mode, &block) if block_given?
end

Instance Method Details

#copy_to(dest) ⇒ Object Also known as: cp



36
37
38
39
# File 'lib/pow/file.rb', line 36

def copy_to(dest)
  FileUtils.cp(path.to_s, dest.to_s)
  Pow(dest)
end

#copy_to!(dest) ⇒ Object Also known as: cp!

Will create the directory path if it does not already exist.



43
44
45
46
# File 'lib/pow/file.rb', line 43

def copy_to!(dest)
  Pow(dest).parent.create_directory
  copy_to(dest)
end

#create(&block) ⇒ Object

:nodoc:



14
15
16
# File 'lib/pow/file.rb', line 14

def create(&block) #:nodoc:
  create_file(&block)
end

#deleteObject



28
29
30
# File 'lib/pow/file.rb', line 28

def delete
  ::File.delete(path)
end

#empty?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/pow/file.rb', line 32

def empty?
  ::File.size(path) == 0
end

#move_to(dest) ⇒ Object Also known as: mv



49
50
51
52
53
# File 'lib/pow/file.rb', line 49

def move_to(dest)
  if FileUtils.mv(path.to_s, dest.to_s)
    self.path = dest.path
  end
end

#move_to!(dest) ⇒ Object Also known as: mv!

Will create the directory path if it does not already exist.



57
58
59
60
# File 'lib/pow/file.rb', line 57

def move_to!(dest)
  Pow(dest).parent.create_directory
  move_to(dest)
end

#open(mode = "r", &block) ⇒ Object

:nodoc:



10
11
12
# File 'lib/pow/file.rb', line 10

def open(mode="r", &block) #:nodoc:
  Kernel.open(path.to_s, mode, &block)
end

#read(length = nil, offset = nil) ⇒ Object

Opens the file, optionally seeks to the given offset, then returns length bytes (defaulting to the rest of the file). read ensures the file is closed before returning. Alias for IO.read



20
21
22
# File 'lib/pow/file.rb', line 20

def read(length=nil, offset=nil)
  ::File.read(path.to_s, length, offset)
end

#write(string) ⇒ Object



24
25
26
# File 'lib/pow/file.rb', line 24

def write(string)
  open("w") {|f| f.write string}
end