Class: Gemi::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/gemi/options.rb

Defined Under Namespace

Classes: YamlGivenError

Constant Summary collapse

TYPES =
{
  "-u"          => :uninstall,
  "--uninstall" => :uninstall,
  "-n"          => :update,
  "--update"    => :update,
  "-c"          => :clean,
  "--clean"     => :clean,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Options

Returns a new instance of Options.



10
11
12
# File 'lib/gemi/options.rb', line 10

def initialize(argv)
  @confpath, @type, @gem_names, @yamls = parse(argv)
end

Instance Attribute Details

#confpathObject (readonly)

Returns the value of attribute confpath.



13
14
15
# File 'lib/gemi/options.rb', line 13

def confpath
  @confpath
end

#gem_namesObject (readonly)

Returns the value of attribute gem_names.



13
14
15
# File 'lib/gemi/options.rb', line 13

def gem_names
  @gem_names
end

#typeObject (readonly)

Returns the value of attribute type.



13
14
15
# File 'lib/gemi/options.rb', line 13

def type
  @type
end

#yamlsObject (readonly)

Returns the value of attribute yamls.



13
14
15
# File 'lib/gemi/options.rb', line 13

def yamls
  @yamls
end

Class Method Details

.parse(argv) ⇒ Object



6
7
8
# File 'lib/gemi/options.rb', line 6

def self.parse(argv)
  new(argv)
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/gemi/options.rb', line 15

def empty?
  @yamls.empty? and @gem_names.empty?
end

#parse(argv) ⇒ Object



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
60
61
62
63
64
65
66
67
# File 'lib/gemi/options.rb', line 28

def parse(argv)
  confpath = GEMIRC

  if %w(-r --rc).include?(argv.first)
    confpath = argv[1]
    if not File.exist?(confpath)
      raise ConfNotFound, "config file not found in #{confpath}"
      exit
    end
    argv.shift
    argv.shift
  end

  case 
  when TYPES.key?(argv.first)
    type = TYPES[argv.first]
    args = argv[1..-1]

  when argv.first == "--"
    type = nil
    args = argv[1..-1]

  else # gem names only
    type = :install
    args = argv
  end

  case type 
  when nil # verbatim(--)
    yamls = []
    gem_names = args
  else
    yamls, gem_names = args.partition{|s| s =~ REXP_YAML}
    if [:update, :clean].include?(type) and not yamls.empty?
      raise YamlGivenError, "You cannot give yaml files with #{argv.first}"
    end
  end

  return confpath, type, gem_names, yamls
end