Class: HP::Cloud::Tableizer

Inherits:
Object
  • Object
show all
Defined in:
lib/hpcloud/tableizer.rb

Constant Summary collapse

DEFAULT_REPORT_PAGE_LENGTH =
60

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, keys, values = []) ⇒ Tableizer

Returns a new instance of Tableizer.



30
31
32
33
34
35
36
37
# File 'lib/hpcloud/tableizer.rb', line 30

def initialize(options, keys, values = [])
  @options = options
  columns = Columns.new(options, keys)
  @keys = columns.keys
  @values = values
  @found = false
  @page_length = Config.new.get_i(:report_page_length, DEFAULT_REPORT_PAGE_LENGTH)
end

Instance Attribute Details

#foundObject (readonly)

Returns the value of attribute found.



26
27
28
# File 'lib/hpcloud/tableizer.rb', line 26

def found
  @found
end

Class Method Details

.option_argsObject



71
72
73
74
# File 'lib/hpcloud/tableizer.rb', line 71

def self.option_args
  return { :type => :string, :aliases => '-d',
     :desc => 'Use the specified value as the report separator.'}
end

.option_nameObject



67
68
69
# File 'lib/hpcloud/tableizer.rb', line 67

def self.option_name
  return :separator
end

Instance Method Details

#add(item) ⇒ Object



39
40
41
42
43
# File 'lib/hpcloud/tableizer.rb', line 39

def add(item)
  @found = true
  @values << item
  print if @values.length >= @page_length
end


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/hpcloud/tableizer.rb', line 45

def print
  return if @values.nil?
  return if @values.empty?
  @found = true
  if @options[:separator]
    separator = @options[:separator]
    separator = ',' if separator == "separator"
    @values.each{ |v|
      sep = ''
      line = ''
      @keys.each{ |k|
        line += "#{sep}#{v[k]}"
        sep = separator
      }
      puts line
    }
  else
    Formatador.display_compact_table(@values, @keys)
  end
  @values = []
end