Class: Gitcopier::Decisions

Inherits:
Object
  • Object
show all
Defined in:
lib/gitcopier/decisions.rb

Instance Method Summary collapse

Constructor Details

#initialize(source_root, des_root) ⇒ Decisions

Returns a new instance of Decisions.



7
8
9
10
# File 'lib/gitcopier/decisions.rb', line 7

def initialize(source_root, des_root)
  @source_root = source_root
  @des_root = des_root
end

Instance Method Details

#get(changed_file) ⇒ Object



28
29
30
# File 'lib/gitcopier/decisions.rb', line 28

def get(changed_file)
  @decisions[changed_file]
end

#get_data_from_fileObject



32
33
34
# File 'lib/gitcopier/decisions.rb', line 32

def get_data_from_file
  File.read(decision_file) rescue "{}"
end

#is_valid?(des_string) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
# File 'lib/gitcopier/decisions.rb', line 67

def is_valid?(des_string)
  if !des_string.start_with? @des_root
    puts "Your path must be inside #{@des_root}".colorize(:red)
    # TODO: support user to reconfigure destination root
    return false
  end
  true
end

#loadObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gitcopier/decisions.rb', line 12

def load
  @data = JSON.parse(get_data_from_file)
  @decisions = Hash[@data.map do |pair|
    source = pair[0]

    decision = Decision.new(
      pair[1]['decision'], pair[1]['source'], pair[1]['des'])

    [source, decision]
  end]
end

#prompt_decision(changed_file) ⇒ Object



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
# File 'lib/gitcopier/decisions.rb', line 36

def prompt_decision(changed_file)
  source_file = @source_root + changed_file
  source_file << "*" if source_file[-1] == "/"
  while true do
    print "Where do you want to copy #{(@source_root + changed_file).colorize(:green)}? (type h for instruction): "
    user_input = Readline.readline("", true).strip
    if user_input == "h"
      puts "      #{'h'.colorize(:green)}: For instructions"
      puts "      #{'n'.colorize(:green)}: For not copy the file or directory recursively"
      puts "#{'<enter>'.colorize(:green)}: If it's a file, it will ignore the file. If it's a directory, it will prompt you to decide on files/directories inside recursively"
      puts " #{'<path>'.colorize(:green)}: It will copy the file or directory's content to the path."
    elsif user_input == "n"
      decision = Decision.new("n", changed_file, "")
      @decisions[changed_file] = decision
      return decision
    elsif user_input == ""
      decision = Decision.new("", changed_file, "")
      @decisions[changed_file] = decision
      return decision
    else
      unless is_valid?(user_input)
        next
      end
      destination = user_input[@des_root.size..user_input.size]
      decision = Decision.new("y", changed_file, destination)
      @decisions[changed_file] = decision
      return decision
    end
  end
end

#saveObject



24
25
26
# File 'lib/gitcopier/decisions.rb', line 24

def save
  File.write(decision_file, JSON.pretty_generate(@decisions))
end