Class: Snippit::CLI::Save

Inherits:
Object
  • Object
show all
Includes:
IO, Slugify
Defined in:
lib/snippit/cli/save.rb

Overview

Snippit::CLI::Save saves a new snippet, or overwrites an existing one.

Instance Method Summary collapse

Methods included from Slugify

#slugify

Methods included from IO

#definitions, #definitions_file, #delete_snippet, #filepath!, #read_snippet, #snippit_dir, #snippit_dir!, #write_definition, #write_snippet

Constructor Details

#initialize(path, force: false, name: nil, slug: nil) ⇒ Save

Initializes a new Save subcommand.

Parameters:

  • path (String)

    The path to the snippet to save

  • force (TrueClass|FalseClass) (defaults to: false)


18
19
20
21
22
23
# File 'lib/snippit/cli/save.rb', line 18

def initialize(path, force: false, name: nil, slug: nil)
  @force = force
  @path = path
  @name = name
  @slug = slug
end

Instance Method Details

#startInteger

Starts the save subcommand.

Returns:

  • (Integer)

    the exit code



28
29
30
31
32
33
# File 'lib/snippit/cli/save.rb', line 28

def start
  base_name = File.basename(@path)
  slug = @slug || slugify(base_name)

  handle(slug) { write_snippet(@name || base_name, slug, File.read(@path), @force) }
end