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
-
#definitions ⇒ Object
Loads the ~/.snippit/.__definitions__.yml file if it exists, or returns an empty hash.
-
#definitions_file ⇒ Object
Returns the path to the ~/.snippit/.__definitions__.yml file.
-
#delete_snippet(slug) ⇒ Object
Removes a code snippet.
-
#filepath!(slug) ⇒ Object
Gets the filepath to the snippet.
-
#read_snippet(slug) ⇒ String
Reads teh contents of a code snippet.
-
#snippit_dir ⇒ Object
Returns the path to the ~/.snippit directory.
-
#snippit_dir! ⇒ Object
Creates the ~/.snippit directory if it does not exist, and returns the path to it.
-
#write_definition(definitions) ⇒ Object
Writes the definitions to the ~/.snippit/.__definitions__.yml file.
-
#write_snippet(name, slug, contents, force) ⇒ Object
Writes the contents of a code snippet to a file.
Instance Method Details
#definitions ⇒ Object
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_file ⇒ Object
Returns the path to the ~/.snippit/.__definitions__.yml file.
78 79 80 |
# File 'lib/snippit/io.rb', line 78 def definitions_file File.('.__definitions__.yml', snippit_dir) end |
#delete_snippet(slug) ⇒ Object
Removes a code snippet.
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.
50 51 52 |
# File 'lib/snippit/io.rb', line 50 def filepath!(slug) File.(slug, snippit_dir!) end |
#read_snippet(slug) ⇒ String
Reads teh contents of a code snippet.
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_dir ⇒ Object
Returns the path to the ~/.snippit directory.
61 62 63 |
# File 'lib/snippit/io.rb', line 61 def snippit_dir File.('.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.
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 |