show_data 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eafd7abc3122b12cb30090e0ca8ab760edbeccd9
4
+ data.tar.gz: e05b91f2ddd06417c274a211fded767567ea8d36
5
+ SHA512:
6
+ metadata.gz: 4c8a1e035ab7a6d3fd7993163df90dffc0f6d4816569cf2f39d49fb97913e6ca1314a979cab33f91c81a469f6cc1a219650e2f69e44edac6ac5c9380464f5224
7
+ data.tar.gz: 3bcf645d3838316bc648f5d615ad9fd23a55e372c607358833ad5379b903304c0f7501846fb171e239aa1086087037cbe56e3e7449d1884e56081e8574e2402c
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Alan Stebbens
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,135 @@
1
+ # show-data.rb
2
+ #
3
+ # show_data -- show the results of any data a pretty way
4
+ #
5
+ # Copyright (C) 2008-2013 Alan K. Stebbens <aks@stebbens.org>
6
+ #
7
+ # This program is free software; you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation; either version 2 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program; if not, write to the Free Software
19
+ # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
+ #
21
+ # Alan K. Stebbens, aks@stebbens.org, Jan 2008
22
+ # $Revision$
23
+ # $Id$
24
+ #
25
+ # 2008/10/14 aks:
26
+ # Added support for OpenStruct objects
27
+ # Fixed symbol key representation
28
+
29
+ #require 'ostruct' # don't need this if we're careful
30
+
31
+ def format_data(data, indent=0)
32
+ s = ''
33
+ klass = data.class
34
+ if (klass == Array)
35
+ s += format_array( data, indent )
36
+ elsif klass == Hash
37
+ s += format_hash( data.keys, data.values, indent )
38
+ elsif %w( Time Date DateTime ).include?(klass.to_s)
39
+ s += data.to_s # use default formatting
40
+ elsif klass.to_s == "OpenStruct"
41
+ s += (prefix = "#<OpenStruct")
42
+ hash = data.marshal_dump
43
+ s += format_hash( hash.keys, hash.values, indent + prefix.length, ' ', '>' )
44
+ elsif data.inspect =~ /^(\#<struct \w+)/ # structure?
45
+ s += $1 + ' '
46
+ s += format_hash(data.members, data.values, indent + $1.length + 1, ' ', '>' )
47
+ elsif data.respond_to?(:attributes)
48
+ attrs = data.attributes
49
+ if (text = data.inspect) =~ /^(\#<[^:>]+: )/
50
+ s += (prefix = $1)
51
+ else
52
+ s += (prefix = text.split(' ').first)
53
+ end
54
+ s += format_hash(attrs.keys, attrs.values, indent + prefix.length, ' ', '>')
55
+ elsif data.respond_to?(:instance_variables) and (vars = data.instance_variables.sort).size > 0
56
+ s += (prefix = data.inspect.split(' ').first) + ' '
57
+ s += format_hash(vars, vars.map{|v| data.instance_eval(v.to_s)}, indent + prefix.length + 1, ' ', '>')
58
+ else
59
+ s += data.inspect
60
+ end
61
+ s += "\n" if indent == 0
62
+ s
63
+ end
64
+
65
+ def format_array( data, indent, open_char = '[', close_char = ']' )
66
+ indexlen = data.size.to_s.length
67
+ s = "["
68
+ indexfmt = "[%0#{indexlen}d]: "
69
+ nextindent = indent + indexlen + 5
70
+ indentstr = ' ' * nextindent
71
+ data.each_index do |x|
72
+ s += sprintf(indexfmt, x)
73
+ s += format_data(data[x], nextindent)
74
+ s += sprintf(",\n" + ' '*indent + ' ') unless x == data.length - 1
75
+ end
76
+ s += "\n" + ' ' * indent + ']'
77
+ end
78
+
79
+ # format a hash
80
+ # args: keys, values, indent, open char, close char, right-align
81
+ # returns: formatted string
82
+ #
83
+ # if the hash looks like this: { 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }
84
+ # then display it like this:
85
+ #
86
+ # { 'key1' => 'value1',
87
+ # 'aLongkey2' => 'value2',
88
+ # 'key3' => 'value3'
89
+ # }
90
+ #
91
+ # the formatter tries to align the '=>' and everything around it, unless the
92
+ # FALSE object is passed as the "right_align" value.
93
+ #
94
+ # Recursively format nested object values. The '{' and '} are defaults.
95
+ #
96
+
97
+ def format_hash( hash_keys, hash_values, indent, open_char = '{', close_char = '}', right_align = nil)
98
+ s = open_char + ' '
99
+ maxkeylen = 0
100
+ if right_align or right_align.nil? # align maybe?
101
+ #complex = 0
102
+ hash_keys.each_index do |x|
103
+ k, v = hash_keys[x], hash_values[x]
104
+ kl = k.class == Symbol ? 1 + k.to_s.length : 2 + k.length
105
+ maxkeylen = kl if maxkeylen < kl
106
+ #complex += v.size if v.class == Hash || v.class == Array || v.class == OpenStruct
107
+ end
108
+ # if the data complexity is < 60%, and the maxkeylen is defined, then allow right-align
109
+ #maxkeylen = 0 if hash_keys.size > 0 && (complex.to_f / hash_keys.size.to_f) < 0.6
110
+ end
111
+ keyfmt = maxkeylen > 0 ? "%#{maxkeylen}s => " : "%s => "
112
+ indentstr = ' ' * indent + ' '
113
+ count = 0
114
+ hash_keys.each_index do |x|
115
+ key, val = hash_keys[x], hash_values[x]
116
+ if key.class == Symbol # Symbols have different representation
117
+ formatted_key = sprintf(keyfmt, ':' + key.to_s) # :somesymbol
118
+ else
119
+ formatted_key = sprintf(keyfmt, "'" + key + "'") # 'somekey'
120
+ end
121
+ s += formatted_key
122
+ vs = format_data(val, indent + formatted_key.size + 2)
123
+ s += vs
124
+ count += 1
125
+ s += ",\n" + indentstr unless count >= hash_keys.size
126
+ end
127
+ s += "\n" + ' ' * indent + close_char
128
+ end
129
+
130
+ def show_data(data, indent=0)
131
+ puts format_data(data, indent)
132
+ end
133
+
134
+ # end of show-data.rb
135
+ # vim: sw=2 ai
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+ # test-show-data.rb
3
+ #
4
+ # Test show-data.rb
5
+ #
6
+ # Alan K. Stebbens <aks@stebbens.org>
7
+
8
+ $:.unshift '.', '../test', 'test'
9
+
10
+ require 'helper'
11
+
12
+ require 'date'
13
+ require 'time'
14
+
15
+ array = [ 1, 2, 3, 10, 11, 12, 1.2, 34.567, 'apple', 'banana', 'cherry', :symbol1, :symbol2,
16
+ Date.parse("2005-12-25"), Time.parse("2006-07-04 12:01:02") ]
17
+ show_data array
18
+
19
+ ahash = { 'a' => 'apple', 'b' => 'banana', 'c' => 'cherry' }
20
+ show_data ahash
21
+
22
+ nested = [ [ 'a', 1.2, 'b' ], [ 'd', :symbol ], [ [ 'ape', 'monkey', 'chimp' ], [ 'banana', 'leaves' ], 2.99 ] ]
23
+ show_data nested
24
+
25
+ combo = [ ahash, ahash.invert, nested ]
26
+ show_data combo
27
+
28
+ puts ''
29
+ puts "Testing Struct display"
30
+
31
+ Vars = Struct::new( :aVar, :varB, :varC, :reallyLongVarName, :andAnotherVar )
32
+ vars1 = Vars.new( 'firstValue', :secondValue, %w( the third value ),
33
+ { :a => 'first', :nother => '2nd', :test => 'last item' },
34
+ nested )
35
+ show_data vars1
36
+
37
+ puts ''
38
+ puts "Testing OpenStruct display .."
39
+
40
+ require 'ostruct'
41
+ data = OpenStruct.new(ahash.invert)
42
+ show_data data
43
+
44
+ puts 'Testing a complicated OpenStruct ..'
45
+ data.complicated = vars1
46
+ show_data data
47
+
48
+ class TestObj
49
+ attr_accessor :first, :second, :third
50
+ def initialize( first='first thing', second='second thing', third='third thing')
51
+ @first = first
52
+ @second = second
53
+ @third = third
54
+ self
55
+ end
56
+ end
57
+
58
+ testo1 = TestObj.new(1, 2, 3)
59
+ testo2 = TestObj.new('a', 'b', 'c')
60
+ testo3 = TestObj.new('I', 'II', 'III')
61
+ show_data testo1
62
+ show_data testo2
63
+ show_data testo3
64
+ testo4 = TestObj.new(testo1, testo2, testo3)
65
+ show_data testo4
66
+
67
+ exit
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: show_data
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Alan Stebbens
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rdoc
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: jeweler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: "\tFormat or pretty-print Ruby data, including arrays, hashes, and structs.\n\tThe
56
+ keys ond values f the arrays and hashes are aligned for easy readability.\n\tObject
57
+ attributes are similarly aligned.\n"
58
+ email: aks@stebbens.org
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - LICENSE.txt
64
+ - lib/show_data.rb
65
+ - test/test_show_data.rb
66
+ homepage: http://github.com/aks/show_data
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 2.0.3
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Format or pretty-print Ruby data
90
+ test_files:
91
+ - test/test_show_data.rb