Class: Topicz::Commands::ReportCommand

Inherits:
RepositoryCommand show all
Defined in:
lib/topicz/commands/report_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 = []) ⇒ ReportCommand

Returns a new instance of ReportCommand.



7
8
9
10
11
12
# File 'lib/topicz/commands/report_command.rb', line 7

def initialize(config_file = nil, arguments = [])
  super(config_file)
  @week = Date.today.cweek
  @year = Date.today.cwyear
  option_parser.order! arguments
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/report_command.rb', line 28

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

  @repository.topics.each do |topic|
    journal = File.join(topic.fullpath, path)
    next unless File.exist? journal

    puts "## #{topic.title}"
    puts
    puts File.readlines(journal)
             .drop(2).join()          # Drop first 2 lines: title (week) and empty line
             .gsub(/^#(.*)/, '##\1')  # Add an extra '#' in front of every title
             .strip                   # Remove leading and ending whitespace
    puts
  end
end

#option_parserObject



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

def option_parser
  OptionParser.new do |options|
    options.banner = 'Usage: report'
    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 'Generates a weekly report from all journals across all topics in a single Markdown file.'
  end
end