class Belajar::Course

Attributes

author[R]
path[R]
title[R]

Public Class Methods

new(path) click to toggle source
# File lib/belajar/course.rb, line 7
def initialize(path)
  @path   = path
  @title  = File.basename(path).gsub(/\_+/, ' ')
  @author = QuickStore.store.get(key(:author))
end
unzip(file_path, options = {}) click to toggle source
# File lib/belajar/course.rb, line 40
def unzip(file_path, options = {})
  target_dir = File.dirname(file_path)
  course_dir = nil

  Zip::File.open(file_path) do |zip_file|
    zip_file.each do |entry|
      if options[:github_repo]
        first, *others = entry.to_s.split('/')
        directory = File.join(first.split('-')[0..-2].join('-'), others)
      else
        directory = entry.to_s
      end

      if course_dir.nil? && directory != '/'
        course_dir = File.join(target_dir, directory.gsub(/\/$/, ''))

        if Dir.exist?(course_dir)
          FileUtils.copy_entry("#{course_dir}/", "#{course_dir}_old/", true)
          FileUtils.rm_rf("#{course_dir}/.", secure: true)
        end
      end

      zip_file.extract(entry, "#{target_dir}/#{directory}") { true }
    end
  end

  FileUtils.rm(file_path)
rescue StandardError => e
  puts e
  old_dir = "#{course_dir}_old/"

  if Dir.exist?(old_dir)
    FileUtils.copy_entry(old_dir, "#{course_dir}/", true)
  end
ensure
  old_dir = "#{course_dir}_old/"
  FileUtils.rm_r(old_dir) if Dir.exist?(old_dir)
  return Course.new(course_dir) if course_dir
end

Public Instance Methods

chapters() click to toggle source
# File lib/belajar/course.rb, line 13
def chapters
  @chapters ||= Loading::Chapters.load(@path)
end
key(key_name) click to toggle source
# File lib/belajar/course.rb, line 25
def key(key_name)
  Storeable.key(title, prefix: 'courses', suffix: key_name)
end
mastered?() click to toggle source
# File lib/belajar/course.rb, line 21
def mastered?
  chapters.all?(&:mastered?)
end
started?() click to toggle source
# File lib/belajar/course.rb, line 17
def started?
  chapters.any?(&:started?)
end