formatador 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -24,6 +24,7 @@ You use tags, similar to html, to set formatting options:
24
24
  == Extensions
25
25
 
26
26
  * display_table: takes an array of hashes. Each hash is a row, with the keys being the headers and values being the data. An optional second argument can specify which headers/columns to include and in what order they should appear.
27
+ * display_compact_table: Same as display_table, execpt that split lines are not drawn by default in the body of the table. If you need a split line, put a :split constant in the body array.
27
28
  * redisplay_progressbar: takes the current and total values as its first two arguments and redisplays a progressbar (until current = total and then it display_lines). An optional third argument represents the start time and will add an elapsed time counter.
28
29
 
29
30
  == Indentation
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
13
13
  ## If your rubyforge_project name is different, then edit it and comment out
14
14
  ## the sub! line in the Rakefile
15
15
  s.name = 'formatador'
16
- s.version = '0.1.3'
17
- s.date = '2011-04-03'
16
+ s.version = '0.1.4'
17
+ s.date = '2011-05-20'
18
18
  s.rubyforge_project = 'formatador'
19
19
 
20
20
  ## Make sure your summary is short. The description may be as long
@@ -3,7 +3,7 @@ require File.join(File.dirname(__FILE__), 'formatador', 'progressbar')
3
3
 
4
4
  class Formatador
5
5
 
6
- VERSION = '0.1.3'
6
+ VERSION = '0.1.4'
7
7
 
8
8
  STYLES = {
9
9
  :"\/" => "0",
@@ -103,7 +103,7 @@ class Formatador
103
103
  nil
104
104
  end
105
105
 
106
- %w{display display_line display_lines display_table indent parse redisplay redisplay_progressbar}.each do |method|
106
+ %w{display display_line display_lines display_table display_compact_table indent parse redisplay redisplay_progressbar}.each do |method|
107
107
  eval <<-DEF
108
108
  def self.#{method}(*args, &block)
109
109
  Thread.current[:formatador] ||= new
@@ -1,6 +1,13 @@
1
1
  class Formatador
2
-
3
2
  def display_table(hashes, keys = nil, &block)
3
+ new_hashes = hashes.inject([]) do |accum,item|
4
+ accum << :split unless accum.empty?
5
+ accum << item
6
+ end
7
+ display_compact_table(new_hashes, keys, &block)
8
+ end
9
+
10
+ def display_compact_table(hashes, keys = nil, &block)
4
11
  headers = keys || []
5
12
  widths = {}
6
13
  if hashes.empty? && keys
@@ -9,11 +16,13 @@ class Formatador
9
16
  end
10
17
  else
11
18
  for hash in hashes
19
+ next unless hash.respond_to?(:keys)
20
+
12
21
  for key in hash.keys
13
22
  unless keys
14
23
  headers << key
15
24
  end
16
- widths[key] = [key.to_s.length, widths[key] || 0, hash[key] && hash[key].to_s.length || 0].max
25
+ widths[key] = [ length(key), widths[key] || 0, hash[key] && length(hash[key]) || 0].max
17
26
  end
18
27
  headers = headers.uniq
19
28
  end
@@ -30,7 +39,7 @@ class Formatador
30
39
  split << '--+'
31
40
  else
32
41
  for header in headers
33
- widths[header] ||= header.to_s.length
42
+ widths[header] ||= length(header)
34
43
  split << ('-' * (widths[header] + 2)) << '+'
35
44
  end
36
45
  end
@@ -44,15 +53,26 @@ class Formatador
44
53
  display_line(split)
45
54
 
46
55
  for hash in hashes
47
- columns = []
48
- for header in headers
49
- datum = hash[header] || ''
50
- columns << "#{datum}#{' ' * (widths[header] - datum.to_s.length)}"
56
+ if hash.respond_to? :keys
57
+ columns = []
58
+ for header in headers
59
+ datum = hash[header] || ''
60
+ columns << "#{datum}#{' ' * (widths[header] - length(datum))}"
61
+ end
62
+ display_line("| #{columns.join(' | ')} |")
63
+ else
64
+ if hash == :split
65
+ display_line(split)
66
+ end
51
67
  end
52
- display_line("| #{columns.join(' | ')} |")
53
- display_line(split)
68
+ nil
54
69
  end
55
- nil
70
+ display_line(split)
56
71
  end
57
72
 
58
- end
73
+ private
74
+
75
+ def length(value)
76
+ value.to_s.gsub(PARSE_REGEX, '').length
77
+ end
78
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: formatador
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 3
10
- version: 0.1.3
9
+ - 4
10
+ version: 0.1.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - geemus (Wesley Beary)
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-03 00:00:00 -04:00
18
+ date: 2011-05-20 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency