Class: Csv

Inherits:
Object
  • Object
show all
Defined in:
lib/documenter/csv.rb

Constant Summary collapse

@@defaults =

defaults params

{:bracket => '"', :line_break => "\n", :cell_split => ',', :file => 'out.csv'}

Class Method Summary collapse

Class Method Details

.array_to_str(array, params) ⇒ Object

converts one demention array to single csv string



26
27
28
29
# File 'lib/documenter/csv.rb', line 26

def self.array_to_str array, params
  params = @@defaults.merge params
  array.map{|x| params[:bracket]+x.to_s.gsub(/#{params[:bracket]}/,params[:bracket]+params[:bracket])+params[:bracket]}*params[:cell_split]
end

.load_table(file, params) ⇒ Object

load data from file into table



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/documenter/csv.rb', line 32

def self.load_table file, params
  params = @@defaults.merge params
  bracket = params[:bracket]
  line_break = params[:line_break]
  cell_split = params[:cell_split]
  
  rows = []
  cells = []
  buffer = ''
  #bracket mode show if we between brackets. If so, we should not react on several things
  bracket_mode = false
  prev_bracket = false
  
  # we will look at one char at time
  file.each_byte do |byte|
    char =  byte.chr
    #if you see a bracket char
    if char == bracket
      #and previus was bracket to
      if prev_bracket==true
        #we just save one bracket
        buffer += char
        prev_bracket = false
      else
        prev_bracket = true
      end
      #after the bracket we change mode
      bracket_mode = !bracket_mode
    else
      prev_bracket = false
      if  !bracket_mode
        if char == cell_split || char == line_break
          #we met splitter
          #in sny way we finishing cell
          cells << buffer
          buffer = ''
          #but if it was a line break
          if char == line_break
            #we finish a line to 
            rows << cells
            cells = []
          end        
        else
          buffer += char        
        end
      else
        #in brackets mode we just copy data
        buffer += char        
      end
    end
  end
  #wen all data finished, we just save what we got in buffers
  cells << buffer if buffer.size>0
  rows << cells if cells.size>0
  rows
end

.save(params = {}) ⇒ Object

save given params two demetions array to file



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/documenter/csv.rb', line 7

def self.save params={}
  params = @@defaults.merge(params)
  File.open(params[:file],'w') do |f|
    f.write(params[:title]+params[:line_break]) if params[:title] 
    f.write array_to_str(params[:header],params)+params[:line_break] if params[:header] 
    params[:data].each do |row|
      #you can edit row in given block if you need so
      if block_given?
        ar = yield row
      else
        ar = row
      end
      f.write array_to_str(ar,params)+params[:line_break]
    end
  end
end