Class: FackUp::Image
- Inherits:
-
Object
- Object
- FackUp::Image
- Defined in:
- lib/fackup/image.rb
Defined Under Namespace
Classes: File
Constant Summary collapse
- PACK_TEMPLATE =
"LLLIII"
- PACK_SIZE =
[0, 0, 0, 0, 0, 0].pack(PACK_TEMPLATE).bytesize
Instance Method Summary collapse
- #close ⇒ Object
- #each(&blk) ⇒ Object
-
#initialize(file, mode = 'r', force = false) ⇒ Image
constructor
A new instance of Image.
- #push(path) ⇒ Object (also: #<<)
- #save ⇒ Object
Constructor Details
#initialize(file, mode = 'r', force = false) ⇒ Image
Returns a new instance of Image.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/fackup/image.rb', line 36 def initialize (file, mode='r', force=false) @files = [] ::File.open(file, 'a'){} if force if mode == 'r' raise Errno::ENOENT, "File doesn't exist" unless ::File.exists?(file) @fd = ::File.open(file, 'rb') load_file [:push, :<<, :save].each {|meth| (class << self; self; end).class_eval { undef_method(meth) } } elsif mode == 'w' @fd = ::File.open(file, 'wb') (class << self; self; end).class_eval { undef_method(:each) } else raise "Unrecognized mode" end if block_given? yield self close end end |
Instance Method Details
#close ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/fackup/image.rb', line 86 def close if self.respond_to?(:save) save end @fd.close end |
#each(&blk) ⇒ Object
74 75 76 |
# File 'lib/fackup/image.rb', line 74 def each(&blk) @files.each(&blk) end |
#push(path) ⇒ Object Also known as: <<
64 65 66 67 68 69 70 71 |
# File 'lib/fackup/image.rb', line 64 def push (path) path = ::File.realpath(path) io = ::File.open(path, 'rb') stat = io.stat @files << File.new(path, io, stat.mtime, 0, io.size, stat.uid, stat.gid, stat.mode) self end |
#save ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/fackup/image.rb', line 78 def save @fd.write(header) @fd.write("\0") @files.each {|file| @fd.write(file.content) } end |