Class: Topicz::Commands::StatsCommand

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

Constant Summary collapse

GLOB_PATTERN =
File.join("{#{Topicz::DIR_NOTES},#{Topicz::DIR_DOCUMENTS},#{Topicz::DIR_REFERENCE}}", '*')

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 = []) ⇒ StatsCommand

Returns a new instance of StatsCommand.


7
8
9
10
11
12
# File 'lib/topicz/commands/stats_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


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/topicz/commands/stats_command.rb', line 32

def execute
  date = Date.commercial(@year, @week)
  first = date.strftime('%Y-%m-%d')
  last = (date + 7).strftime('%Y-%m-%d')

  output = []
  @repository.topics.each do |topic|
    stats = find_files(topic, first, last)
    next if stats.empty?
    item = "#{topic.title}\n"
    stats.each {|path| item += "* #{path}\n"}
    output << item
  end

  puts output.join("\n") unless output.empty?
end

#find_files(topic, first, last) ⇒ Object


51
52
53
54
55
56
57
58
# File 'lib/topicz/commands/stats_command.rb', line 51

def find_files(topic, first, last)
  Dir.chdir(topic.fullpath) do
    Dir.glob(GLOB_PATTERN).select do |path|
      file = File.basename(path)
      file >= first && file < last
    end.sort
  end
end

#option_parserObject


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

def option_parser
  OptionParser.new do |options|
    options.banner = 'Usage: stats'
    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 weekly statistics across all topics.'
    options.separator ''
    options.separator 'This command reports on all Documents, Notes and Reference Material across all topics.'
    options.separator 'Contrary to what you might think, this command does NOT look at file timestamps!'
    options.separator 'Instead it expects each filename to start with a date in yyyy-mm-dd format.'
  end
end