Class: Daigaku::Course

Inherits:
Object
  • Object
show all
Defined in:
lib/daigaku/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.



8
9
10
11
12
# File 'lib/daigaku/course.rb', line 8

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.



6
7
8
# File 'lib/daigaku/course.rb', line 6

def author
  @author
end

Returns the value of attribute link.



6
7
8
# File 'lib/daigaku/course.rb', line 6

def link
  @link
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/daigaku/course.rb', line 6

def path
  @path
end

#titleObject (readonly)

Returns the value of attribute title.



6
7
8
# File 'lib/daigaku/course.rb', line 6

def title
  @title
end

Class Method Details

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



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
# File 'lib/daigaku/course.rb', line 41

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 Exception => e
  puts e
  old_dir = "#{course_dir}_old/"
  FileUtils.copy_entry(old_dir, "#{course_dir}/", true) if Dir.exist?(old_dir)
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



14
15
16
# File 'lib/daigaku/course.rb', line 14

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

#key(key_name) ⇒ Object



26
27
28
# File 'lib/daigaku/course.rb', line 26

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

#mastered?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/daigaku/course.rb', line 22

def mastered?
  chapters.reduce(true) { |mastered, chapter| mastered &&= chapter.mastered? }
end

#started?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/daigaku/course.rb', line 18

def started?
  chapters.reduce(false) { |started, chapter| started ||= chapter.started? }
end