Class: Ki::VersionFileOperations

Inherits:
Object
  • Object
show all
Defined in:
lib/data_access/version_operations.rb

Instance Method Summary collapse

Instance Method Details

#copy_or_move(file_map, args, op) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/data_access/version_operations.rb', line 33

def copy_or_move(file_map, args, op)
  delete = op == "mv"
  dest = args.delete_at(-1)
  patterns = args.map { |pattern| FileRegexp.matcher(pattern) }
  matching_files = []
  file_map.keys.each do |file|
    matching_pattern = patterns.any_matches?(file)
    if matching_pattern
      matching_files << file
      dest_file = resolve_dest_file(file, dest, matching_pattern)
      file_map[dest_file]=file_map[file]
      if delete
        file_map.delete(file)
      end
    end
  end
end

#delete(file_map, args, op) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/data_access/version_operations.rb', line 51

def delete(file_map, args, op)
  patterns = args.map { |pattern| FileRegexp.matcher(pattern) }
  file_map.keys.each do |file|
    if patterns.any_matches?(file)
      file_map.delete(file)
    end
  end
end

#edit_file_map(file_map, operations) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/data_access/version_operations.rb', line 60

def edit_file_map(file_map, operations)
  operations.each do |op, *args|
    case op
      when "cp"
        copy_or_move(file_map, args, op)
      when "mv"
        copy_or_move(file_map, args, op)
      when "rm"
        delete(file_map, args, op)
    end
  end
end

#resolve_dest_file(file, dest, matching_pattern) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/data_access/version_operations.rb', line 19

def resolve_dest_file(file, dest, matching_pattern)
  matcher = matching_pattern.match(file)
  dest = dest.gsub(/\$\d/) do |str|
    matcher[Integer(str[1..-1])]
  end
  if dest.end_with?("/")
    dest = File.join(dest, File.basename(file))
  end
  if dest.start_with?("/")
    dest = dest[1..-1]
  end
  dest
end