Class: Mangos::Update
- Inherits:
-
Object
- Object
- Mangos::Update
- Defined in:
- lib/mangos/update.rb
Instance Attribute Summary collapse
-
#books ⇒ Object
Returns the value of attribute books.
Instance Method Summary collapse
- #all_paths ⇒ Object
-
#initialize(package, processor) ⇒ Update
constructor
A new instance of Update.
- #load_data ⇒ Object
- #process ⇒ Object
- #process_deleted ⇒ Object
- #process_path(path) ⇒ Object
- #save_data ⇒ Object
- #save_key_mapping ⇒ Object
- #update ⇒ Object
Constructor Details
#initialize(package, processor) ⇒ Update
Returns a new instance of Update.
4 5 6 7 8 |
# File 'lib/mangos/update.rb', line 4 def initialize(package, processor) @package = package @processor = processor @books = [] end |
Instance Attribute Details
#books ⇒ Object
Returns the value of attribute books.
2 3 4 |
# File 'lib/mangos/update.rb', line 2 def books @books end |
Instance Method Details
#all_paths ⇒ Object
87 88 89 |
# File 'lib/mangos/update.rb', line 87 def all_paths @package.path.children.reject { |p| p.hidden? }.select { |p| p.directory? } end |
#load_data ⇒ Object
19 20 21 22 23 24 |
# File 'lib/mangos/update.rb', line 19 def load_data return unless @package.data_path.exist? puts "Reading in JSON file" @books = JSON.parse(@package.data_path.read).map { |b| Mangos::Book.from_hash(b) } end |
#process ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/mangos/update.rb', line 36 def process puts "Processing books...\n" @new_books = 0 @updated_books = 0 @skipped_books = 0 @deleted_books = 0 paths = all_paths paths.each_with_index do |p, i| $stdout.write "\rProcessing #{i + 1} of #{paths.length} (#{(((i + 1) / paths.length.to_f) * 100.0).round}%)" $stdout.flush process_path(p) end process_deleted puts "\nProcessed #{@new_books} new books, updated #{@updated_books} existing books, removed #{@deleted_books} deleted books, and skipped #{@skipped_books} books" end |
#process_deleted ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/mangos/update.rb', line 57 def process_deleted books_to_remove = @books.reject do |book| path = @package.path + book.path path.exist? end @deleted_books = books_to_remove.length books_to_remove.each { |book| @processor.delete(book) } @books -= books_to_remove end |
#process_path(path) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/mangos/update.rb', line 70 def process_path(path) book_path = path.basename.to_s book = @books.find { |b| b.path == book_path } if book if @processor.update(path, book) @updated_books += 1 else @skipped_books += 1 end else @books << @processor.create(path) @new_books += 1 end end |
#save_data ⇒ Object
26 27 28 29 |
# File 'lib/mangos/update.rb', line 26 def save_data puts "Writing out JSON file" @package.data_path.write(@books.map { |b| b.to_hash }.to_json) end |
#save_key_mapping ⇒ Object
31 32 33 34 |
# File 'lib/mangos/update.rb', line 31 def save_key_mapping puts "Writing out key mapping JSON file" @package.key_mapping_path.write(@books.to_h { |b| [b.old_key, b.key] }.to_json) end |
#update ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/mangos/update.rb', line 10 def update puts "Running with options #{@package..inspect}" unless @package..empty? load_data process save_data save_key_mapping if @package.migrate? puts "Done!" end |