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
|
# File 'lib/raicoto/array/inspection.rb', line 4
def ls(*attrs)
ActiveRecord.without_logging do
attrs.map!(&:to_s)
attrs.unshift('id')
attrs << 'name' if self._attribute_names.include?('name')
attrs << 'title' if self._attribute_names.include?('title')
attrs.uniq!
lengths = {}
records = self._all_for_ls
if records.count < 1
puts "No Records."
return
end
records.each do |r|
attrs.each do |a|
val = r
a.split('.').map{|path| val = val.send(path)}
len = val.to_s.length
lengths[a] ||= a.length
lengths[a] = [lengths[a], len].max
end
end
out = [attrs.map{|a| a.rjust(lengths[a]+1)}.join]
out += records.map { |r|
line = ""
attrs.each do |a|
val = r
a.split('.').map{|path| val = val.send(path)}
line << val.to_s.rjust(lengths[a]+1)
end
line
}
out.each{|s| puts s }
out.length - 1
end
end
|