Class: Mangdown::Chapter

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Equality
Defined in:
lib/mangdown/chapter.rb

Overview

Mangdown chapter object, which holds pages

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Equality

#<=>

Constructor Details

#initialize(chapter) ⇒ Chapter

Returns a new instance of Chapter.



16
17
18
# File 'lib/mangdown/chapter.rb', line 16

def initialize(chapter)
  @chapter = chapter
end

Instance Attribute Details

#chapterObject (readonly)

Returns the value of attribute chapter.



10
11
12
# File 'lib/mangdown/chapter.rb', line 10

def chapter
  @chapter
end

Instance Method Details

#cbz(dir = to_path) ⇒ Object



40
41
42
# File 'lib/mangdown/chapter.rb', line 40

def cbz(dir = to_path)
  CBZ.one(dir)
end

#download_to(dir = nil, opts = { force_download: false }) ⇒ Object

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength rubocop:disable Metrics/PerceivedComplexity



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mangdown/chapter.rb', line 48

def download_to(dir = nil, opts = { force_download: false })
  failed = []
  succeeded = []
  skipped = []

  setup_download_dir!(dir)
  if opts[:force_download]
    FileUtils.rm_r(to_path)
    setup_download_dir!(dir)
  end

  Tools.hydra_streaming(
    pages,
    chapter.hydra_opts
  ) do |stage, page, data = nil|
    case stage
    when :failed
      failed << [page, data]
    when :succeeded
      succeeded << page
    when :before
      !(page.file_exist?(to_path) && skipped << page)
    when :body
      page.append_file_data(to_path, data) unless failed.include?(page)
    when :complete
      page.append_file_ext(to_path) unless failed.include?(page)
    end
  end

  FileUtils.rm_r(to_path) if succeeded.empty? && skipped.empty?

  { failed: failed, succeeded: succeeded, skipped: skipped }
end

#mangaObject



24
25
26
# File 'lib/mangdown/chapter.rb', line 24

def manga
  @manga ||= Mangdown.manga(chapter.manga)
end

#pagesObject



20
21
22
# File 'lib/mangdown/chapter.rb', line 20

def pages
  @pages ||= chapter.pages.map { |page| Mangdown.page(page) }
end

#pathObject Also known as: to_path



28
29
30
# File 'lib/mangdown/chapter.rb', line 28

def path
  @path ||= setup_path
end

#setup_path(dir = nil) ⇒ Object



33
34
35
36
37
38
# File 'lib/mangdown/chapter.rb', line 33

def setup_path(dir = nil)
  dir ||= manga.path
  path = Tools.file_join(dir, name)
  path = Tools.valid_path_name(path)
  @path = Tools.relative_or_absolute_path(path)
end