Class: Topicz::Commands::NoteCommand
- Inherits:
-
EditorCommand
- Object
- RepositoryCommand
- EditorCommand
- Topicz::Commands::NoteCommand
- Defined in:
- lib/topicz/commands/note_command.rb
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(config_file = nil, arguments = [], kernel = Kernel) ⇒ NoteCommand
constructor
A new instance of NoteCommand.
- #option_parser ⇒ Object
Methods inherited from EditorCommand
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
#execute ⇒ Object
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_parser ⇒ Object
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 || . = 'Usage: note <filter> [<title>]' .on('-s', '--strict', 'Do a full strict match on topic IDs only') do @strict = true end .separator '' .separator 'Creates a new note for the specified topic.' .separator '' .separator 'The filter specifies the text to search on. The text is matched against the topic\'s: ' .separator '- path on the filesystem' .separator '- id, if specified in the topic\'s topic.yaml file' .separator '- title, if specified in the topic\'s topic.yaml file' .separator '- aliases, if specified in the topic\'s topic.yaml file' .separator '' .separator 'The filter must return precisely one topic. Zero or more matches give an error.' .separator '' .separator 'The note title is optional. If omitted the title will be \'Unnamed note\'.' end end |