2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/table_display.rb', line 2
def to_table_display(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
= args.collect { |arg| arg.respond_to?(:call) ? arg : arg.to_s }
+= Array(options.delete(:methods)) if options[:methods]
only_attributes = Array(options.delete(:only)) if options[:only]
only_attributes ||= [] if args.length > 0
except_attributes = Array(options.delete(:except)) if options[:except]
except_attributes = (except_attributes + except_attributes.collect(&:to_s)).uniq if except_attributes.present? display_inspect = options.nil? || !options.has_key?(:inspect) || options.delete(:inspect)
raise "unknown options passed to to_table_display: #{options.keys.to_sentence}" unless options.blank?
column_lengths = ActiveSupport::OrderedHash.new
if only_attributes
only_attributes.each {|attribute| column_lengths[attribute] = 0}
else
each do |record|
next if record.nil?
if record.respond_to?(:attribute_names)
attribute_names = record.attribute_names
elsif Object.const_defined?(:OpenStruct) && record.is_a?(OpenStruct)
record = record.instance_variable_get("@table")
attribute_names = record.keys
elsif record.respond_to?(:attributes)
attribute_names = record.attributes.keys
else
attribute_names = record.keys
end
if attribute_names.any? {|name| column_lengths[name].nil?} columns_to_check = record.is_a?(ActiveRecord::Base) ? ((record.class.columns.collect(&:name) & attribute_names) + attribute_names).uniq : attribute_names
columns_to_check.each do |name|
next if (only_attributes && !only_attributes.include?(name)) || (except_attributes && except_attributes.include?(name))
column_lengths[name] = 0 end
end
end
end
.each {|name| column_lengths[name] = 0}
data = collect do |record|
column_lengths.collect do |attribute, max_width|
value = if attribute.respond_to?(:call)
attribute.call(record)
elsif record.is_a?(Hash)
record[attribute]
else
record.send(attribute)
end
string_value = display_inspect ? value.inspect : (value.is_a?(String) ? value : value.to_s)
column_lengths[attribute] = string_value.mb_chars.length if string_value.mb_chars.length > max_width
value.is_a?(Numeric) ? value : string_value end
end
return [] if data.empty?
separator_string = "+"
heading_string = "|"
column_lengths.each do |attribute, max_width|
next unless max_width > 0 name = (attribute.respond_to?(:name) ? attribute.name : attribute).to_s
if name.mb_chars.length > max_width
column_lengths[attribute] = max_width = name.mb_chars.length
end
separator_string << '-'*(max_width + 2) << '+'
heading_string << ' ' << name.ljust(max_width) << ' |'
end
rows = [separator_string, heading_string, separator_string]
data.each do |data_row|
data_string = "|"
column_lengths.each_with_index do |(_attribute, max_width), index|
next unless max_width > 0 value = data_row[index]
if value.is_a?(Numeric)
data_string << ' ' << (display_inspect ? value.inspect : value.to_s).mb_chars.rjust(max_width) << ' |'
else
data_string << ' ' << value.mb_chars.ljust(max_width) << ' |'
end
end
rows << data_string
end
rows << separator_string
end
|