Class: Belajar::Course

Inherits:
Object
  • Object
show all
Defined in:
lib/belajar/course.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Course

Returns a new instance of Course.



7
8
9
10
11
# File 'lib/belajar/course.rb', line 7

def initialize(path)
  @path   = path
  @title  = File.basename(path).gsub(/\_+/, ' ')
  @author = QuickStore.store.get(key(:author))
end

Instance Attribute Details

#authorObject (readonly)

Returns the value of attribute author.



5
6
7
# File 'lib/belajar/course.rb', line 5

def author
  @author
end

Returns the value of attribute link.



5
6
7
# File 'lib/belajar/course.rb', line 5

def link
  @link
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/belajar/course.rb', line 5

def path
  @path
end

#titleObject (readonly)

Returns the value of attribute title.



5
6
7
# File 'lib/belajar/course.rb', line 5

def title
  @title
end

Class Method Details

.unzip(file_path, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
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
# 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

Instance Method Details

#chaptersObject



13
14
15
# File 'lib/belajar/course.rb', line 13

def chapters
  @chapters ||= Loading::Chapters.load(@path)
end

#key(key_name) ⇒ Object



25
26
27
# File 'lib/belajar/course.rb', line 25

def key(key_name)
  Storeable.key(title, prefix: 'courses', suffix: key_name)
end

#mastered?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/belajar/course.rb', line 21

def mastered?
  chapters.all?(&:mastered?)
end

#started?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/belajar/course.rb', line 17

def started?
  chapters.any?(&:started?)
end