Class: Topicz::Commands::NoteCommand

Inherits:
EditorCommand show all
Defined in:
lib/topicz/commands/note_command.rb

Instance Method Summary collapse

Methods inherited from EditorCommand

#editor

Methods inherited from RepositoryCommand

#find_exactly_one_topic, #load_config, #load_repository, #process_excludes

Constructor Details

#initialize(config_file = nil, arguments = [], kernel = Kernel) ⇒ NoteCommand

Returns a new instance of NoteCommand.



11
12
13
14
15
16
17
18
# File 'lib/topicz/commands/note_command.rb', line 11

def initialize(config_file = nil, arguments = [], kernel = Kernel)
  super(config_file)
  @kernel = kernel
  @strict = false
  option_parser.order! arguments
  @filter = arguments.shift
  @title = arguments.empty? ? nil : arguments.join(' ')
end

Instance Method Details

#executeObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/topicz/commands/note_command.rb', line 41

def execute
  topic = find_exactly_one_topic(@filter, @strict)
  path = File.join(topic.fullpath, Topicz::DIR_NOTES)
  FileUtils.mkdir(path) unless Dir.exist? path

  if @title
    date = DateTime.now.strftime('%Y-%m-%d')
    title = @title
    filename = Zaru.sanitize! "#{date} #{title}.md"
  else
    date = DateTime.now.strftime('%Y-%m-%d %H%M')
    title = 'Unnamed note'
    filename = "#{date}.md"
  end

  path = File.join(path, filename)

  unless File.exists? path
    File.open(path, 'w') do | file |
      file.puts("# #{topic.title} - #{title}")
    end
  end

  @kernel.exec "#{editor} \"#{path}\""
end

#option_parserObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/topicz/commands/note_command.rb', line 20

def option_parser
  OptionParser.new do |options|
    options.banner = 'Usage: note <filter> [<title>]'
    options.on('-s', '--strict', 'Do a full strict match on topic IDs only') do
      @strict = true
    end
    options.separator ''
    options.separator 'Creates a new note for the specified topic.'
    options.separator ''
    options.separator 'The filter specifies the text to search on. The text is matched against the topic\'s: '
    options.separator '- path on the filesystem'
    options.separator '- id, if specified in the topic\'s topic.yaml file'
    options.separator '- title, if specified in the topic\'s topic.yaml file'
    options.separator '- aliases, if specified in the topic\'s topic.yaml file'
    options.separator ''
    options.separator 'The filter must return precisely one topic. Zero or more matches give an error.'
    options.separator ''
    options.separator 'The note title is optional. If omitted the title will be \'Unnamed note\'.'
  end
end