Class: Array

Inherits:
Object
  • Object
show all
Includes:
Raicoto::Inspection
Defined in:
lib/raicoto/array/inspection.rb

Instance Method Summary collapse

Instance Method Details

#_all_for_lsObject



41
42
43
# File 'lib/raicoto/array/inspection.rb', line 41

def _all_for_ls
  self
end

#_attribute_namesObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/raicoto/array/inspection.rb', line 45

def _attribute_names
  return [] if empty?
  case true
  when first.respond_to?(:attribute_names)
    first.attribute_names
  when first.respond_to?(:attributes)
    first.attributes.keys
  when first.respond_to?(:keys)
    first.keys
  else
    raise "don't know how to get attribute_names for #{first.inspect}"
  end
  
end

#ls(*attrs) ⇒ Object



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