ztk 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/ztk/report.rb +50 -14
  2. data/lib/ztk/version.rb +1 -1
  3. metadata +3 -3
@@ -38,18 +38,6 @@ module ZTK
38
38
  config.logger.debug { "config=#{config.send(:table).inspect}" }
39
39
  end
40
40
 
41
- def format_header(headers, lengths)
42
- line = headers.collect do |header|
43
- "-" * lengths.send(header)
44
- end
45
- ["+-", line.join("-+-"), "-+"].join.strip
46
- end
47
-
48
- def format_row(*args)
49
- spacer = " "
50
- [spacer, args, spacer].flatten.join(" | ").strip
51
- end
52
-
53
41
  def spreadsheet(dataset, headers, &block)
54
42
  !block_given? and log_and_raise(ReportError, "You must supply a block!")
55
43
  headers.nil? and log_and_raise(ReportError, "Headers can not be nil!")
@@ -87,10 +75,58 @@ module ZTK
87
75
 
88
76
  config.stdout.puts(format_header(headers, max_lengths))
89
77
 
90
- OpenStruct.new(:rows => rows, :max_lengths => max_lengths, :width => (2 + max_lengths.send(:table).values.reduce(:+) + ((headers.count * 3) - 3) + 2))
78
+ width = (2 + max_lengths.send(:table).values.reduce(:+) + ((headers.count * 3) - 3) + 2)
79
+
80
+ OpenStruct.new(:rows => rows, :max_lengths => max_lengths, :width => width)
81
+ end
82
+
83
+ def list(dataset, headers, &block)
84
+ !block_given? and log_and_raise(ReportError, "You must supply a block!")
85
+ headers.nil? and log_and_raise(ReportError, "Headers can not be nil!")
86
+ dataset.nil? and log_and_raise(ReportError, "Dataset can not be nil!")
87
+
88
+ rows = Array.new
89
+ max_lengths = OpenStruct.new
90
+ headers = headers.map(&:downcase).map(&:to_sym)
91
+
92
+ dataset.each do |data|
93
+ rows << block.call(data)
94
+ end
95
+
96
+ max_key_length = headers.collect{ |header| header.length }.max
97
+ max_value_length = rows.collect{ |row| headers.collect{ |header| row.send(header).to_s.length }.max }.max
98
+
99
+ width = (max_key_length + max_value_length + 2 + 2 + 2)
100
+
101
+ rows.each do |row|
102
+ config.stdout.puts("+#{"-" * width}+")
103
+ headers.each do |header|
104
+ entry_line = format_entry(header, max_key_length, row.send(header), max_value_length)
105
+ config.stdout.puts(entry_line)
106
+ end
107
+ end
108
+ config.stdout.puts("+#{"-" * width}+")
109
+
110
+ OpenStruct.new(:rows => rows, :max_key_length => max_key_length, :max_value_length => max_value_length, :width => width)
111
+ end
112
+
113
+
114
+ private
115
+
116
+ def format_header(headers, lengths)
117
+ line = headers.collect do |header|
118
+ "-" * lengths.send(header)
119
+ end
120
+ ["+-", line.join("-+-"), "-+"].join.strip
121
+ end
122
+
123
+ def format_row(*args)
124
+ spacer = " "
125
+ [spacer, args, spacer].flatten.join(" | ").strip
91
126
  end
92
127
 
93
- def list
128
+ def format_entry(key, key_length, value, value_length)
129
+ "| %#{key_length}s: %-#{value_length}s |" % [key.to_s.upcase, value.to_s]
94
130
  end
95
131
 
96
132
  end
@@ -19,5 +19,5 @@
19
19
  ################################################################################
20
20
 
21
21
  module ZTK
22
- VERSION = "0.3.0" unless const_defined?(:VERSION)
22
+ VERSION = "0.3.1" unless const_defined?(:VERSION)
23
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ztk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -217,7 +217,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
217
217
  version: '0'
218
218
  segments:
219
219
  - 0
220
- hash: 3083188834271822862
220
+ hash: -4389390539649954166
221
221
  required_rubygems_version: !ruby/object:Gem::Requirement
222
222
  none: false
223
223
  requirements:
@@ -226,7 +226,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
226
226
  version: '0'
227
227
  segments:
228
228
  - 0
229
- hash: 3083188834271822862
229
+ hash: -4389390539649954166
230
230
  requirements: []
231
231
  rubyforge_project:
232
232
  rubygems_version: 1.8.24