Class: Topicz::Commands::CreateCommand

Inherits:
RepositoryCommand show all
Defined in:
lib/topicz/commands/create_command.rb

Instance Method Summary collapse

Methods inherited from RepositoryCommand

#find_exactly_one_topic, #load_config, #load_repository, #process_excludes

Constructor Details

#initialize(config_file = nil, arguments = []) ⇒ CreateCommand

Returns a new instance of CreateCommand.



11
12
13
14
# File 'lib/topicz/commands/create_command.rb', line 11

def initialize(config_file = nil, arguments = [])
  super(config_file)
  @title = arguments.join(' ').strip
end

Instance Method Details

#executeObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/topicz/commands/create_command.rb', line 28

def execute
  if @repository.exist_title?(@title)
    raise "Topic already exists: '#{@title}'."
  end
  path = Zaru.sanitize! @title
  fullpath = File.join(@repository.root, path)
  if File.exist?(fullpath)
    raise "Directory already exists: '#{path}'."
  end
  FileUtils.mkdir fullpath
  Topicz::DIRECTORIES.each { |dir| FileUtils.mkdir(File.join(fullpath, dir)) }
  if path != @title
    File.open(File.join(fullpath, Topicz::TOPIC_FILE), 'w') do | file |
      file.write(YAML.dump({'title' => @title}))
    end
  end
  puts "Created topic '#{@title}'"
end

#option_parserObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/topicz/commands/create_command.rb', line 16

def option_parser
  OptionParser.new do |options|
    options.banner = 'Usage: create <name>'
    options.separator ''
    options.separator 'Creates a new topic with the specified name'
    options.separator ''
    options.separator 'Basically all this command does is create a new directory structure to hold a topic.'
    options.separator ''
    options.separator 'It is an error if a topic with the specified name already exists.'
  end
end