ztk 0.2.7 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ztk.rb CHANGED
@@ -38,6 +38,7 @@ module ZTK
38
38
  autoload :Config, 'ztk/config'
39
39
  autoload :Logger, 'ztk/logger'
40
40
  autoload :Parallel, 'ztk/parallel'
41
+ autoload :Report, 'ztk/report'
41
42
  autoload :RescueRetry, 'ztk/rescue_retry'
42
43
  autoload :Spinner, 'ztk/spinner'
43
44
  autoload :SSH, 'ztk/ssh'
@@ -0,0 +1,98 @@
1
+ ################################################################################
2
+ #
3
+ # Author: Zachary Patten <zachary@jovelabs.net>
4
+ # Copyright: Copyright (c) Jove Labs
5
+ # License: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+ ################################################################################
20
+
21
+ require 'socket'
22
+ require 'timeout'
23
+
24
+ module ZTK
25
+
26
+ # ZTK::Report Error Class
27
+ #
28
+ # @author Zachary Patten <zachary@jovelabs.net>
29
+ class ReportError < Error; end
30
+
31
+ # @author Zachary Patten <zachary@jovelabs.net>
32
+ class Report < ZTK::Base
33
+
34
+ # @param [Hash] configuration Configuration options hash.
35
+ def initialize(configuration={})
36
+ super({
37
+ }.merge(configuration))
38
+ config.logger.debug { "config=#{config.send(:table).inspect}" }
39
+ end
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
+ def spreadsheet(dataset, headers, &block)
54
+ !block_given? and log_and_raise(ReportError, "You must supply a block!")
55
+ headers.nil? and log_and_raise(ReportError, "Headers can not be nil!")
56
+ dataset.nil? and log_and_raise(ReportError, "Dataset can not be nil!")
57
+
58
+ rows = Array.new
59
+ max_lengths = OpenStruct.new
60
+ headers = headers.map(&:downcase).map(&:to_sym)
61
+
62
+ dataset.each do |data|
63
+ rows << block.call(data)
64
+ end
65
+
66
+ headers.each do |header|
67
+ maximum = [header, rows.collect{ |r| r.send(header) }].flatten.map(&:to_s).map(&:length).max
68
+ max_lengths.send("#{header}=", maximum)
69
+ end
70
+
71
+ header_line = headers.collect do |header|
72
+ "%-#{max_lengths.send(header)}s" % header.to_s.upcase
73
+ end
74
+ header_line = format_row(header_line)
75
+
76
+ config.stdout.puts(format_header(headers, max_lengths))
77
+ config.stdout.puts(header_line)
78
+ config.stdout.puts(format_header(headers, max_lengths))
79
+
80
+ rows.each do |row|
81
+ row_line = headers.collect do |header|
82
+ "%-#{max_lengths.send(header)}s" % row.send(header)
83
+ end
84
+ row_line = format_row(row_line)
85
+ config.stdout.puts(row_line)
86
+ end
87
+
88
+ config.stdout.puts(format_header(headers, max_lengths))
89
+
90
+ OpenStruct.new(:rows => rows, :max_lengths => max_lengths, :width => (2 + max_lengths.send(:table).values.reduce(:+) + ((headers.count * 3) - 3) + 2))
91
+ end
92
+
93
+ def list
94
+ end
95
+
96
+ end
97
+
98
+ end
@@ -19,5 +19,5 @@
19
19
  ################################################################################
20
20
 
21
21
  module ZTK
22
- VERSION = "0.2.7" unless const_defined?(:VERSION)
22
+ VERSION = "0.3.0" 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.2.7
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -180,6 +180,7 @@ files:
180
180
  - lib/ztk/config.rb
181
181
  - lib/ztk/logger.rb
182
182
  - lib/ztk/parallel.rb
183
+ - lib/ztk/report.rb
183
184
  - lib/ztk/rescue_retry.rb
184
185
  - lib/ztk/spinner.rb
185
186
  - lib/ztk/ssh.rb
@@ -216,7 +217,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
216
217
  version: '0'
217
218
  segments:
218
219
  - 0
219
- hash: 2970796941377044564
220
+ hash: 3083188834271822862
220
221
  required_rubygems_version: !ruby/object:Gem::Requirement
221
222
  none: false
222
223
  requirements:
@@ -225,7 +226,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
225
226
  version: '0'
226
227
  segments:
227
228
  - 0
228
- hash: 2970796941377044564
229
+ hash: 3083188834271822862
229
230
  requirements: []
230
231
  rubyforge_project:
231
232
  rubygems_version: 1.8.24