Class: Scorm::Command::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/scorm/commands/base.rb

Direct Known Subclasses

Bundle, Check, Create, Extract, Help, Version

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Base

Returns a new instance of Base.



8
9
10
11
# File 'lib/scorm/commands/base.rb', line 8

def initialize(args)
  @args = args
  @autodetected_package = false
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



5
6
7
# File 'lib/scorm/commands/base.rb', line 5

def args
  @args
end

#autodetected_packageObject (readonly)

Returns the value of attribute autodetected_package.



6
7
8
# File 'lib/scorm/commands/base.rb', line 6

def autodetected_package
  @autodetected_package
end

Instance Method Details

#display(msg, newline = true) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/scorm/commands/base.rb', line 13

def display(msg, newline=true)
  if newline
    puts(msg)
  else
    print(msg)
    STDOUT.flush
  end
end

#error(msg) ⇒ Object



22
23
24
25
# File 'lib/scorm/commands/base.rb', line 22

def error(msg)
  STDERR.puts(msg)
  exit 1
end

#extract_option(options, default = true) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/scorm/commands/base.rb', line 37

def extract_option(options, default=true)
  values = options.is_a?(Array) ? options : [options]
  return unless opt_index = args.select { |a| values.include? a }.first
  opt_position = args.index(opt_index) + 1
  if args.size > opt_position && opt_value = args[opt_position]
    if opt_value.include?('--')
      opt_value = nil
    else
      args.delete_at(opt_position)
    end
  end
  opt_value ||= default
  args.delete(opt_index)
  block_given? ? yield(opt_value) : opt_value
end

#extract_package(force = true) ⇒ Object

Raises:



27
28
29
30
31
32
33
34
35
# File 'lib/scorm/commands/base.rb', line 27

def extract_package(force=true)
  package = extract_option('--package', false)
  raise(CommandFailed, "You must specify a package name after --package") if package == false
  unless package
    raise(CommandFailed, "No package specified.\nRun this command from package folder or set it adding --package <package name>") if force
    @autodetected_package = true
  end
  package
end