Module: DestinationFileWriter
- Extended by:
- DestinationFileWriter
- Included in:
- DestinationFileWriter
- Defined in:
- lib/models/utils/destinationfilewriter.rb
Instance Method Summary collapse
- #check_config_file_for_snip_file ⇒ Object
- #check_if_file_still_exists ⇒ Object
- #determine_next_index(filename) ⇒ Object
- #directory ⇒ Object
- #find_all_snippet_files ⇒ Object
- #full_file_directory ⇒ Object
- #log_filepath ⇒ Object
- #reindex_all ⇒ Object
- #reindexer(filename) ⇒ Object
- #restore_whitespace(filename, array) ⇒ Object
- #return_display_file ⇒ Object
- #rewrite_file(filename, array) ⇒ Object
- #run(snippet_array, type = nil) ⇒ Object
- #save_file_path_to_config_file(filedir) ⇒ Object
- #snip_filepath ⇒ Object
- #write_file(snippet_array, filename, type) ⇒ Object
- #write_to_log_file(log) ⇒ Object
Instance Method Details
#check_config_file_for_snip_file ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/models/utils/destinationfilewriter.rb', line 14 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_exists ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/models/utils/destinationfilewriter.rb', line 26 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
72 73 74 75 76 77 78 79 |
# File 'lib/models/utils/destinationfilewriter.rb', line 72 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 |
#directory ⇒ Object
34 35 36 37 |
# File 'lib/models/utils/destinationfilewriter.rb', line 34 def directory check_if_file_still_exists File.dirname(@snip_file_name) end |
#find_all_snippet_files ⇒ Object
44 45 46 47 48 |
# File 'lib/models/utils/destinationfilewriter.rb', line 44 def find_all_snippet_files snippet_files = [] Dir.glob(directory + "/my_snips.*").each { |file| snippet_files << file } snippet_files end |
#full_file_directory ⇒ Object
138 139 140 |
# File 'lib/models/utils/destinationfilewriter.rb', line 138 def full_file_directory File.absolute_path(@snip_file_name) end |
#log_filepath ⇒ Object
142 143 144 |
# File 'lib/models/utils/destinationfilewriter.rb', line 142 def log_filepath LOG_PATH end |
#reindex_all ⇒ Object
97 98 99 |
# File 'lib/models/utils/destinationfilewriter.rb', line 97 def reindex_all find_all_snippet_files.each {|file| reindexer(file)} end |
#reindexer(filename) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/models/utils/destinationfilewriter.rb', line 81 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
101 102 103 104 |
# File 'lib/models/utils/destinationfilewriter.rb', line 101 def restore_whitespace(filename, array) array.map! { |snippet| snippet.strip + "\n\n\n\n" } rewrite_file(filename, array) end |
#return_display_file ⇒ Object
39 40 41 42 |
# File 'lib/models/utils/destinationfilewriter.rb', line 39 def return_display_file check_if_file_still_exists @snip_file_name end |
#rewrite_file(filename, array) ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/models/utils/destinationfilewriter.rb', line 106 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
50 51 52 53 54 55 56 57 58 |
# File 'lib/models/utils/destinationfilewriter.rb', line 50 def run(snippet_array, type=nil) if Language.supports?(type) filename = directory + "/my_snips.#{type}" 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
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/models/utils/destinationfilewriter.rb', line 61 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_filepath ⇒ Object
22 23 24 |
# File 'lib/models/utils/destinationfilewriter.rb', line 22 def snip_filepath File.readlines(CONFIG_PATH)[0] end |
#write_file(snippet_array, filename, type) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/models/utils/destinationfilewriter.rb', line 114 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
132 133 134 135 136 |
# File 'lib/models/utils/destinationfilewriter.rb', line 132 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 |