Module: DiceBag::DiceBagFile

Included in:
ConfigFile, DefaultTemplateFile, TemplateFile
Defined in:
lib/dice_bag/dice_bag_file.rb

Constant Summary collapse

@@overwrite_all =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#destinationObject (readonly)

Returns the value of attribute destination.



9
10
11
# File 'lib/dice_bag/dice_bag_file.rb', line 9

def destination
  @destination
end

#fileObject (readonly)

Returns the value of attribute file.



9
10
11
# File 'lib/dice_bag/dice_bag_file.rb', line 9

def file
  @file
end

#filenameObject (readonly)

Returns the value of attribute filename.



9
10
11
# File 'lib/dice_bag/dice_bag_file.rb', line 9

def filename
  @filename
end

Instance Method Details

#assert_existenceObject



13
14
15
# File 'lib/dice_bag/dice_bag_file.rb', line 13

def assert_existence
  raise "File #{@file} not found. Configuration file not created" unless File.exist?(@file)
end

#should_write?(new_contents) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dice_bag/dice_bag_file.rb', line 23

def should_write?(new_contents)
  return true if @@overwrite_all || !File.exist?(file)
  return false if diff(file, new_contents).empty?

  # rubocop:disable Metrics/BlockLength
  loop do
    puts "Overwrite #{file} ?    Recommended: Yes. "
    puts " [Y]es, [n]o, [a]ll files, [q]uit, [d]show diff"
    answer = $stdin.gets
    answer &&= answer.chars.first.downcase
    case answer
    when "y"
      return true
    when "n"
      return false
    when "a"
      return @@overwrite_all = true
    when "q"
      exit
    when "d"
      puts diff(file, new_contents)
    else
      return true
    end
  end
  # rubocop:enable Metrics/BlockLength
end

#write(contents) ⇒ Object



17
18
19
20
21
# File 'lib/dice_bag/dice_bag_file.rb', line 17

def write(contents)
  File.open(@file, "w") do |file|
    file.puts(contents)
  end
end