Class: LearnOpen::ArgumentParser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ ArgumentParser

Returns a new instance of ArgumentParser.



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

def initialize(args)
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



3
4
5
# File 'lib/learn_open/argument_parser.rb', line 3

def args
  @args
end

Instance Method Details

#executeObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/learn_open/argument_parser.rb', line 9

def execute
  config_path = File.expand_path('~/.learn-config')
  editor_data = YAML.load(File.read(config_path))[:editor]
  if editor_data.match(/ /)
    editor_data = editor_data.split(' ').first
  end

  lesson = nil
  next_lesson = false

  configured_editor = !(editor_data.empty? || editor_data.nil?) ? editor_data : nil
  editor_specified = ARGV.detect {|arg| arg.start_with?('--editor=')}.match(/\-\-editor=(.+)/) || configured_editor
  open_after = !!editor_specified

  if !ARGV[0].start_with?('--editor=') && !ARGV[0].start_with?('--next')
    lesson = ARGV[0].sub(/\/$/, '')
  elsif ARGV[0].start_with?('--next')
    next_lesson = true
  end

  if open_after
    editor_specified = editor_specified.is_a?(String) ? editor_specified : editor_specified[1]
  end

  [lesson, editor_specified, next_lesson]
end