Class: Topicz::Commands::JournalCommand

Inherits:
EditorCommand show all
Defined in:
lib/topicz/commands/journal_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) ⇒ JournalCommand

Returns a new instance of JournalCommand.



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

def initialize(config_file = nil, arguments = [], kernel = Kernel)
  super(config_file)
  @kernel = kernel
  @strict = false
  @week = Date.today.cweek
  @year = Date.today.cwyear
  option_parser.order! arguments
  @filter = arguments.join ' '
end

Instance Method Details

#executeObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/topicz/commands/journal_command.rb', line 45

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

  year = @year.to_s
  week = @week.to_s.rjust(2, '0')
  path = File.join(path, "#{year}-week-#{week}.md")

  unless File.exists? path
    File.open(path, 'w') do |file|
      file.puts("# #{topic.title} - Week #{week}, #{year}")
    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
40
41
42
43
# File 'lib/topicz/commands/journal_command.rb', line 20

def option_parser
  OptionParser.new do |options|
    options.banner = 'Usage: journal <filter>'
    options.on('-s', '--strict', 'Do a full strict match on topic IDs only') do
      @strict = true
    end
    options.on('-w', '--week WEEK', 'Use week WEEK instead of the current week') do |week|
      @week = week.to_i
    end
    options.on('-y', '--year YEAR', 'Use year YEAR instead of the current year') do |year|
      @year = year.to_i
    end
    options.separator ''
    options.separator 'Opens the weekly journal 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.'
  end
end