Class: EpubForge::Action::FileTransformer

Inherits:
Object
  • Object
show all
Defined in:
lib/epubforge/action/file_transformer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ FileTransformer

Returns a new instance of FileTransformer.



5
6
7
8
9
10
# File 'lib/epubforge/action/file_transformer.rb', line 5

def initialize( file )
  @original_filename = file
  @transformed_filename = "#{@original_filename}.epubforge.#{sprintf("%07i", rand(1000000))}.tmp"
  @out = File.open( @transformed_filename, "w" )
  @finished = false
end

Instance Attribute Details

#original_filenameObject (readonly)

Returns the value of attribute original_filename.



4
5
6
# File 'lib/epubforge/action/file_transformer.rb', line 4

def original_filename
  @original_filename
end

#transformed_filenameObject (readonly)

Returns the value of attribute transformed_filename.



4
5
6
# File 'lib/epubforge/action/file_transformer.rb', line 4

def transformed_filename
  @transformed_filename
end

Instance Method Details

#<<(input) ⇒ Object



32
33
34
# File 'lib/epubforge/action/file_transformer.rb', line 32

def <<( input )
  write( input )
end

#abortObject



20
21
22
23
24
# File 'lib/epubforge/action/file_transformer.rb', line 20

def abort
  return if finished?
  @finished = true
  FileUtils.rm( @transformed_filename )
end

#finalizeObject



12
13
14
15
16
17
18
# File 'lib/epubforge/action/file_transformer.rb', line 12

def finalize
  return if finished?
  @finished = true
  @out.close
    
  FileUtils.mv( @transformed_filename, @original_filename )
end

#finished?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/epubforge/action/file_transformer.rb', line 54

def finished?
  @finished
end

#new_sizeObject



50
51
52
# File 'lib/epubforge/action/file_transformer.rb', line 50

def new_size
  File.size?( @transformed_filename )
end

#old_sizeObject



46
47
48
# File 'lib/epubforge/action/file_transformer.rb', line 46

def old_size
  File.size?( @original_filename )
end

#read_fileObject



36
37
38
# File 'lib/epubforge/action/file_transformer.rb', line 36

def read_file
  File.read( @original_filename )
end

#readlines(&block) ⇒ Object



40
41
42
43
44
# File 'lib/epubforge/action/file_transformer.rb', line 40

def readlines( &block )
  File.readlines( @original_filename ) do |line|
    yield line
  end
end

#write(input) ⇒ Object



26
27
28
29
30
# File 'lib/epubforge/action/file_transformer.rb', line 26

def write( input )
  return if finished?
  @out << input
  @out.flush
end