Method: Censive#initialize

Defined in:
lib/censive.rb

#initialize(str = nil, drop: false, encoding: nil, excel: false, mode: :compact, out: nil, quote: '"', relax: false, rowsep: "\n", sep: ",", strip: false, **opts) {|_self| ... } ⇒ Censive

Returns a new instance of Censive.

Yields:

  • (_self)

Yield Parameters:

  • _self (Censive)

    the object that the method was called on



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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/censive.rb', line 55

def initialize(str=nil,
  drop:     false   , # drop trailing empty columns?
  encoding: nil     , # character encoding
  excel:    false   , # literals ="01" formulas =A1 + B2 http://bit.ly/3Y7jIvc
  mode:     :compact, # output mode: compact or full
  out:      nil     , # output stream, needs to respond to <<
  quote:    '"'     , # quote character
  relax:    false   , # relax quote parsing so ,"Fo"o, => ,"Fo""o",
  rowsep:   "\n"    , # row separator for output
  sep:      ","     , # column separator character
  strip:    false   , # strip columns when reading
  **opts              # grab bag
)
  # initialize data source
  if str && str.size < 100 && File.readable?(str)
    str = File.open(str, encoding ? "r:#{encoding}" : "r").read
  else
    str ||= ""
    str = str.encode(encoding) if encoding
  end
  super(str)
  reset

  # config options
  @cheat    = true
  @drop     = drop
  @encoding = str.encoding
  @excel    = excel
  @mode     = mode
  @out      = out || $stdout
  @relax    = relax
  @strip    = strip

  # config strings
  @quote    = quote
  @rowsep   = rowsep
  @sep      = sep

  # static strings
  @cr       = "\r"
  @lf       = "\n"
  @es       = ""
  @eq       = "="

  # combinations
  @esc      = (@quote * 2)
  @seq      = [@sep, @eq].join # used for parsing in excel mode

  # regexes
  xsep      = Regexp.escape(@sep) # may need to be escaped
  @eoc      = /(?=#{"\\" + xsep}|#{@cr}|#{@lf}|\z)/ # end of cell
  @eol      = /#{@cr}#{@lf}?|#{@lf}/                # end of line
  @escapes  = /(#{"\\" + @quote})|#{xsep}|#{@cr}|#{@lf}/
  @quotable = /#{xsep}|#{@cr}|#{@lf}/
  @quotes   = /#{"\\" + @quote}/
  @seps     = /#{"\\" + xsep}+/
  @quoted   = @excel ? /(?:=)?#{"\\" + @quote}/ : @quote
  @unquoted = /[^#{"\\" + xsep}#{@cr}#{@lf}][^#{"\\" + @quote}#{@cr}#{@lf}]*/
  @leadzero = /\A0\d*\z/

  yield self if block_given?
end