Class: Snip::Command::Add

Inherits:
Object
  • Object
show all
Includes:
Snip
Defined in:
lib/snip/command/add.rb

Constant Summary

Constants included from Snip

DEFAULT_DIR, INSTALL_DIR, VERSION

Instance Method Summary collapse

Methods included from Snip

init, initialized?, snippet_exists?

Constructor Details

#initialize(args, options = {}) ⇒ Add

Returns a new instance of Add.



9
10
11
12
# File 'lib/snip/command/add.rb', line 9

def initialize(args, options={})
  @args = Array(args)
  @options = options
end

Instance Method Details

#executeObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/snip/command/add.rb', line 14

def execute
  name, content = @args
  raise NotInitializedError unless Snip::initialized?
  raise DuplicateSnippetNameError if Snip::snippet_exists?(name)

  file_path = File.join(Snip::INSTALL_DIR, name)
  if content.nil?
    if Snip::Util::Editor::open(file_path)
      if File.exists?(file_path)
        puts "Snippet created successfully"
      end
    end
  else
    File.open(file_path, 'w') do |f|
      f.puts content
    end
  end
end