lib/aviglitch/frames.rb in aviglitch-0.2.0 vs lib/aviglitch/frames.rb in aviglitch-0.2.2
- old
+ new
@@ -20,20 +20,21 @@
attr_reader :avi
##
# Creates a new AviGlitch::Frames object.
+ # It takes AviGlitch::Avi as an argument.
def initialize avi
@avi = avi
end
##
# Enumerates the frames.
# It returns Enumerator if a block is not given.
def each &block
if block_given?
- Tempfile.open('temp', binmode: true) do |newmovi|
+ Tempfile.open('aviglitch-temp', @avi.tmpdir, binmode: true) do |newmovi|
@avi.process_movi do |indices, movi|
newindices = indices.select do |m|
movi.pos = m[:offset] + 8 # 8 for id and size
frame = Frame.new(movi.read(m[:size]), m[:id], m[:flag])
block.call frame
@@ -134,10 +135,11 @@
result = self.slice 0, 0
frames = self.slice 0..-1
times.times do
result.concat frames
end
+ frames.terminate
result
end
##
# Returns the Frame object at the given index or
@@ -173,11 +175,16 @@
sliced = l.nil? ? self.slice(b) : self.slice(b, l)
head = self.slice(0, b)
l = 1 if l.nil?
tail = self.slice((b + l)..-1)
self.clear
- self.concat head + tail
+ temp = head + tail
+ self.concat temp
+ temp.terminate
+ head.terminate
+ tail.terminate
+
sliced
end
##
# Removes frame(s) at the given index or the range (same as []).
@@ -197,10 +204,14 @@
end
new_frames = head + rest
self.clear
self.concat new_frames
+
+ new_frames.terminate
+ head.terminate
+ rest.terminate
end
##
# Returns one Frame object at the given index.
def at n
@@ -340,10 +351,11 @@
end
new_frames.concat self.slice(n..-1)
self.clear
self.concat new_frames
+ new_frames.terminate
self
end
##
# Deletes one Frame at the given index.
@@ -365,9 +377,16 @@
##
# Returns true if +other+'s frames are same as self's frames.
def == other
@avi == other.avi
+ end
+
+ ##
+ # Closes the internal temp file explicitly. This instance becomes unusable.
+ def terminate
+ @avi.close
+ @avi = nil
end
##
# Generates new AviGlitch::Base instance using self.
def to_avi