Module: MyManga

Defined in:
lib/my_manga.rb,
lib/my_manga/add.rb,
lib/my_manga/cli.rb,
lib/my_manga/env.rb,
lib/my_manga/find.rb,
lib/my_manga/list.rb,
lib/my_manga/mark.rb,
lib/my_manga/zine.rb,
lib/my_manga/remove.rb,
lib/my_manga/update.rb,
lib/my_manga/command.rb,
lib/my_manga/version.rb,
lib/my_manga/download.rb,
lib/my_manga/m_clean_up.rb

Defined Under Namespace

Modules: CLI

Constant Summary collapse

VERSION =
'2.2.0'

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Object



19
20
21
# File 'lib/my_manga.rb', line 19

def [](name)
  Manga.find_by(name: name)
end

.add(uri) ⇒ Object



47
48
49
50
51
52
# File 'lib/my_manga.rb', line 47

def add(uri)
  return if Manga.find_by_uri(uri)

  md_manga = Mangdown::MDHash.new(uri: uri).to_manga
  Manga.from_md(md_manga)
end

.add_to_zine(names) ⇒ Object



35
36
37
# File 'lib/my_manga.rb', line 35

def add_to_zine(names)
  Manga.where(name: names).update(zine: true)
end

.create_manga_download_dir(manga) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/my_manga.rb', line 113

def create_manga_download_dir(manga)
  base = MyManga.download_dir
  manga_dir = [base, manga.name].join('/')
  Dir.mkdir(base) unless Dir.exist?(base)
  Dir.mkdir(manga_dir) unless Dir.exist?(manga_dir)
  manga_dir
end

.download(manga, numbers) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/my_manga.rb', line 85

def download(manga, numbers)
  unless env == 'test'
    download_dir = create_manga_download_dir(manga)
    chapters = fetch_chapters(manga, numbers)

    chapters.each do |chapter|
      download_chapter(chapter, download_dir)
    end
    package(download_dir)
  end

  read!(manga, numbers)
end

.download_chapter(chapter, download_dir) ⇒ Object



78
79
80
81
82
83
# File 'lib/my_manga.rb', line 78

def download_chapter(chapter, download_dir)
  return if env == 'test'

  md_chapter = chapter.to_md
  md_chapter.download_to(download_dir)
end

.fetch_chapters(manga, numbers) ⇒ Object



58
59
60
# File 'lib/my_manga.rb', line 58

def fetch_chapters(manga, numbers)
  manga.chapters.where(number: numbers)
end

.find(search) ⇒ Object



23
24
25
# File 'lib/my_manga.rb', line 23

def find(search)
  M.find(Regexp.new(search, 'i'))
end

.mangdown_client_clean_upObject



99
100
101
# File 'lib/my_manga.rb', line 99

def mangdown_client_clean_up
  M.clean_up
end

.namesObject



31
32
33
# File 'lib/my_manga.rb', line 31

def names
  Manga.pluck(:name)
end

.package(dir) ⇒ Object



27
28
29
# File 'lib/my_manga.rb', line 27

def package(dir)
  Mangdown::CBZ.all(dir)
end

.read!(manga, numbers) ⇒ Object



62
63
64
# File 'lib/my_manga.rb', line 62

def read!(manga, numbers)
  set_read(manga, numbers, true)
end

.remove(name) ⇒ Object



54
55
56
# File 'lib/my_manga.rb', line 54

def remove(name)
  Manga.find_by_name(name)&.destroy
end

.remove_from_zine(names) ⇒ Object



39
40
41
# File 'lib/my_manga.rb', line 39

def remove_from_zine(names)
  Manga.where(name: names).update(zine: false)
end

.set_read(manga, numbers, bool) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/my_manga.rb', line 104

def set_read(manga, numbers, bool)
  chapters = fetch_chapters(manga, numbers)
  chapters.update_all(read: bool)
  manga.reload
  manga.read_count = manga.chapters_read.length
  manga.save
end

.unread!(manga, numbers) ⇒ Object



66
67
68
# File 'lib/my_manga.rb', line 66

def unread!(manga, numbers)
  set_read(manga, numbers, false)
end

.update(manga) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/my_manga.rb', line 70

def update(manga)
  return if env == 'test'

  md_manga = manga.to_md
  chapters = md_manga.chapters
  manga.update_chapters(chapters)
end

.zineObject



43
44
45
# File 'lib/my_manga.rb', line 43

def zine
  Manga.where(zine: true)
end