Class: Mangos::Update

Inherits:
Object
  • Object
show all
Defined in:
lib/mangos/update.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#booksObject

Returns the value of attribute books.



2
3
4
# File 'lib/mangos/update.rb', line 2

def books
  @books
end

Instance Method Details

#all_pathsObject



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_dataObject



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

#processObject



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_deletedObject



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_dataObject



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_mappingObject



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

#updateObject



10
11
12
13
14
15
16
17
# File 'lib/mangos/update.rb', line 10

def update
  puts "Running with options #{@package.options.inspect}" unless @package.options.empty?
  load_data
  process
  save_data
  save_key_mapping if @package.migrate?
  puts "Done!"
end