Class: Flucti::Utilities::ListDisplayer

Inherits:
Object
  • Object
show all
Defined in:
lib/flucti/utilities/list_displayer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ ListDisplayer

Returns a new instance of ListDisplayer.



6
7
8
9
# File 'lib/flucti/utilities/list_displayer.rb', line 6

def initialize(options={}, &block)
  @options = options
  @columns = build_column_list(&block)
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



4
5
6
# File 'lib/flucti/utilities/list_displayer.rb', line 4

def columns
  @columns
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/flucti/utilities/list_displayer.rb', line 4

def options
  @options
end

Instance Method Details

#display(entries) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/flucti/utilities/list_displayer.rb', line 11

def display(entries)
  if options[:table] || entries.size > 3
    puts entries.inject(Utilities::Table.new(*columns.map { |title,| title })) { |table, entry|
      table << columns.map { |title, proc| proc.call(entry) }
    }
  else
    entries.each do |entry|
      if attribute = options[:title]
        puts_subtitle(entry.send(attribute))
      else
        title, proc = columns.first
        val = proc.call(entry)
        puts_subtitle(val.to_s =~ /^\d+$/ ? "#{title}=#{val}" : val)
      end
      display_single_entry(entry)
    end
  end
  puts "\nTotal: #{entries.size}"
end

#display_single_entry(entry) ⇒ Object



31
32
33
34
35
# File 'lib/flucti/utilities/list_displayer.rb', line 31

def display_single_entry(entry)
  columns.each do |title, proc|
    puts "%-*s %s" % [column_width+1, "#{title}:", proc.call(entry)]
  end
end