Sha256: 08483d64b01140ae2d661a61aff96c345c49e223b8e9b4121e81e522c7a1116f

Contents?: true

Size: 1.74 KB

Versions: 3

Compression:

Stored size: 1.74 KB

Contents

require 'csv'

module ExpandSync
  # AText Class
  class AText
    # The output filename
    OUTPUT_FILENAME = 'aText-snippets.csv'

    # 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
        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

      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
    end

    # 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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
expandsync-0.2.2 lib/expandsync/atext.rb
expandsync-0.2.1 lib/expandsync/atext.rb
expandsync-0.2.0 lib/expandsync/atext.rb