cardigan 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,15 +1,36 @@
1
+ require 'cardigan/context'
2
+ require 'cardigan/text_report_formatter'
3
+
1
4
  class Cardigan::Command::CountCards
2
5
  def initialize repository, io
3
6
  @repository, @io = repository, io
4
7
  end
5
8
 
6
- def execute text
9
+ def execute text=''
7
10
  grouping_fields = text.scan(/\w+/)
11
+ lengths = Array.new(grouping_fields.size, 0)
8
12
  counts = {}
9
- @repository.cards.each do |card|
13
+ total = 0
14
+ @repository.each do |card|
15
+ grouping_fields.each_with_index do |grouping_field,index|
16
+ lengths[index] = card[grouping_field].size if card[grouping_field] and card[grouping_field].size > lengths[index]
17
+ end
10
18
  key = grouping_fields.map {|key| card[key] ? card[key] : ''}
11
19
  counts[key] = counts[key] ? counts[key] + 1 : 1
20
+ total += counts[key]
21
+ end
22
+
23
+ values = counts.keys.sort.map do |key|
24
+ hash = {'count' => counts[key]}
25
+ grouping_fields.each_with_index {|grouping_field,index| hash[grouping_field] = key[index] }
26
+ hash
12
27
  end
13
- counts.keys.sort.each {|key| @io.say "#{counts[key]} #{key.inspect}"}
28
+
29
+ values << {'count' => total} unless counts.size == 1
30
+
31
+ formatter = Cardigan::TextReportFormatter.new @io
32
+ grouping_fields.each_with_index {|grouping_field,index| formatter.add_column(grouping_field, lengths[index])}
33
+ formatter.add_column('count', 0)
34
+ formatter.output values, :suppress_index => true
14
35
  end
15
36
  end
@@ -1,3 +1,6 @@
1
+ require 'cardigan/context'
2
+ require 'cardigan/text_report_formatter'
3
+
1
4
  class Cardigan::Command::TotalCards
2
5
  def initialize repository, io
3
6
  @repository, @io = repository, io
@@ -6,11 +9,21 @@ class Cardigan::Command::TotalCards
6
9
  def execute text
7
10
  count_field, *grouping_fields = text.scan(/\w+/)
8
11
  counts = {}
9
- @repository.cards.each do |card|
10
- key = grouping_fields.map {|key| card[key] ? card[key] : ''}
12
+ @repository.each do |card|
13
+ key = grouping_fields.map {|grouping_field| card[grouping_field] ? card[grouping_field] : ''}
11
14
  value = card[count_field].to_i
12
15
  counts[key] = counts[key] ? counts[key] + value : value
13
16
  end
14
- counts.keys.sort.each {|key| @io.say "#{counts[key]} #{key.inspect}"}
17
+
18
+ values = counts.keys.sort.map do |key|
19
+ hash = {count_field => counts[key]}
20
+ grouping_fields.each_with_index {|grouping_field,index| hash[grouping_field] = key[index] }
21
+ hash
22
+ end
23
+
24
+ formatter = Cardigan::TextReportFormatter.new @io
25
+ grouping_fields.each {|grouping_field| formatter.add_column(grouping_field, 0)}
26
+ formatter.add_column(count_field, 0)
27
+ formatter.output values, :suppress_index => true
15
28
  end
16
29
  end
@@ -1,36 +1,41 @@
1
1
  module Cardigan
2
2
  class TextReportFormatter
3
+ INDEX_HEADING = 'index'
4
+
3
5
  def initialize io
4
6
  @io = io
5
7
  @columns = []
6
- @heading = 'index'
7
- @width = 2 + @heading.length + 1
8
8
  end
9
9
 
10
10
  def add_column name, length
11
11
  longer = [name.length, length].max
12
12
  @columns << [name, longer]
13
- @width += longer + 3
14
13
  end
15
14
 
16
- def output hashes
17
- hline
18
- row 'index', @columns.map {|tuple| tuple.first }
19
- hline
15
+ def output hashes, options={}
16
+ return if @columns.empty?
17
+ width = calculate_width(options[:suppress_index])
18
+ hline width
19
+ row INDEX_HEADING, @columns.map {|tuple| tuple.first }, options[:suppress_index]
20
+ hline width
20
21
  hashes.each_with_index do |h,i|
21
- first = (i+1).to_s
22
- values = @columns.map {|tuple| h[tuple.first]}
23
- row first, values
22
+ row (i+1).to_s, @columns.map {|tuple| h[tuple.first]}, options[:suppress_index]
24
23
  end
25
- hline
24
+ hline width
26
25
  end
27
26
  private
28
- def hline
29
- @io.say ' ' + '-' * @width
27
+ def hline width
28
+ @io.say ' ' + '-' * (width - 2)
30
29
  end
31
30
 
32
- def row first, values
33
- a = "| #{first.ljust(@heading.length)} | "
31
+ def calculate_width suppress_index
32
+ width = suppress_index ? 1 : INDEX_HEADING.length + 4
33
+ @columns.each {|column| width += column[1] + 3}
34
+ width
35
+ end
36
+
37
+ def row first, values, suppress_index
38
+ a = suppress_index ? "|" : "| #{first.ljust(INDEX_HEADING.length)} |"
34
39
  @columns.each_with_index do |tuple, index|
35
40
  a << " #{values[index].to_s.ljust(tuple[1])} |"
36
41
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cardigan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 8
9
+ version: 0.0.8
5
10
  platform: ruby
6
11
  authors:
7
12
  - Mark Ryall
@@ -9,29 +14,37 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-03-16 00:00:00 +11:00
17
+ date: 2010-04-10 00:00:00 +10:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: uuidtools
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ~>
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 1
30
+ - 1
23
31
  version: 2.1.1
24
- version:
32
+ type: :runtime
33
+ version_requirements: *id001
25
34
  - !ruby/object:Gem::Dependency
26
35
  name: activesupport
27
- type: :runtime
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
30
38
  requirements:
31
39
  - - ~>
32
40
  - !ruby/object:Gem::Version
41
+ segments:
42
+ - 2
43
+ - 3
44
+ - 5
33
45
  version: 2.3.5
34
- version:
46
+ type: :runtime
47
+ version_requirements: *id002
35
48
  description: |
36
49
  A command line utility that manages cards as individual files so that they can be added to a version control system
37
50
 
@@ -92,18 +105,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
92
105
  requirements:
93
106
  - - ">="
94
107
  - !ruby/object:Gem::Version
108
+ segments:
109
+ - 0
95
110
  version: "0"
96
- version:
97
111
  required_rubygems_version: !ruby/object:Gem::Requirement
98
112
  requirements:
99
113
  - - ">="
100
114
  - !ruby/object:Gem::Version
115
+ segments:
116
+ - 0
101
117
  version: "0"
102
- version:
103
118
  requirements: []
104
119
 
105
120
  rubyforge_project:
106
- rubygems_version: 1.3.5
121
+ rubygems_version: 1.3.6
107
122
  signing_key:
108
123
  specification_version: 3
109
124
  summary: command line utility for managing cards (tasks, bugs, stories or whatever)