Class: ActiveModelArchive::FileManager
- Inherits:
-
Object
- Object
- ActiveModelArchive::FileManager
- Defined in:
- lib/active_model_archive/file_manager.rb
Constant Summary collapse
- UNLIMITED_PER_FILE =
999999999
Instance Attribute Summary collapse
-
#base_filename ⇒ Object
Returns the value of attribute base_filename.
-
#file_number ⇒ Object
Returns the value of attribute file_number.
-
#item_count ⇒ Object
Returns the value of attribute item_count.
-
#items_per_file ⇒ Object
Returns the value of attribute items_per_file.
Instance Method Summary collapse
- #add(object) ⇒ Object
- #create_file ⇒ Object
- #flush ⇒ Object
-
#initialize(base_filename, items_per_file = nil) {|_self| ... } ⇒ FileManager
constructor
A new instance of FileManager.
Constructor Details
#initialize(base_filename, items_per_file = nil) {|_self| ... } ⇒ FileManager
Returns a new instance of FileManager.
5 6 7 8 9 10 11 12 13 |
# File 'lib/active_model_archive/file_manager.rb', line 5 def initialize(base_filename, items_per_file = nil) @item_count = 0 @file_number = 0 @base_filename = base_filename @items_per_file = items_per_file || UNLIMITED_PER_FILE yield(self) flush end |
Instance Attribute Details
#base_filename ⇒ Object
Returns the value of attribute base_filename.
4 5 6 |
# File 'lib/active_model_archive/file_manager.rb', line 4 def base_filename @base_filename end |
#file_number ⇒ Object
Returns the value of attribute file_number.
4 5 6 |
# File 'lib/active_model_archive/file_manager.rb', line 4 def file_number @file_number end |
#item_count ⇒ Object
Returns the value of attribute item_count.
4 5 6 |
# File 'lib/active_model_archive/file_manager.rb', line 4 def item_count @item_count end |
#items_per_file ⇒ Object
Returns the value of attribute items_per_file.
4 5 6 |
# File 'lib/active_model_archive/file_manager.rb', line 4 def items_per_file @items_per_file end |
Instance Method Details
#add(object) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/active_model_archive/file_manager.rb', line 15 def add(object) if item_count % items_per_file == 0 flush @current_file = create_file @current_file.write(object.to_archive_header) end self.item_count += 1 @current_file.write(object.to_archive) end |
#create_file ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/active_model_archive/file_manager.rb', line 27 def create_file self.file_number += 1 if items_per_file == UNLIMITED_PER_FILE filename = base_filename else extension = File.extname(base_filename) filename = "#{base_filename.chomp(extension)}.#{file_number}#{extension}" end File.open(filename, "w") end |
#flush ⇒ Object
40 41 42 43 44 |
# File 'lib/active_model_archive/file_manager.rb', line 40 def flush if @current_file @current_file.close end end |