Class: LearnOpen::Opener

Inherits:
Object
  • Object
show all
Defined in:
lib/learn_open/opener.rb

Constant Summary collapse

HOME_DIR =
File.expand_path("~")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lesson, editor, get_next_lesson) ⇒ Opener

Returns a new instance of Opener.



11
12
13
14
15
16
17
18
19
20
# File 'lib/learn_open/opener.rb', line 11

def initialize(lesson, editor, get_next_lesson)
  , @token = Netrc.read['learn-config']
  @client        = LearnWeb::Client.new(token: @token)

  @lesson          = lesson
  @editor          = editor
  @get_next_lesson = get_next_lesson
  @lessons_dir     = YAML.load(File.read("#{HOME_DIR}/.learn-config"))[:learn_directory]
  @file_path       = "#{HOME_DIR}/.learn-open-tmp"
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



4
5
6
# File 'lib/learn_open/opener.rb', line 4

def client
  @client
end

#dot_learnObject

Returns the value of attribute dot_learn.



5
6
7
# File 'lib/learn_open/opener.rb', line 5

def dot_learn
  @dot_learn
end

#editorObject (readonly)

Returns the value of attribute editor.



4
5
6
# File 'lib/learn_open/opener.rb', line 4

def editor
  @editor
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



4
5
6
# File 'lib/learn_open/opener.rb', line 4

def file_path
  @file_path
end

#get_next_lessonObject (readonly)

Returns the value of attribute get_next_lesson.



4
5
6
# File 'lib/learn_open/opener.rb', line 4

def get_next_lesson
  @get_next_lesson
end

#later_lessonObject

Returns the value of attribute later_lesson.



5
6
7
# File 'lib/learn_open/opener.rb', line 5

def later_lesson
  @later_lesson
end

#lessonObject

Returns the value of attribute lesson.



5
6
7
# File 'lib/learn_open/opener.rb', line 5

def lesson
  @lesson
end

#lesson_idObject

Returns the value of attribute lesson_id.



5
6
7
# File 'lib/learn_open/opener.rb', line 5

def lesson_id
  @lesson_id
end

#lesson_is_labObject

Returns the value of attribute lesson_is_lab.



5
6
7
# File 'lib/learn_open/opener.rb', line 5

def lesson_is_lab
  @lesson_is_lab
end

#lessons_dirObject (readonly)

Returns the value of attribute lessons_dir.



4
5
6
# File 'lib/learn_open/opener.rb', line 4

def lessons_dir
  @lessons_dir
end

#repo_dirObject

Returns the value of attribute repo_dir.



5
6
7
# File 'lib/learn_open/opener.rb', line 5

def repo_dir
  @repo_dir
end

#tokenObject (readonly)

Returns the value of attribute token.



4
5
6
# File 'lib/learn_open/opener.rb', line 4

def token
  @token
end

Class Method Details

.run(lesson:, editor_specified:, get_next_lesson:) ⇒ Object



7
8
9
# File 'lib/learn_open/opener.rb', line 7

def self.run(lesson:, editor_specified:, get_next_lesson:)
  new(lesson, editor_specified, get_next_lesson).run
end

Instance Method Details

#repo_exists?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/learn_open/opener.rb', line 61

def repo_exists?
  File.exists?("#{lessons_dir}/#{repo_dir}/.git")
end

#runObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/learn_open/opener.rb', line 22

def run
  setup_tmp_file

  set_lesson

  if ide_version_3?
    if self.repo_dir != ENV['LAB_NAME']
      home_dir = "/home/#{ENV['CREATED_USER']}"
      File.open("#{home_dir}/.custom_commands.log", "a") do |f|
        f.puts %Q{{"command": "open_lab", "lab_name": "#{self.repo_dir}"}}
      end
      exit
    end
  end

  puts "Looking for lesson..."

  if jupyter_notebook_environment?
    git_tasks
    file_tasks
    restore_files
    watch_for_changes
    jupyter_pip_install
    completion_tasks
  else
    warn_if_necessary
    if lesson_is_readme?
      open_readme
    else
      git_tasks
      file_tasks
      setup_backup_if_needed
      dependency_tasks
      completion_tasks
    end
  end

end