Class: Cardigan::Command::TotalCards

Inherits:
Object
  • Object
show all
Defined in:
lib/cardigan/command/total_cards.rb

Instance Method Summary collapse

Constructor Details

#initialize(repository, io) ⇒ TotalCards

Returns a new instance of TotalCards.



2
3
4
# File 'lib/cardigan/command/total_cards.rb', line 2

def initialize repository, io
  @repository, @io = repository, io
end

Instance Method Details

#execute(text) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/cardigan/command/total_cards.rb', line 6

def execute text
  count_field, *grouping_fields = text.scan(/\w+/)
  counts = {}
  @repository.cards.each do |card|
    key = grouping_fields.map {|key| card[key] ? card[key] : ''}
    value = card[count_field].to_i
    counts[key] = counts[key] ? counts[key] + value : value
  end
  counts.keys.sort.each {|key| @io.say "#{counts[key]} #{key.inspect}"}
end