Class: Origen::PowerDomains::PowerDomainsCollection

Inherits:
Hash
  • Object
show all
Defined in:
lib/origen/power_domains/power_domains_collection.rb

Instance Method Summary collapse

Methods inherited from Hash

#filter, #ids, #intersect?, #intersections, #longest_key, #longest_value, #recursive_find_by_key, #update_common

Instance Method Details

#inspect(options = {}) ⇒ 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
40
41
42
43
44
45
46
47
48
49
# File 'lib/origen/power_domains/power_domains_collection.rb', line 4

def inspect(options = {})
  options = {
    fancy_output: true
  }.update(options)
  headers = []
  output_power_domain_list = []
  column_widths = {}.tap do |colhash|
    each do |domain_name, domain|
      output_attr_list = {}
      domain.instance_variables.each do |attr|
        attr_getter = attr.to_s[/\@(\S+)/, 1].to_sym
        attr_val = domain.send attr_getter
        next unless [String, Numeric, Float, Integer, Symbol, Range].include? attr_val.class

        headers << attr_getter unless headers.include?(attr_getter)
        str = case attr_val
        when Range
          start_voltage = attr_val.first
          end_voltage = attr_val.last
          "#{start_voltage}\.\.#{end_voltage}"
        else
          attr_val.to_s
              end
        curr_longest = [attr_getter, str].max_by(&:length).size + 2 # Add 2 for the whitespace
        if colhash[attr].nil? || (colhash[attr] < curr_longest)
          colhash[attr] = curr_longest
        end
        output_attr_list[attr_getter] = str
      end
      output_power_domain_list << output_attr_list
    end
  end
  if options[:fancy_output]
    puts '' + column_widths.values.each.map { |i| '' * i }.join('') + ''
    puts '' + headers.each_with_index.map { |col_val, i| " #{col_val} ".ljust(column_widths.values[i]) }.join('') + ''
    puts '' + column_widths.values.each.map { |i| '' * i }.join('') + ''
    puts output_power_domain_list.each.map { |attributes| '' + headers.each_with_index.map { |value, attr_idx| " #{attributes[value]} ".ljust(column_widths.values[attr_idx]) }.join('') + '' }
    puts '' + column_widths.values.each.map { |i| '' * i }.join('') + ''
  else
    puts '.' + column_widths.values.each.map { |i| '-' * i }.join('-') + '.'
    puts '|' + headers.each_with_index.map { |col_val, i| " #{col_val} ".ljust(column_widths.values[i]) }.join('|') + '|'
    puts '|' + column_widths.values.each.map { |i| '-' * i }.join('+') + '|'
    puts output_power_domain_list.each.map { |attributes| '|' + headers.each_with_index.map { |value, attr_idx| " #{attributes[value]} ".ljust(column_widths.values[attr_idx]) }.join('|') + '|' }
    puts '`' + column_widths.values.each.map { |i| '-' * i }.join('-') + '\''
  end
end