Module: DestinationFileWriter

Extended by:
DestinationFileWriter
Included in:
DestinationFileWriter
Defined in:
app/models/utils/destinationfilewriter.rb

Instance Method Summary collapse

Instance Method Details

#check_config_file_for_snip_fileObject



13
14
15
16
17
18
19
# File 'app/models/utils/destinationfilewriter.rb', line 13

def check_config_file_for_snip_file
  if snip_filepath
    check_if_file_still_exists
  else
    abort(ViewFormatter.specify_path)
  end
end

#check_if_file_still_existsObject



25
26
27
28
29
30
31
# File 'app/models/utils/destinationfilewriter.rb', line 25

def check_if_file_still_exists
  if File.exist?(snip_filepath)
    @snip_file_name = snip_filepath
  else
    abort(ViewFormatter.output_file_not_found(snip_file))
  end
end

#determine_next_index(filename) ⇒ Object



60
61
62
63
64
65
66
67
# File 'app/models/utils/destinationfilewriter.rb', line 60

def determine_next_index(filename)
  last_snip_scan = File.readlines(filename).reverse.join
  if last_snip_scan.match(/\*\*\*\* Snippet (\d+):/)
    last_snip_scan.match(/\*\*\*\* Snippet (\d+):/).captures[0].to_i+1
  else
    1
  end
end

#full_file_directoryObject



92
93
94
# File 'app/models/utils/destinationfilewriter.rb', line 92

def full_file_directory
  File.absolute_path(@snip_file_name)
end

#log_filepathObject



96
97
98
# File 'app/models/utils/destinationfilewriter.rb', line 96

def log_filepath
  LOG_PATH
end

#run(snippet_array, type = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/utils/destinationfilewriter.rb', line 33

def run(snippet_array, type=nil)
  directory = File.dirname(@snip_file_name)
  case type
  when "rb"
    filename = directory + "/my_snips.rb"
    File.new(filename, 'w') unless File.exist?(filename)
  when "js"
    filename = directory + "/my_snips.js"
    File.new(filename, 'w') unless File.exist?(filename)
  else
    filename = @snip_file_name
  end
  write_file(snippet_array, filename, type)
end

#save_file_path_to_config_file(filedir) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'app/models/utils/destinationfilewriter.rb', line 49

def save_file_path_to_config_file(filedir)
  snip_file = File.absolute_path(filedir).chomp('/') + "/my_snips.txt"
  File.open(CONFIG_PATH, "w+") do |f|
    f << snip_file
  end
  unless File.exist?(snip_file)
    File.new(snip_file, 'w')
    abort(ViewFormatter.new_file(snip_file))
  end
end

#snip_filepathObject



21
22
23
# File 'app/models/utils/destinationfilewriter.rb', line 21

def snip_filepath
  File.readlines(CONFIG_PATH)[0]
end

#write_file(snippet_array, filename, type) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/utils/destinationfilewriter.rb', line 69

def write_file(snippet_array, filename, type)
  File.open(filename, "a") do |file|
    snippet_array.each_with_index do |snip_object, index|
      file << ViewFormatter.snippet_indexer(determine_next_index(filename)+index, snip_object.title, type)
      file << ViewFormatter.status_line(snip_object.line, type)
      file << "\n"
      file << snip_object.code
      file << "\n\n"
      unless type 
        log = ViewFormatter.snip_terminal_status(snip_object.filename, snip_object.line)
        puts log
        write_to_log_file(log)
      end
    end
  end
end

#write_to_log_file(log) ⇒ Object



86
87
88
89
90
# File 'app/models/utils/destinationfilewriter.rb', line 86

def write_to_log_file(log)
  File.open(LOG_PATH, "a") do |file|
    file << Time.now.strftime("%m-%d-%Y").to_s + " " + log + "\n"
  end
end