Class: RGhost::Grid::CSV

Inherits:
Base show all
Defined in:
lib/rghost/grid/csv_grid.rb

Overview

Example

grid=Grid::CSV.new :width => 2
grid.column :title => "User", :align => :right
grid.column :title => "Password", :format => lambda{|v| (v.to_s == "x") ? "Yes" : "No"}
grid.column :title => "UID", :width => 1
grid.column :title => "GID"
grid.column :title => "Gecos", :width => 2.5
grid.column :title => "Home Dir", :width => 4
grid.column :title => "Shell"
grid.style  :bottom_lines
grid.data("/etc/passwd",/:/)

Constant Summary

Constants included from RubyToPs

RubyToPs::UTF8_ENCODINGS

Instance Attribute Summary

Attributes inherited from Base

#column_padding, #header

Instance Method Summary collapse

Methods inherited from Base

#col, #column, #format_field, #initialize, #proc_line, #ps, #style, #width

Methods included from CallbackFacade

#after_column, #before_column, #before_row, #even_column, #even_row, #odd_column, #odd_row

Methods included from RubyToPs

#array_to_stack, #hash_to_array, #pack_string, #ps_escape, #string_eval, #to_array, #to_bool, #to_string, #to_string_array

Methods inherited from PsObject

#<<, #call, #graphic_scope, #initialize, #ps, #raw, #set, #to_s

Constructor Details

This class inherits a constructor from RGhost::Grid::Base

Instance Method Details

#data(filepath, col_sep = ';') ⇒ Object

Pass in path to file and the record separator:

grid.data("/mydir/myfile.csv", ';')

You can use a regular expression for record separator:

grid.data("/mydir/myfile.csv", /[^\w]/)

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rghost/grid/csv_grid.rb', line 25

def data(filepath,col_sep=';')
  _data=filepath
  first=true
  _data=File.open(_data) if _data.is_a? String
  _data.each do |line|
    l=line.split(Regexp.new(col_sep)) if col_sep.is_a?(String)
    l=line.split(col_sep) if col_sep.is_a?(Regexp)
    if first
      v=l.size-@header.titles.size
      #v.times{@header.titles << "tt"}
      #l.each {|h| col(h)} if @header.titles.size == 0
      l.each {|h| col("")} if @header.titles.size == 0
      proc_line(l)
      first=false

      next
    else

      proc_line(l)
    end
  end

end