lib/expandsync/atext.rb in expandsync-0.1.3 vs lib/expandsync/atext.rb in expandsync-0.2.0

- old
+ new

@@ -1,69 +1,64 @@ -# ====================================================== -# AText Class -# ====================================================== -class AText - # ==================================================== - # Constants - # ==================================================== - OUTPUT_FILENAME = 'aText-snippets.csv' - OUTPUT_PATH = ENV['HOME'] +require 'csv' - # ==================================================== - # Attributes - # ==================================================== - attr_accessor :output_file, :snippet_csv, :snippets +module ExpandSync + # AText Class + class AText + # The output filename + OUTPUT_FILENAME = 'aText-snippets.csv' - # ==================================================== - # Methods - # ==================================================== - # ---------------------------------------------------- - # initialize method - # - # @param csv_filepath The filepath to the aText CSV - # @return Void - # ---------------------------------------------------- - def initialize(csv_filepath, custom_output_path) - if custom_output_path.nil? - @output_file = File.join(OUTPUT_PATH, OUTPUT_FILENAME) - else - if Dir.exists?(File.dirname(custom_output_path)) - @output_file = custom_output_path + # The output filepath + OUTPUT_PATH = ENV['HOME'] + + # Stores the output filepath. + # @return [String] + attr_accessor :output_file + + # Stores a CSV string of the snippets. + # @return [String] + attr_accessor :snippet_csv + + # Stores an array of snippets. + # @return [Array] + attr_accessor :snippets + + # Initialize by loading snippets from an aText CSV. + # @param [String] csv_filepath The filepath to the aText CSV + # @return [void] + def initialize(csv_filepath, custom_output_path) + if custom_output_path.nil? + @output_file = File.join(OUTPUT_PATH, OUTPUT_FILENAME) else - fail "Invalid output directory for aText: #{ custom_output_path }" + if Dir.exists?(File.dirname(custom_output_path)) + @output_file = custom_output_path + else + fail "Invalid output directory for aText: #{ custom_output_path }" + end end - end - - if File.exists?(csv_filepath) && File.extname(csv_filepath) == '.csv' - begin - @snippets = CSV.read(csv_filepath) - rescue - fail "Could not load CSV from file: #{ csv_filepath }" + + if File.exists?(csv_filepath) && File.extname(csv_filepath) == '.csv' + begin + @snippets = CSV.read(csv_filepath) + rescue + fail "Could not load CSV from file: #{ csv_filepath }" + end + + @snippets.each { |s| s[2] = 'aText' } + else + fail "Invalid CSV file: #{ csv_filepath }" end - - @snippets.each { |s| s[2] = 'aText' } - else - fail "Invalid CSV file: #{ csv_filepath }" end - end - - # ---------------------------------------------------- - # construct_data method - # - # Outputs a CSV listing of the supplied snippets - # @param new_snippets The snippet array to use - # @return String - # ---------------------------------------------------- - def construct_data(new_snippets) - @snippet_csv = CSV.generate { |csv| new_snippets.each { |s| csv << [s[0], s[1]] } } - end - # ---------------------------------------------------- - # save method - # - # Saves the current snippets to Settings.textexpander. - # @return Void - # ---------------------------------------------------- - def save - File.open(@output_file, 'w') {|f| f.write(@snippet_csv) } + # Outputs a CSV listing of the supplied snippets + # @param [Array] new_snippets The snippet array to use + # @return [String] + def construct_data(new_snippets) + @snippet_csv = CSV.generate { |csv| new_snippets.each { |s| csv << [s[0], s[1]] } } + end + + # Saves the current snippets to Settings.textexpander. + # @return [void] + def save + File.open(@output_file, 'w') {|f| f.write(@snippet_csv) } + end end end \ No newline at end of file