Class: CSVDoc

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCSVDoc

Returns a new instance of CSVDoc.



14
15
16
17
18
19
# File 'lib/csvgen.rb', line 14

def initialize()
  @rows = []
  self.newline = "\n"
  self.sep = ","
  self.quote = '"'
end

Instance Attribute Details

#newlineObject

Returns the value of attribute newline.



10
11
12
# File 'lib/csvgen.rb', line 10

def newline
  @newline
end

#quoteObject

Returns the value of attribute quote.



12
13
14
# File 'lib/csvgen.rb', line 12

def quote
  @quote
end

#sepObject

Returns the value of attribute sep.



11
12
13
# File 'lib/csvgen.rb', line 11

def sep
  @sep
end

Instance Method Details

#enquote(val) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/csvgen.rb', line 42

def enquote(val)
  val_s = val.to_s
  if needs_quote(val_s) then
    quote + val_s.gsub(quote, quote * 2) + quote
  else
    val_s
  end
end

#needs_quote(val) ⇒ Object



38
39
40
# File 'lib/csvgen.rb', line 38

def needs_quote(val)
  val.include?(quote) || val.include?(sep)
end

#row(*args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/csvgen.rb', line 21

def row(*args)
  length = args[0] || 0
  r = CSVRow.new(self)
  r.length = length
  begin
    yield(r)
    @rows.push(r)
  rescue
    @rows.push("Error in row: #{$!.inspect}")
  end
  self
end

#to_sObject



34
35
36
# File 'lib/csvgen.rb', line 34

def to_s
  @rows.collect { |r| r.to_s }.join(@newline)
end