Module: Snippit::IO

Included in:
CLI::Delete, CLI::Get, CLI::List, CLI::Save
Defined in:
lib/snippit/io.rb

Overview

Provides IO functions for Snippit.

Instance Method Summary collapse

Instance Method Details

#definitionsObject

Loads the ~/.snippit/.__definitions__.yml file if it exists, or returns an empty hash.



66
67
68
69
70
# File 'lib/snippit/io.rb', line 66

def definitions
  return {} unless File.exist?(definitions_file)

  @definitions ||= YAML.load_file(definitions_file)
end

#definitions_fileObject

Returns the path to the ~/.snippit/.__definitions__.yml file.



78
79
80
# File 'lib/snippit/io.rb', line 78

def definitions_file
  File.expand_path('.__definitions__.yml', snippit_dir)
end

#delete_snippet(slug) ⇒ Object

Removes a code snippet.

Parameters:

  • slug (String)

    The name of the file. Relative to the ~/.snippit directory

Raises:



38
39
40
41
42
43
# File 'lib/snippit/io.rb', line 38

def delete_snippet(slug)
  raise ReservedFilenameError, slug if slug == '.__definitions__.yml'

  File.delete(filepath!(slug))
  write_definition(definitions.reject { |k, _v| k == slug })
end

#filepath!(slug) ⇒ Object

Gets the filepath to the snippet.

Also creates the ~/.snippit directory if it does not exist.

Parameters:

  • slug (String)

    The name of the snippet.



50
51
52
# File 'lib/snippit/io.rb', line 50

def filepath!(slug)
  File.expand_path(slug, snippit_dir!)
end

#read_snippet(slug) ⇒ String

Reads teh contents of a code snippet.

Parameters:

  • slug (String)

    The name of the snippet.

Returns:

  • (String)

    The contents of the snippet.

Raises:



14
15
16
17
18
# File 'lib/snippit/io.rb', line 14

def read_snippet(slug)
  raise SnippetNotFoundError, slug unless File.exist?(filepath!(slug))

  File.read(filepath!(slug))
end

#snippit_dirObject

Returns the path to the ~/.snippit directory.



61
62
63
# File 'lib/snippit/io.rb', line 61

def snippit_dir
  File.expand_path('.snippit', Dir.home)
end

#snippit_dir!Object

Creates the ~/.snippit directory if it does not exist, and returns the path to it.



55
56
57
58
# File 'lib/snippit/io.rb', line 55

def snippit_dir!
  FileUtils.mkdir_p(snippit_dir)
  snippit_dir
end

#write_definition(definitions) ⇒ Object

Writes the definitions to the ~/.snippit/.__definitions__.yml file.



73
74
75
# File 'lib/snippit/io.rb', line 73

def write_definition(definitions)
  File.write(definitions_file, definitions.to_yaml)
end

#write_snippet(name, slug, contents, force) ⇒ Object

Writes the contents of a code snippet to a file.

Parameters:

  • name (String)

    The name of the snippet

  • slug (String)

    The name of the file. Relative to the ~/.snippit directory

  • contents (String)

    The contents of the file.

  • force (TrueClass|FalseClass)

    Whether or not to overwrite an existing file.

Raises:



26
27
28
29
30
31
32
33
# File 'lib/snippit/io.rb', line 26

def write_snippet(name, slug, contents, force)
  raise ReservedFilenameError, slug if slug == '.__definitions__.yml'

  raise SnippetExistsError, slug if File.exist?(filepath!(slug)) && !force

  File.write(filepath!(slug), contents)
  write_definition(definitions.merge(slug => name))
end