Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#format_array(data, indent, open_char = '[', close_char = ']') ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/show_data.rb', line 65

def format_array( data, indent, open_char = '[', close_char = ']' )
  indexlen = data.size.to_s.length
  s = "["
  indexfmt = "[%0#{indexlen}d]: "
  nextindent = indent + indexlen + 5
  indentstr = ' ' * nextindent
  data.each_index do |x|
    s += sprintf(indexfmt, x)
    s += format_data(data[x], nextindent)
    s += sprintf(",\n" + ' '*indent + ' ') unless x == data.length - 1
  end
  s += "\n" + ' ' * indent + ']'
end

#format_data(data, indent = 0) ⇒ Object

require ‘ostruct’ # don’t need this if we’re careful



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/show_data.rb', line 31

def format_data(data, indent=0)
  s = ''
  klass = data.class
  if (klass == Array) 
    s += format_array( data, indent )
  elsif klass == Hash
    s += format_hash( data.keys, data.values, indent )
  elsif %w( Time Date DateTime ).include?(klass.to_s)
    s += data.to_s                      # use default formatting
  elsif klass.to_s == "OpenStruct"
    s += (prefix = "#<OpenStruct")
    hash = data.marshal_dump
    s += format_hash( hash.keys, hash.values, indent + prefix.length, ' ', '>' )
  elsif data.inspect =~ /^(\#<struct \w+)/	# structure?
    s += $1 + ' '
    s += format_hash(data.members, data.values, indent + $1.length + 1, ' ', '>' )
  elsif data.respond_to?(:attributes)
    attrs = data.attributes
    if (text = data.inspect) =~ /^(\#<[^:>]+: )/
      s += (prefix = $1)
    else
      s += (prefix = text.split(' ').first)
    end
    s += format_hash(attrs.keys, attrs.values, indent + prefix.length, ' ', '>')
  elsif data.respond_to?(:instance_variables) and (vars = data.instance_variables.sort).size > 0
    s += (prefix = data.inspect.split(' ').first) + ' '
    s += format_hash(vars, vars.map{|v| data.instance_eval(v.to_s)}, indent + prefix.length + 1, ' ', '>')
  else
    s += data.inspect
  end
  s += "\n" if indent == 0
  s
end

#format_hash(hash_keys, hash_values, indent, open_char = '{', close_char = '}', right_align = nil) ⇒ Object

format a hash

args: keys, values, indent, open char, close char, right-align

returns: formatted string

if the hash looks like this: { ‘key1’ => ‘value1’, ‘key2’ => ‘value2’, ‘key3’ => ‘value3’ } then display it like this:

{      'key1' => 'value1',
  'aLongkey2' => 'value2',
       'key3' => 'value3'
 }

the formatter tries to align the ‘=>’ and everything around it, unless the FALSE object is passed as the “right_align” value.

Recursively format nested object values. The ‘and ‘ are defaults.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/show_data.rb', line 97

def format_hash( hash_keys, hash_values, indent, open_char = '{', close_char = '}', right_align = nil)
  s = open_char + ' '
  maxkeylen = 0
  if right_align or right_align.nil?		# align maybe?
    #complex = 0
    hash_keys.each_index do |x| 
      k, v = hash_keys[x], hash_values[x]
      kl = k.class == Symbol ? 1 + k.to_s.length : 2 + k.length
      maxkeylen = kl if maxkeylen < kl 
      #complex += v.size if v.class == Hash || v.class == Array || v.class == OpenStruct
    end
    # if the data complexity is < 60%, and the maxkeylen is defined, then allow right-align
    #maxkeylen = 0 if hash_keys.size > 0 && (complex.to_f / hash_keys.size.to_f) < 0.6
  end
  keyfmt = maxkeylen > 0 ?  "%#{maxkeylen}s => " : "%s => "
  indentstr = ' ' * indent + '  '
  count = 0
  hash_keys.each_index do |x|
    key, val = hash_keys[x], hash_values[x]
    if key.class == Symbol			# Symbols have different representation
      formatted_key = sprintf(keyfmt, ':' + key.to_s)	# :somesymbol
    else
      formatted_key = sprintf(keyfmt, "'" + key + "'")	# 'somekey'
    end
    s += formatted_key
    vs = format_data(val, indent + formatted_key.size + 2)
    s += vs
    count += 1
    s += ",\n" + indentstr unless count >= hash_keys.size
  end
  s += "\n" + ' ' * indent + close_char
end

#show_data(data, indent = 0) ⇒ Object



130
131
132
# File 'lib/show_data.rb', line 130

def show_data(data, indent=0)
  puts format_data(data, indent)
end