Class: Cotta::CottaFile
- Inherits:
-
Object
- Object
- Cotta::CottaFile
- Defined in:
- lib/cotta/cotta_file.rb
Overview
This class represents a file
Instance Attribute Summary collapse
-
#factory ⇒ Object
readonly
factory with the backing system.
-
#path ⇒ Object
readonly
path of this file.
-
#stat ⇒ Object
readonly
stats of this file.
Class Method Summary collapse
-
.copy_io(input, output) ⇒ Object
copy from input handle to output handle.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#append(&block) ⇒ Object
Calls open with ‘a’ argument and make sure that the parent directory of the file exists.
-
#basename ⇒ Object
basename of this file.
-
#copy_to(target_file) ⇒ Object
copy this file to the target file.
-
#copy_to_path(target_path) ⇒ Object
copy this file to the target path.
-
#delete ⇒ Object
deletes the file.
-
#exists? ⇒ Boolean
returns true if this file exists.
-
#extname ⇒ Object
extension of this file, with ‘.’.
-
#extract(directory = nil) ⇒ Object
reads this file as an archive and extracts the content to the target directory.
-
#foreach ⇒ Object
reads the file and calls back for each line.
-
#initialize(factory, path) ⇒ CottaFile
constructor
creates an instance of file with the given factory and path.
- #inspect ⇒ Object
-
#load ⇒ Object
Loading the file full total.
-
#move_to(target_file) ⇒ Object
move this file to the target file.
-
#move_to_path(target_path) ⇒ Object
move this file to the target path.
-
#name ⇒ Object
name of this file.
-
#older_than?(file) ⇒ Boolean
returns true if this file is older than the given file.
-
#open(*args) ⇒ Object
opens the file with the g iven arguments.
-
#parent ⇒ Object
returns the parent directory.
-
#read(&block) ⇒ Object
calls open with ‘r’ as argument.
-
#read_binary ⇒ Object
Calls open with ‘r’ as argument and sets the io to binary mode.
-
#relative_path_from(entry) ⇒ Object
returns the relative path from the given file or directory.
-
#save(content = '') ⇒ Object
save conent to this file this will create the parent directory if necessary.
- #to_s ⇒ Object
-
#unzip ⇒ Object
unzips the file.
-
#write(&block) ⇒ Object
Calls open with ‘w’ argument and makes sure that the parent directory of the file exists.
-
#write_binary(&block) ⇒ Object
Calls open with ‘wb’ argument, sets the io to binmode and make sure that the parent directory of the file exists.
-
#zip(target = nil, level = nil, strategy = nil) ⇒ Object
Create the zip file from current file When target is nil, the output will be the name of the current file appended with “.zip”.
Constructor Details
#initialize(factory, path) ⇒ CottaFile
creates an instance of file with the given factory and path
14 15 16 17 |
# File 'lib/cotta/cotta_file.rb', line 14 def initialize(factory, path) @path = path @factory = factory end |
Instance Attribute Details
#factory ⇒ Object (readonly)
factory with the backing system
7 8 9 |
# File 'lib/cotta/cotta_file.rb', line 7 def factory @factory end |
#path ⇒ Object (readonly)
path of this file
9 10 11 |
# File 'lib/cotta/cotta_file.rb', line 9 def path @path end |
#stat ⇒ Object (readonly)
stats of this file
11 12 13 |
# File 'lib/cotta/cotta_file.rb', line 11 def stat @stat end |
Class Method Details
.copy_io(input, output) ⇒ Object
copy from input handle to output handle
232 233 234 235 236 237 |
# File 'lib/cotta/cotta_file.rb', line 232 def self::copy_io(input, output) # output.write input.read while (content = input.read(1024)) do output.write content end end |
Instance Method Details
#==(other) ⇒ Object
239 240 241 242 |
# File 'lib/cotta/cotta_file.rb', line 239 def ==(other) return false unless other.kind_of? CottaFile return factory.system == other.factory.system && @path == other.path end |
#append(&block) ⇒ Object
Calls open with ‘a’ argument and make sure that the parent directory of the file exists
98 99 100 101 |
# File 'lib/cotta/cotta_file.rb', line 98 def append(&block) parent.mkdirs open('a', &block) end |
#basename ⇒ Object
basename of this file
30 31 32 |
# File 'lib/cotta/cotta_file.rb', line 30 def basename return @path.basename(extname).to_s end |
#copy_to(target_file) ⇒ Object
copy this file to the target file
60 61 62 63 64 |
# File 'lib/cotta/cotta_file.rb', line 60 def copy_to(target_file) target_file.parent.mkdirs factory.system.copy_file(path, target_file.path) target_file end |
#copy_to_path(target_path) ⇒ Object
copy this file to the target path
67 68 69 |
# File 'lib/cotta/cotta_file.rb', line 67 def copy_to_path(target_path) copy_to(cotta.file(target_path)) end |
#delete ⇒ Object
deletes the file
173 174 175 |
# File 'lib/cotta/cotta_file.rb', line 173 def delete factory.system.delete_file(@path) end |
#exists? ⇒ Boolean
returns true if this file exists
45 46 47 |
# File 'lib/cotta/cotta_file.rb', line 45 def exists? return factory.system.file_exists?(@path) end |
#extname ⇒ Object
extension of this file, with ‘.’
25 26 27 |
# File 'lib/cotta/cotta_file.rb', line 25 def extname return @path.extname end |
#extract(directory = nil) ⇒ Object
reads this file as an archive and extracts the content to the target directory. If the target directory is missing, it will create a directory in the same directory as this file with the same basename
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/cotta/cotta_file.rb', line 181 def extract(directory = nil) require 'rubygems/package' directory = parent.dir(basename) unless directory read_binary do |io| reader = Gem::Package::TarReader.new(io) reader.each do |entry| full_name = entry.full_name if (entry.file?) directory.file(full_name).write_binary do |output| CottaFile.copy_io(entry, output) end elsif (entry.directory?) directory.dir(full_name).mkdirs end end end directory end |
#foreach ⇒ Object
reads the file and calls back for each line
153 154 155 156 157 |
# File 'lib/cotta/cotta_file.rb', line 153 def foreach() open('r') do |file| file.each {|line| yield line} end end |
#inspect ⇒ Object
248 249 250 |
# File 'lib/cotta/cotta_file.rb', line 248 def inspect return "#{self.class}:#{self.object_id}-#{factory.system.inspect}-#@path" end |
#load ⇒ Object
Loading the file full total. This is used generally for loading
an ascii file content. It does not work with binary file on
windows system because it does no put the system on binary mode
124 125 126 127 128 129 130 131 |
# File 'lib/cotta/cotta_file.rb', line 124 def load content = nil size = stat.size read do |io| content = io.read end return content end |
#move_to(target_file) ⇒ Object
move this file to the target file
72 73 74 75 |
# File 'lib/cotta/cotta_file.rb', line 72 def move_to(target_file) target_file.parent.mkdirs factory.system.move_file(path, target_file.path) end |
#move_to_path(target_path) ⇒ Object
move this file to the target path
78 79 80 |
# File 'lib/cotta/cotta_file.rb', line 78 def move_to_path(target_path) move_to(cotta.file(target_path)) end |
#name ⇒ Object
name of this file
20 21 22 |
# File 'lib/cotta/cotta_file.rb', line 20 def name return @path.basename.to_s end |
#older_than?(file) ⇒ Boolean
returns true if this file is older than the given file
40 41 42 |
# File 'lib/cotta/cotta_file.rb', line 40 def older_than?(file) (stat <=> file.stat) == -1 end |
#open(*args) ⇒ Object
opens the file with the g iven arguments
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/cotta/cotta_file.rb', line 160 def open(*args) result = f = factory.system.io(@path, *args) if block_given? begin result = yield f ensure f.close unless f.closed? end end result end |
#parent ⇒ Object
returns the parent directory
55 56 57 |
# File 'lib/cotta/cotta_file.rb', line 55 def parent return CottaDir.new(factory, @path.parent) end |
#read(&block) ⇒ Object
calls open with ‘r’ as argument.
134 135 136 |
# File 'lib/cotta/cotta_file.rb', line 134 def read(&block) open('r', &block) end |
#read_binary ⇒ Object
Calls open with ‘r’ as argument and sets the io to binary mode
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/cotta/cotta_file.rb', line 139 def read_binary if block_given? open('r') do |io| io.binmode yield io end else io = open('r') io.binmode io end end |
#relative_path_from(entry) ⇒ Object
returns the relative path from the given file or directory
50 51 52 |
# File 'lib/cotta/cotta_file.rb', line 50 def relative_path_from(entry) path.relative_path_from(entry.path) end |
#save(content = '') ⇒ Object
save conent to this file this will create the parent directory if necessary
84 85 86 87 |
# File 'lib/cotta/cotta_file.rb', line 84 def save(content = '') write {|file| file.printf content.to_s} self end |
#to_s ⇒ Object
244 245 246 |
# File 'lib/cotta/cotta_file.rb', line 244 def to_s @path.to_s end |
#unzip ⇒ Object
unzips the file
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/cotta/cotta_file.rb', line 215 def unzip name = basename if (extname.length == 0) name = "#{name}.unzip" end target = parent.file(name) read_binary do |read_io| target.write_binary do |write_io| gz = Zlib::GzipReader.new(read_io) CottaFile.copy_io(gz, write_io) gz.close end end target end |
#write(&block) ⇒ Object
Calls open with ‘w’ argument and makes sure that the parent directory of the file exists
91 92 93 94 |
# File 'lib/cotta/cotta_file.rb', line 91 def write(&block) parent.mkdirs open('w', &block) end |
#write_binary(&block) ⇒ Object
Calls open with ‘wb’ argument, sets the io to binmode and make sure that the parent directory of the file exists
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/cotta/cotta_file.rb', line 105 def write_binary(&block) parent.mkdirs if (block_given?) open('wb') do |io| io.binmode yield io end else io = open('wb') io.binmode io end end |
#zip(target = nil, level = nil, strategy = nil) ⇒ Object
Create the zip file from current file When target is nil, the output will be the name of the current file appended with “.zip”
202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/cotta/cotta_file.rb', line 202 def zip(target = nil, level=nil, strategy=nil) target = parent.file("#{name}.zip") unless target read_binary do |read_io| target.write_binary do |write_io| gz = Zlib::GzipWriter.new(write_io, level, strategy) CottaFile.copy_io(read_io, gz) gz.close end end target end |