doc/TaskJuggler/CSVFile.html in taskjuggler-0.0.5 vs doc/TaskJuggler/CSVFile.html in taskjuggler-0.0.6

- old
+ new

@@ -67,24 +67,26 @@ <div id="method-list-section" class="section"> <h3 class="section-header">Methods</h3> <ul class="link-list"> - <li><a href="#M000873">::new</a></li> + <li><a href="#method-c-new">::new</a></li> - <li><a href="#M000879">#closeField</a></li> + <li><a href="#method-c-strToNative">::strToNative</a></li> - <li><a href="#M000877">#marshal</a></li> + <li><a href="#method-i-marshal">#marshal</a></li> - <li><a href="#M000876">#parse</a></li> + <li><a href="#method-i-parse">#parse</a></li> - <li><a href="#M000878">#parseLine</a></li> + <li><a href="#method-i-read">#read</a></li> - <li><a href="#M000875">#read</a></li> + <li><a href="#method-i-to_s">#to_s</a></li> - <li><a href="#M000874">#write</a></li> + <li><a href="#method-i-unMarshal">#unMarshal</a></li> + <li><a href="#method-i-write">#write</a></li> + </ul> </div> <!-- Included Modules --> @@ -96,10 +98,12 @@ <div id="fileindex-section" class="section project-section"> <h3 class="section-header">Files</h3> <ul> + <li class="file"><a href="../CHANGELOG.html">CHANGELOG</a></li> + <li class="file"><a href="../COPYING.html">COPYING</a></li> <li class="file"><a href="../README.html">README</a></li> </ul> @@ -265,10 +269,12 @@ <li><a href="../TaskJuggler/NikuReport.html">TaskJuggler::NikuReport</a></li> <li><a href="../TaskJuggler/NikuResource.html">TaskJuggler::NikuResource</a></li> + <li><a href="../TaskJuggler/NodeListAttribute.html">TaskJuggler::NodeListAttribute</a></li> + <li><a href="../TaskJuggler/OnShiftCache.html">TaskJuggler::OnShiftCache</a></li> <li><a href="../TaskJuggler/ProcessIntercom.html">TaskJuggler::ProcessIntercom</a></li> <li><a href="../TaskJuggler/ProcessIntercomIface.html">TaskJuggler::ProcessIntercomIface</a></li> @@ -279,10 +285,12 @@ <li><a href="../TaskJuggler/ProjectBrokerIface.html">TaskJuggler::ProjectBrokerIface</a></li> <li><a href="../TaskJuggler/ProjectFileParser.html">TaskJuggler::ProjectFileParser</a></li> + <li><a href="../TaskJuggler/ProjectFileScanner.html">TaskJuggler::ProjectFileScanner</a></li> + <li><a href="../TaskJuggler/ProjectRecord.html">TaskJuggler::ProjectRecord</a></li> <li><a href="../TaskJuggler/ProjectServer.html">TaskJuggler::ProjectServer</a></li> <li><a href="../TaskJuggler/ProjectServerIface.html">TaskJuggler::ProjectServerIface</a></li> @@ -295,16 +303,20 @@ <li><a href="../TaskJuggler/PropertyTreeNode.html">TaskJuggler::PropertyTreeNode</a></li> <li><a href="../TaskJuggler/Query.html">TaskJuggler::Query</a></li> + <li><a href="../TaskJuggler/RTFHandlers.html">TaskJuggler::RTFHandlers</a></li> + <li><a href="../TaskJuggler/RTFNavigator.html">TaskJuggler::RTFNavigator</a></li> <li><a href="../TaskJuggler/RTFQuery.html">TaskJuggler::RTFQuery</a></li> <li><a href="../TaskJuggler/RTFReport.html">TaskJuggler::RTFReport</a></li> + <li><a href="../TaskJuggler/RTFReportLink.html">TaskJuggler::RTFReportLink</a></li> + <li><a href="../TaskJuggler/RealFormat.html">TaskJuggler::RealFormat</a></li> <li><a href="../TaskJuggler/RealFormatAttribute.html">TaskJuggler::RealFormatAttribute</a></li> <li><a href="../TaskJuggler/ReferenceAttribute.html">TaskJuggler::ReferenceAttribute</a></li> @@ -319,10 +331,12 @@ <li><a href="../TaskJuggler/ReportServerIface.html">TaskJuggler::ReportServerIface</a></li> <li><a href="../TaskJuggler/ReportServerRecord.html">TaskJuggler::ReportServerRecord</a></li> + <li><a href="../TaskJuggler/ReportServlet.html">TaskJuggler::ReportServlet</a></li> + <li><a href="../TaskJuggler/ReportTable.html">TaskJuggler::ReportTable</a></li> <li><a href="../TaskJuggler/ReportTableCell.html">TaskJuggler::ReportTableCell</a></li> <li><a href="../TaskJuggler/ReportTableColumn.html">TaskJuggler::ReportTableColumn</a></li> @@ -447,10 +461,12 @@ <li><a href="../TaskJuggler/TextScanner/BufferStreamHandle.html">TaskJuggler::TextScanner::BufferStreamHandle</a></li> <li><a href="../TaskJuggler/TextScanner/FileStreamHandle.html">TaskJuggler::TextScanner::FileStreamHandle</a></li> + <li><a href="../TaskJuggler/TextScanner/MacroStackEntry.html">TaskJuggler::TextScanner::MacroStackEntry</a></li> + <li><a href="../TaskJuggler/TextScanner/StreamHandle.html">TaskJuggler::TextScanner::StreamHandle</a></li> <li><a href="../TaskJuggler/TimeSheet.html">TaskJuggler::TimeSheet</a></li> <li><a href="../TaskJuggler/TimeSheetReceiver.html">TaskJuggler::TimeSheetReceiver</a></li> @@ -491,12 +507,16 @@ <li><a href="../TaskJuggler/TjpExportRE.html">TaskJuggler::TjpExportRE</a></li> <li><a href="../TaskJuggler/TjpSyntaxRules.html">TaskJuggler::TjpSyntaxRules</a></li> + <li><a href="../TaskJuggler/URLParameter.html">TaskJuggler::URLParameter</a></li> + <li><a href="../TaskJuggler/UserManual.html">TaskJuggler::UserManual</a></li> + <li><a href="../TaskJuggler/WebServer.html">TaskJuggler::WebServer</a></li> + <li><a href="../TaskJuggler/WorkingHours.html">TaskJuggler::WorkingHours</a></li> <li><a href="../TaskJuggler/WorkingHoursAttribute.html">TaskJuggler::WorkingHoursAttribute</a></li> <li><a href="../TaskJuggler/XMLBlob.html">TaskJuggler::XMLBlob</a></li> @@ -519,10 +539,12 @@ <li><a href="../RuntimeConfig.html">RuntimeConfig</a></li> <li><a href="../String.html">String</a></li> + <li><a href="../StringIO.html">StringIO</a></li> + </ul> <div id="no-class-search-results" style="display: none;">No matching classes.</div> </div> @@ -544,24 +566,45 @@ <!-- Constants --> <!-- Attributes --> + <div id="attribute-method-details" class="method-section section"> + <h3 class="section-header">Attributes</h3> + + <div id="data-attribute-method" class="method-detail"> + <a name="data"></a> + + <div class="method-heading attribute-method-heading"> + <span class="method-name">data</span><span + class="attribute-access-type">[R]</span> + </div> + + <div class="method-description"> + + + + </div> + </div> + + </div> + + <!-- Methods --> <div id="public-class-method-details" class="method-section section"> <h3 class="section-header">Public Class Methods</h3> <div id="new-method" class="method-detail "> - <a name="M000873"></a> + <a name="method-c-new"></a> <div class="method-heading"> <span class="method-name">new</span><span - class="method-args">(data, separator = ';', quote = '"')</span> + class="method-args">(data = nil, separator = ';', quote = '"')</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> @@ -575,34 +618,87 @@ <div class="method-source-code" id="new-source"> <pre> - <span class="ruby-comment cmt"># File lib/reports/CSVFile.rb, line 25</span> -25: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">data</span>, <span class="ruby-identifier">separator</span> = <span class="ruby-value str">';'</span>, <span class="ruby-identifier">quote</span> = <span class="ruby-value str">'&quot;'</span>) -26: <span class="ruby-ivar">@data</span> = <span class="ruby-identifier">data</span> -27: <span class="ruby-ivar">@separator</span> = <span class="ruby-identifier">separator</span> -28: <span class="ruby-ivar">@quote</span> = <span class="ruby-identifier">quote</span> -29: <span class="ruby-keyword kw">end</span></pre> + <span class="ruby-comment cmt"># File lib/reports/CSVFile.rb, line 27</span> +27: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">data</span> = <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">separator</span> = <span class="ruby-value str">';'</span>, <span class="ruby-identifier">quote</span> = <span class="ruby-value str">'&quot;'</span>) +28: <span class="ruby-ivar">@data</span> = <span class="ruby-identifier">data</span> +29: <span class="ruby-identifier">raise</span> <span class="ruby-node">&quot;Illegal separator: #{separator}&quot;</span> <span class="ruby-keyword kw">if</span> <span class="ruby-value str">'.&quot;'</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">separator</span>) +30: <span class="ruby-ivar">@separator</span> = <span class="ruby-identifier">separator</span> +31: <span class="ruby-identifier">raise</span> <span class="ruby-node">&quot;Illegal quote: #{separator}&quot;</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">quote</span> <span class="ruby-operator">==</span> <span class="ruby-value str">'.'</span> +32: <span class="ruby-ivar">@quote</span> = <span class="ruby-identifier">quote</span> +33: <span class="ruby-keyword kw">end</span></pre> </div> </div> </div> + <div id="str-o-ative-method" class="method-detail "> + <a name="method-c-strToNative"></a> + + <div class="method-heading"> + + <span class="method-name">strToNative</span><span + class="method-args">(str)</span> + <span class="method-click-advice">click to toggle source</span> + + </div> + + <div class="method-description"> + + <p> +Utility function that tries to convert a <a +href="../String.html">String</a> into a native type that is supported by +the <a href="CSVFile.html">CSVFile</a> generator. If no native type is +found, the input <a href="../String.html">String</a> <em>str</em> will be +returned unmodified. nil is returned as nil. +</p> + + + + <div class="method-source-code" + id="str-o-ative-source"> +<pre> + <span class="ruby-comment cmt"># File lib/reports/CSVFile.rb, line 182</span> +182: <span class="ruby-keyword kw">def</span> <span class="ruby-constant">CSVFile</span>.<span class="ruby-identifier">strToNative</span>(<span class="ruby-identifier">str</span>) +183: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">str</span>.<span class="ruby-identifier">nil?</span> +184: <span class="ruby-keyword kw">nil</span> +185: <span class="ruby-keyword kw">elsif</span> <span class="ruby-regexp re">/^[-+]?\d+$/</span> <span class="ruby-operator">=~</span> <span class="ruby-identifier">str</span> +186: <span class="ruby-comment cmt"># field is a Fixnum</span> +187: <span class="ruby-identifier">str</span>.<span class="ruby-identifier">to_i</span> +188: <span class="ruby-keyword kw">elsif</span> <span class="ruby-regexp re">/^[-+]?\d*\.?\d+([eE][-+]?\d+)?$/</span> <span class="ruby-operator">=~</span> <span class="ruby-identifier">str</span> +189: <span class="ruby-comment cmt"># field is a Float</span> +190: <span class="ruby-identifier">str</span>.<span class="ruby-identifier">to_f</span> +191: <span class="ruby-keyword kw">else</span> +192: <span class="ruby-comment cmt"># Everything else is kept as String</span> +193: <span class="ruby-identifier">str</span> +194: <span class="ruby-keyword kw">end</span> +195: <span class="ruby-keyword kw">end</span></pre> + </div> + + </div> + + + + + </div> + + </div> <div id="public-instance-method-details" class="method-section section"> <h3 class="section-header">Public Instance Methods</h3> <div id="parse-method" class="method-detail "> - <a name="M000876"></a> + <a name="method-i-parse"></a> <div class="method-heading"> <span class="method-name">parse</span><span class="method-args">(str)</span> @@ -620,17 +716,105 @@ <div class="method-source-code" id="parse-source"> <pre> - <span class="ruby-comment cmt"># File lib/reports/CSVFile.rb, line 76</span> -76: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>) -77: <span class="ruby-identifier">csv</span> = [] -78: <span class="ruby-identifier">str</span>.<span class="ruby-identifier">each_line</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">line</span><span class="ruby-operator">|</span> -79: <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">parseLine</span>(<span class="ruby-identifier">line</span>) -80: <span class="ruby-keyword kw">end</span> -81: <span class="ruby-keyword kw">end</span></pre> + <span class="ruby-comment cmt"># File lib/reports/CSVFile.rb, line 84</span> + 84: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>) + 85: <span class="ruby-ivar">@data</span> = [] + 86: <span class="ruby-identifier">state</span> = <span class="ruby-value">:startOfRecord</span> + 87: <span class="ruby-identifier">fields</span> = <span class="ruby-identifier">field</span> = <span class="ruby-identifier">quoted</span> = <span class="ruby-keyword kw">nil</span> + 88: + 89: <span class="ruby-comment cmt"># Make sure the input is terminated with a record end.</span> + 90: <span class="ruby-identifier">str</span> <span class="ruby-operator">+=</span> <span class="ruby-value str">&quot;\n&quot;</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">str</span>[<span class="ruby-value">1</span>] <span class="ruby-operator">==</span> <span class="ruby-value">\n\</span> + 91: + 92: <span class="ruby-identifier">str</span>.<span class="ruby-identifier">each_utf8_char</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">c</span><span class="ruby-operator">|</span> + 93: <span class="ruby-comment cmt">#puts &quot;c: #{c} State: #{state}&quot;</span> + 94: <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">state</span> + 95: <span class="ruby-keyword kw">when</span> <span class="ruby-value">:startOfRecord</span> + 96: <span class="ruby-comment cmt"># This will store the fields of a record</span> + 97: <span class="ruby-identifier">fields</span> = [] + 98: <span class="ruby-identifier">state</span> = <span class="ruby-value">:startOfField</span> + 99: <span class="ruby-keyword kw">redo</span> +100: <span class="ruby-keyword kw">when</span> <span class="ruby-value">:startOfField</span> +101: <span class="ruby-identifier">field</span> = <span class="ruby-value str">''</span> +102: <span class="ruby-identifier">quoted</span> = <span class="ruby-keyword kw">false</span> +103: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">c</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@quote</span> +104: <span class="ruby-comment cmt"># We've found the start of a quoted field.</span> +105: <span class="ruby-identifier">state</span> = <span class="ruby-value">:inQuotedField</span> +106: <span class="ruby-identifier">quoted</span> = <span class="ruby-keyword kw">true</span> +107: <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">c</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@separator</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">c</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;\n&quot;</span> +108: <span class="ruby-comment cmt"># We've found an empty field</span> +109: <span class="ruby-identifier">field</span> = <span class="ruby-keyword kw">nil</span> +110: <span class="ruby-identifier">state</span> = <span class="ruby-value">:fieldEnd</span> +111: <span class="ruby-keyword kw">redo</span> +112: <span class="ruby-keyword kw">else</span> +113: <span class="ruby-comment cmt"># We've found the first character of an unquoted field</span> +114: <span class="ruby-identifier">field</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">c</span> +115: <span class="ruby-identifier">state</span> = <span class="ruby-value">:inUnquotedField</span> +116: <span class="ruby-keyword kw">end</span> +117: <span class="ruby-keyword kw">when</span> <span class="ruby-value">:inQuotedField</span> +118: <span class="ruby-comment cmt"># We are processing the content of a quoted field</span> +119: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">c</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@quote</span> +120: <span class="ruby-comment cmt"># This could be then end of the field or a quoted quote.</span> +121: <span class="ruby-identifier">state</span> = <span class="ruby-value">:quoteInQuotedField</span> +122: <span class="ruby-keyword kw">else</span> +123: <span class="ruby-comment cmt"># We've found a normal character of the quoted field</span> +124: <span class="ruby-identifier">field</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">c</span> +125: <span class="ruby-keyword kw">end</span> +126: <span class="ruby-keyword kw">when</span> <span class="ruby-value">:quoteInQuotedField</span> +127: <span class="ruby-comment cmt"># We are processing a quoted quote or the end of a quoted field</span> +128: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">c</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@quote</span> +129: <span class="ruby-comment cmt"># We've found a quoted quote</span> +130: <span class="ruby-identifier">field</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">c</span> +131: <span class="ruby-identifier">state</span> = <span class="ruby-value">:inQuotedField</span> +132: <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">c</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@separator</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">c</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;\n&quot;</span> +133: <span class="ruby-identifier">state</span> = <span class="ruby-value">:fieldEnd</span> +134: <span class="ruby-keyword kw">redo</span> +135: <span class="ruby-keyword kw">else</span> +136: <span class="ruby-identifier">raise</span> <span class="ruby-node">&quot;Unexpected character #{c} in CSV&quot;</span> +137: <span class="ruby-keyword kw">end</span> +138: <span class="ruby-keyword kw">when</span> <span class="ruby-value">:inUnquotedField</span> +139: <span class="ruby-comment cmt"># We are processing an unquoted field</span> +140: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">c</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@separator</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">c</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;\n&quot;</span> +141: <span class="ruby-comment cmt"># We've found the end of a unquoted field</span> +142: <span class="ruby-identifier">state</span> = <span class="ruby-value">:fieldEnd</span> +143: <span class="ruby-keyword kw">redo</span> +144: <span class="ruby-keyword kw">else</span> +145: <span class="ruby-comment cmt"># A normal character of an unquoted field</span> +146: <span class="ruby-identifier">field</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">c</span> +147: <span class="ruby-keyword kw">end</span> +148: <span class="ruby-keyword kw">when</span> <span class="ruby-value">:fieldEnd</span> +149: <span class="ruby-comment cmt"># We've completed processing a field. Add the field to the list of</span> +150: <span class="ruby-comment cmt"># fields. Convert Fixnums and Floats in native types.</span> +151: <span class="ruby-identifier">fields</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">unMarshal</span>(<span class="ruby-identifier">field</span>, <span class="ruby-identifier">quoted</span>) +152: +153: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">c</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;\n&quot;</span> +154: <span class="ruby-comment cmt"># The field end is an end of a record as well.</span> +155: <span class="ruby-identifier">state</span> = <span class="ruby-value">:recordEnd</span> +156: <span class="ruby-keyword kw">redo</span> +157: <span class="ruby-keyword kw">else</span> +158: <span class="ruby-comment cmt"># Get the next field.</span> +159: <span class="ruby-identifier">state</span> = <span class="ruby-value">:startOfField</span> +160: <span class="ruby-keyword kw">end</span> +161: <span class="ruby-keyword kw">when</span> <span class="ruby-value">:recordEnd</span> +162: <span class="ruby-comment cmt"># We've found the end of a record. Add fields to the @data</span> +163: <span class="ruby-comment cmt"># structure.</span> +164: <span class="ruby-ivar">@data</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">fields</span> +165: <span class="ruby-comment cmt"># Look for a new record.</span> +166: <span class="ruby-identifier">state</span> = <span class="ruby-value">:startOfRecord</span> +167: <span class="ruby-keyword kw">else</span> +168: <span class="ruby-identifier">raise</span> <span class="ruby-node">&quot;Unknown state #{state}&quot;</span> +169: <span class="ruby-keyword kw">end</span> +170: <span class="ruby-keyword kw">end</span> +171: +172: <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">state</span> <span class="ruby-operator">==</span> <span class="ruby-value">:startOfRecord</span> +173: <span class="ruby-identifier">raise</span> <span class="ruby-node">&quot;CSV state machine error in state #{state}&quot;</span> +174: <span class="ruby-keyword kw">end</span> +175: +176: <span class="ruby-ivar">@data</span> +177: <span class="ruby-keyword kw">end</span></pre> </div> </div> @@ -638,11 +822,11 @@ </div> <div id="read-method" class="method-detail "> - <a name="M000875"></a> + <a name="method-i-read"></a> <div class="method-heading"> <span class="method-name">read</span><span class="method-args">(fileName)</span> @@ -661,137 +845,137 @@ <div class="method-source-code" id="read-source"> <pre> - <span class="ruby-comment cmt"># File lib/reports/CSVFile.rb, line 59</span> -59: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">read</span>(<span class="ruby-identifier">fileName</span>) -60: <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">fileName</span> <span class="ruby-operator">==</span> <span class="ruby-value str">'.'</span>) -61: <span class="ruby-identifier">file</span> = <span class="ruby-identifier">$stdin</span> -62: <span class="ruby-keyword kw">else</span> -63: <span class="ruby-identifier">file</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">fileName</span>, <span class="ruby-value str">'r'</span>) -64: <span class="ruby-keyword kw">end</span> -65: -66: <span class="ruby-identifier">csv</span> = [] -67: <span class="ruby-identifier">file</span>.<span class="ruby-identifier">each_line</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">line</span><span class="ruby-operator">|</span> -68: <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">parseLine</span>(<span class="ruby-identifier">line</span>) -69: <span class="ruby-keyword kw">end</span> -70: -71: <span class="ruby-identifier">file</span>.<span class="ruby-identifier">close</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">fileName</span> <span class="ruby-operator">==</span> <span class="ruby-value str">'.'</span> -72: <span class="ruby-identifier">csv</span> -73: <span class="ruby-keyword kw">end</span></pre> + <span class="ruby-comment cmt"># File lib/reports/CSVFile.rb, line 51</span> +51: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">read</span>(<span class="ruby-identifier">fileName</span>) +52: <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">fileName</span> <span class="ruby-operator">==</span> <span class="ruby-value str">'.'</span>) +53: <span class="ruby-identifier">file</span> = <span class="ruby-identifier">$stdin</span> +54: <span class="ruby-keyword kw">else</span> +55: <span class="ruby-identifier">file</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">fileName</span>, <span class="ruby-value str">'r'</span>) +56: <span class="ruby-keyword kw">end</span> +57: +58: <span class="ruby-ivar">@data</span> = <span class="ruby-identifier">parse</span>(<span class="ruby-identifier">file</span>.<span class="ruby-identifier">read</span>) +59: +60: <span class="ruby-identifier">file</span>.<span class="ruby-identifier">close</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">fileName</span> <span class="ruby-operator">==</span> <span class="ruby-value str">'.'</span> +61: <span class="ruby-ivar">@data</span> +62: <span class="ruby-keyword kw">end</span></pre> </div> </div> </div> - <div id="write-method" class="method-detail "> - <a name="M000874"></a> + <div id="to-s-method" class="method-detail "> + <a name="method-i-to_s"></a> <div class="method-heading"> - <span class="method-name">write</span><span - class="method-args">(fileName)</span> + <span class="method-name">to_s</span><span + class="method-args">()</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <p> -Use this function to write the table into a CSV file <tt>fileName</tt>. -&#8217;.&#8217; can be used to write to $stdout. +Convert the CSV data into a CSV formatted <a +href="../String.html">String</a>. </p> <div class="method-source-code" - id="write-source"> + id="to-s-source"> <pre> - <span class="ruby-comment cmt"># File lib/reports/CSVFile.rb, line 33</span> -33: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">write</span>(<span class="ruby-identifier">fileName</span>) -34: <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">fileName</span> <span class="ruby-operator">==</span> <span class="ruby-value str">'.'</span>) -35: <span class="ruby-identifier">file</span> = <span class="ruby-identifier">$stdout</span> -36: <span class="ruby-keyword kw">else</span> -37: <span class="ruby-identifier">file</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">fileName</span>, <span class="ruby-value str">'w'</span>) -38: <span class="ruby-keyword kw">end</span> -39: -40: <span class="ruby-ivar">@data</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">line</span><span class="ruby-operator">|</span> -41: <span class="ruby-identifier">first</span> = <span class="ruby-keyword kw">true</span> -42: <span class="ruby-identifier">line</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">field</span><span class="ruby-operator">|</span> -43: <span class="ruby-comment cmt"># Don't output a separator before the first field of the line.</span> -44: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">first</span> -45: <span class="ruby-identifier">first</span> = <span class="ruby-keyword kw">false</span> -46: <span class="ruby-keyword kw">else</span> -47: <span class="ruby-identifier">file</span>.<span class="ruby-identifier">write</span> <span class="ruby-ivar">@separator</span> -48: <span class="ruby-keyword kw">end</span> -49: <span class="ruby-identifier">file</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">marshal</span>(<span class="ruby-identifier">field</span>)) -50: <span class="ruby-keyword kw">end</span> -51: <span class="ruby-identifier">file</span>.<span class="ruby-identifier">write</span> <span class="ruby-value str">&quot;\n&quot;</span> -52: <span class="ruby-keyword kw">end</span> -53: -54: <span class="ruby-identifier">file</span>.<span class="ruby-identifier">close</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">fileName</span> <span class="ruby-operator">==</span> <span class="ruby-value str">'.'</span> -55: <span class="ruby-keyword kw">end</span></pre> + <span class="ruby-comment cmt"># File lib/reports/CSVFile.rb, line 65</span> +65: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">to_s</span> +66: <span class="ruby-identifier">s</span> = <span class="ruby-value str">''</span> +67: <span class="ruby-ivar">@data</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">line</span><span class="ruby-operator">|</span> +68: <span class="ruby-identifier">first</span> = <span class="ruby-keyword kw">true</span> +69: <span class="ruby-identifier">line</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">field</span><span class="ruby-operator">|</span> +70: <span class="ruby-comment cmt"># Don't output a separator before the first field of the line.</span> +71: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">first</span> +72: <span class="ruby-identifier">first</span> = <span class="ruby-keyword kw">false</span> +73: <span class="ruby-keyword kw">else</span> +74: <span class="ruby-identifier">s</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@separator</span> +75: <span class="ruby-keyword kw">end</span> +76: <span class="ruby-identifier">s</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">marshal</span>(<span class="ruby-identifier">field</span>) +77: <span class="ruby-keyword kw">end</span> +78: <span class="ruby-identifier">s</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-value str">&quot;\n&quot;</span> +79: <span class="ruby-keyword kw">end</span> +80: <span class="ruby-identifier">s</span> +81: <span class="ruby-keyword kw">end</span></pre> </div> </div> </div> - </div> - - <div id="private-instance-method-details" class="method-section section"> - <h3 class="section-header">Private Instance Methods</h3> + <div id="write-method" class="method-detail "> + <a name="method-i-write"></a> - - <div id="close-ield-method" class="method-detail "> - <a name="M000879"></a> - <div class="method-heading"> - <span class="method-name">closeField</span><span - class="method-args">()</span> + <span class="method-name">write</span><span + class="method-args">(fileName)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> + <p> +Use this function to write the table into a CSV file <tt>fileName</tt>. +&#8217;.&#8217; can be used to write to $stdout. +</p> - <div class="method-source-code" - id="close-ield-source"> + id="write-source"> <pre> - <span class="ruby-comment cmt"># File lib/reports/CSVFile.rb, line 137</span> -137: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">closeField</span> -138: <span class="ruby-ivar">@fields</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@field</span> -139: <span class="ruby-ivar">@field</span> = <span class="ruby-value str">''</span> -140: <span class="ruby-ivar">@state</span> = <span class="ruby-value">0</span> -141: <span class="ruby-keyword kw">end</span></pre> + <span class="ruby-comment cmt"># File lib/reports/CSVFile.rb, line 37</span> +37: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">write</span>(<span class="ruby-identifier">fileName</span>) +38: <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">fileName</span> <span class="ruby-operator">==</span> <span class="ruby-value str">'.'</span>) +39: <span class="ruby-identifier">file</span> = <span class="ruby-identifier">$stdout</span> +40: <span class="ruby-keyword kw">else</span> +41: <span class="ruby-identifier">file</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">fileName</span>, <span class="ruby-value str">'w'</span>) +42: <span class="ruby-keyword kw">end</span> +43: +44: <span class="ruby-identifier">file</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">to_s</span>) +45: +46: <span class="ruby-identifier">file</span>.<span class="ruby-identifier">close</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">fileName</span> <span class="ruby-operator">==</span> <span class="ruby-value str">'.'</span> +47: <span class="ruby-keyword kw">end</span></pre> </div> </div> </div> + </div> + + <div id="private-instance-method-details" class="method-section section"> + <h3 class="section-header">Private Instance Methods</h3> + + <div id="marshal-method" class="method-detail "> - <a name="M000877"></a> + <a name="method-i-marshal"></a> <div class="method-heading"> <span class="method-name">marshal</span><span class="method-args">(field)</span> @@ -809,89 +993,67 @@ <div class="method-source-code" id="marshal-source"> <pre> - <span class="ruby-comment cmt"># File lib/reports/CSVFile.rb, line 87</span> -87: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">marshal</span>(<span class="ruby-identifier">field</span>) -88: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">field</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-ivar">@quote</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">field</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-ivar">@separator</span>) -89: <span class="ruby-identifier">field</span>.<span class="ruby-identifier">gsub!</span>(<span class="ruby-regexp re">/@quote/</span>, <span class="ruby-value str">'&quot;&quot;'</span>) -90: <span class="ruby-identifier">field</span> = <span class="ruby-value str">'&quot;'</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">field</span> <span class="ruby-operator">+</span> <span class="ruby-value str">'&quot;'</span> -91: <span class="ruby-keyword kw">end</span> -92: <span class="ruby-identifier">field</span> -93: <span class="ruby-keyword kw">end</span></pre> + <span class="ruby-comment cmt"># File lib/reports/CSVFile.rb, line 201</span> +201: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">marshal</span>(<span class="ruby-identifier">field</span>) +202: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">field</span>.<span class="ruby-identifier">nil?</span> +203: <span class="ruby-value str">''</span> +204: <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">field</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Fixnum</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">field</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Float</span>) +205: <span class="ruby-comment cmt"># Numbers don't have to be quoted.</span> +206: <span class="ruby-identifier">field</span>.<span class="ruby-identifier">to_s</span> +207: <span class="ruby-keyword kw">else</span> +208: <span class="ruby-comment cmt"># Duplicate quote characters.</span> +209: <span class="ruby-identifier">f</span> = <span class="ruby-identifier">field</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-regexp re">/@quote/</span>, <span class="ruby-node">&quot;#{@quote * 2}&quot;</span>) +210: <span class="ruby-comment cmt"># Enclose the field in quote characters</span> +211: <span class="ruby-ivar">@quote</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">f</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-operator">+</span> <span class="ruby-ivar">@quote</span> +212: <span class="ruby-keyword kw">end</span> +213: <span class="ruby-keyword kw">end</span></pre> </div> </div> </div> - <div id="parse-ine-method" class="method-detail "> - <a name="M000878"></a> + <div id="un-arshal-method" class="method-detail "> + <a name="method-i-unMarshal"></a> <div class="method-heading"> - <span class="method-name">parseLine</span><span - class="method-args">(line)</span> + <span class="method-name">unMarshal</span><span + class="method-args">(field, quoted)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> + <p> +Convert the <a href="../String.html">String</a> <em>field</em> into a +native Ruby type. If field was <em>quoted</em>, the result is always the <a +href="../String.html">String</a>. +</p> - <div class="method-source-code" - id="parse-ine-source"> + id="un-arshal-source"> <pre> - <span class="ruby-comment cmt"># File lib/reports/CSVFile.rb, line 95</span> - 95: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">parseLine</span>(<span class="ruby-identifier">line</span>) - 96: <span class="ruby-ivar">@state</span> = <span class="ruby-value">0</span> <span class="ruby-comment cmt"># start of field</span> - 97: <span class="ruby-ivar">@fields</span> = [] - 98: <span class="ruby-ivar">@field</span> = <span class="ruby-value str">''</span> - 99: <span class="ruby-identifier">line</span>.<span class="ruby-identifier">each_utf8_char</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">c</span><span class="ruby-operator">|</span> -100: <span class="ruby-keyword kw">case</span> <span class="ruby-ivar">@state</span> -101: <span class="ruby-keyword kw">when</span> <span class="ruby-value">0</span> <span class="ruby-comment cmt"># start of field</span> -102: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">c</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@quote</span> -103: <span class="ruby-ivar">@state</span> = <span class="ruby-value">1</span> -104: <span class="ruby-keyword kw">else</span> -105: <span class="ruby-ivar">@field</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">c</span> -106: <span class="ruby-ivar">@state</span> = <span class="ruby-value">2</span> -107: <span class="ruby-keyword kw">end</span> -108: <span class="ruby-keyword kw">when</span> <span class="ruby-value">1</span> <span class="ruby-comment cmt"># in quoted field</span> -109: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">c</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@quote</span> -110: <span class="ruby-ivar">@state</span> = <span class="ruby-value">3</span> -111: <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">c</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@separator</span> -112: <span class="ruby-identifier">closeField</span> -113: <span class="ruby-keyword kw">else</span> -114: <span class="ruby-ivar">@field</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">c</span> -115: <span class="ruby-keyword kw">end</span> -116: <span class="ruby-keyword kw">when</span> <span class="ruby-value">2</span> <span class="ruby-comment cmt"># in unquoted field</span> -117: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">c</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@separator</span> -118: <span class="ruby-identifier">closeField</span> -119: <span class="ruby-keyword kw">else</span> -120: <span class="ruby-ivar">@field</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">c</span> -121: <span class="ruby-keyword kw">end</span> -122: <span class="ruby-keyword kw">when</span> <span class="ruby-value">3</span> <span class="ruby-comment cmt"># quote found in quoted field</span> -123: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">c</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@quote</span> -124: <span class="ruby-ivar">@field</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">c</span> -125: <span class="ruby-ivar">@state</span> = <span class="ruby-value">2</span> -126: <span class="ruby-keyword kw">else</span> -127: <span class="ruby-identifier">closeField</span> -128: <span class="ruby-keyword kw">end</span> -129: <span class="ruby-keyword kw">else</span> -130: <span class="ruby-identifier">raise</span> <span class="ruby-node">&quot;Unknown state #{state}&quot;</span> -131: <span class="ruby-keyword kw">end</span> -132: <span class="ruby-keyword kw">end</span> -133: <span class="ruby-identifier">closeField</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@state</span> <span class="ruby-operator">!=</span> <span class="ruby-value">0</span> -134: <span class="ruby-ivar">@fields</span> -135: <span class="ruby-keyword kw">end</span></pre> + <span class="ruby-comment cmt"># File lib/reports/CSVFile.rb, line 217</span> +217: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">unMarshal</span>(<span class="ruby-identifier">field</span>, <span class="ruby-identifier">quoted</span>) +218: <span class="ruby-comment cmt"># Quoted Strings and nil are returned verbatim.</span> +219: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">quoted</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">field</span>.<span class="ruby-identifier">nil?</span> +220: <span class="ruby-identifier">field</span> +221: <span class="ruby-keyword kw">else</span> +222: <span class="ruby-comment cmt"># Unquoted fields are inspected for special types</span> +223: <span class="ruby-constant">CSVFile</span>.<span class="ruby-identifier">strToNative</span>(<span class="ruby-identifier">field</span>) +224: <span class="ruby-keyword kw">end</span> +225: <span class="ruby-keyword kw">end</span></pre> </div> </div>