Module: IrokiLib::CoreExt::File

Defined in:
lib/iroki_lib/core_ext/file/file.rb

Instance Method Summary collapse

Instance Method Details

#check_file(arg, which) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/iroki_lib/core_ext/file/file.rb', line 22

def check_file arg, which
  help = " Try color_tree --help for help."

  abort_if arg.nil?,
           "You must provide a #{which} file.#{help}"

  abort_unless Object::File.exists?(arg),
               "The file #{arg} doesn't exist.#{help}"

  arg
end

#parse_name_map(fname) ⇒ Object



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
# File 'lib/iroki_lib/core_ext/file/file.rb', line 34

def parse_name_map fname
  check_file fname, :name_map

  name_map = {}
  Object::File.open(fname).each_line do |line|
    oldname, newname = line.chomp.split "\t"


    abort_if oldname.nil? || oldname.empty?,
             "Column 1 missing for line: #{line.inspect}"

    abort_if newname.nil? || newname.empty?,
             "Column 2 missing for line: #{line.inspect}"

    oldname = clean oldname
    newname = clean newname

    abort_if name_map.has_key?(oldname),
             "#{oldname} is repeated in column 1"

    name_map[oldname] = newname
  end

  abort_if duplicate_values?(name_map),
           "Names in column 2 of name map file must be unique"

  name_map
end