Module: Raicoto::Inspection

Included in:
ActiveRecord::Base, Array
Defined in:
lib/raicoto/inspection.rb

Instance Method Summary collapse

Instance Method Details

#ls(*attrs) ⇒ Object



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
# File 'lib/raicoto/inspection.rb', line 3

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
    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