Class: EpubForge::Action::FileTransformer
- Inherits:
-
Object
- Object
- EpubForge::Action::FileTransformer
- Defined in:
- lib/epubforge/action/file_transformer.rb
Instance Attribute Summary collapse
-
#original_filename ⇒ Object
readonly
Returns the value of attribute original_filename.
-
#transformed_filename ⇒ Object
readonly
Returns the value of attribute transformed_filename.
Instance Method Summary collapse
- #<<(input) ⇒ Object
- #abort ⇒ Object
- #finalize ⇒ Object
- #finished? ⇒ Boolean
-
#initialize(file) ⇒ FileTransformer
constructor
A new instance of FileTransformer.
- #new_size ⇒ Object
- #old_size ⇒ Object
- #read_file ⇒ Object
- #readlines(&block) ⇒ Object
- #write(input) ⇒ Object
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_filename ⇒ Object (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_filename ⇒ Object (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 |
#abort ⇒ Object
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 |
#finalize ⇒ Object
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
54 55 56 |
# File 'lib/epubforge/action/file_transformer.rb', line 54 def finished? @finished end |
#new_size ⇒ Object
50 51 52 |
# File 'lib/epubforge/action/file_transformer.rb', line 50 def new_size File.size?( @transformed_filename ) end |
#old_size ⇒ Object
46 47 48 |
# File 'lib/epubforge/action/file_transformer.rb', line 46 def old_size File.size?( @original_filename ) end |
#read_file ⇒ Object
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 |