Class: ExpandSync::AText

Inherits:
Object
  • Object
show all
Defined in:
lib/expandsync/atext.rb

Overview

AText Class

Constant Summary collapse

OUTPUT_FILENAME =

The output filename

'aText-snippets.csv'
OUTPUT_PATH =

The output filepath

ENV['HOME']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(csv_filepath, custom_output_path) ⇒ void

Initialize by loading snippets from an aText CSV.

Parameters:

  • csv_filepath (String)

    The filepath to the aText CSV



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/expandsync/atext.rb', line 27

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

Instance Attribute Details

#output_fileString

Stores the output filepath.

Returns:

  • (String)


14
15
16
# File 'lib/expandsync/atext.rb', line 14

def output_file
  @output_file
end

#snippet_csvString

Stores a CSV string of the snippets.

Returns:

  • (String)


18
19
20
# File 'lib/expandsync/atext.rb', line 18

def snippet_csv
  @snippet_csv
end

#snippetsArray

Stores an array of snippets.

Returns:

  • (Array)


22
23
24
# File 'lib/expandsync/atext.rb', line 22

def snippets
  @snippets
end

Instance Method Details

#construct_data(new_snippets) ⇒ String

Outputs a CSV listing of the supplied snippets

Parameters:

  • new_snippets (Array)

    The snippet array to use

Returns:

  • (String)


54
55
56
# File 'lib/expandsync/atext.rb', line 54

def construct_data(new_snippets)
  @snippet_csv = CSV.generate { |csv| new_snippets.each { |s| csv << [s[0], s[1]] } }
end

#savevoid

This method returns an undefined value.

Saves the current snippets to Settings.textexpander.



60
61
62
# File 'lib/expandsync/atext.rb', line 60

def save
  File.open(@output_file, 'w') {|f| f.write(@snippet_csv) }
end