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_filepath))
  end
end

#determine_next_index(filename) ⇒ Object



78
79
80
81
82
83
84
85
# File 'app/models/utils/destinationfilewriter.rb', line 78

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

#directoryObject



33
34
35
36
# File 'app/models/utils/destinationfilewriter.rb', line 33

def directory
  check_if_file_still_exists
  File.dirname(@snip_file_name)
end

#find_all_snippet_filesObject



43
44
45
46
47
# File 'app/models/utils/destinationfilewriter.rb', line 43

def find_all_snippet_files
  snippet_files = []
  Dir.glob(directory + "/my_snips.*").each { |file| snippet_files << file }
  snippet_files
end

#full_file_directoryObject



144
145
146
# File 'app/models/utils/destinationfilewriter.rb', line 144

def full_file_directory
  File.absolute_path(@snip_file_name)
end

#log_filepathObject



148
149
150
# File 'app/models/utils/destinationfilewriter.rb', line 148

def log_filepath
  LOG_PATH
end

#reindex_allObject



103
104
105
# File 'app/models/utils/destinationfilewriter.rb', line 103

def reindex_all
  find_all_snippet_files.each {|file| reindexer(file)}
end

#reindexer(filename) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/utils/destinationfilewriter.rb', line 87

def reindexer(filename)
  index = 1
  file_array = []
  File.open(filename, "r+").each do |line|
    if line.match(/\*\*\*\* Snippet \d+:/)
      line.sub!(/Snippet \d+:/, "Snippet #{index}:")
      file_array << line
      index += 1
    else
      file_array << line
    end
    line.gsub!("\t", "  ")
  end
  rewrite_file(filename,file_array)
end

#restore_whitespace(filename, array) ⇒ Object



107
108
109
110
# File 'app/models/utils/destinationfilewriter.rb', line 107

def restore_whitespace(filename, array)
  array.map! { |snippet| snippet.strip + "\n\n\n\n" }
  rewrite_file(filename, array)
end

#return_display_fileObject



38
39
40
41
# File 'app/models/utils/destinationfilewriter.rb', line 38

def return_display_file
  check_if_file_still_exists
  @snip_file_name
end

#rewrite_file(filename, array) ⇒ Object



112
113
114
115
116
117
118
# File 'app/models/utils/destinationfilewriter.rb', line 112

def rewrite_file(filename, array)
  File.open(filename, "w") do |file|
    array.each do |line|
      file << line
    end
  end
end

#run(snippet_array, type = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/utils/destinationfilewriter.rb', line 49

def run(snippet_array, type=nil)
  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)
  when "erb"
    filename = directory + "/my_snips.erb"
    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



67
68
69
70
71
72
73
74
75
76
# File 'app/models/utils/destinationfilewriter.rb', line 67

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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/models/utils/destinationfilewriter.rb', line 120

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, snip_object.filename)
      file << "\n"
      file << snip_object.code
      file << "\n\n\n"
      unless type || snip_object.line == nil
        log = ViewFormatter.snip_terminal_status(snip_object.filename, snip_object.line)
        puts log
        write_to_log_file(log)
      end
    end
  end
  reindexer(filename)
end

#write_to_log_file(log) ⇒ Object



138
139
140
141
142
# File 'app/models/utils/destinationfilewriter.rb', line 138

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