Permalink
Browse files

Generated documentation for 412d63b

  • Loading branch information...
1 parent bea5b40 commit 1c58f114698b7888650d9112499b389c0454f422 @paulspringett committed Dec 11, 2012
Showing with 51 additions and 19 deletions.
  1. +2 −2 csv_shaper.html
  2. +9 −3 csv_shaper/encoder.html
  3. +15 −5 csv_shaper/row.html
  4. +15 −6 csv_shaper/shaper.html
  5. +1 −1 csv_shaper/version.html
  6. +1 −1 csv_shaper_template.html
  7. +8 −1 index.html
View
@@ -9,8 +9,8 @@
<span class="nb">require</span> <span class="s1">&#39;csv_shaper/shaper&#39;</span>
<span class="k">module</span> <span class="nn">CsvShaper</span>
- <span class="k">class</span> <span class="nc">MissingHeadersError</span> <span class="o">&lt;</span> <span class="no">StandardError</span><span class="p">;</span> <span class="k">end</span></div></div></div><div class="segment"><div class="comments"><div class="wrapper"><p>Shortcut the encode method</p></div></div><div class="code"><div class="wrapper"> <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">encode</span><span class="p">(</span><span class="o">&amp;</span><span class="n">block</span><span class="p">)</span>
- <span class="no">CsvShaper</span><span class="o">::</span><span class="no">Shaper</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="o">&amp;</span><span class="n">block</span><span class="p">)</span>
+ <span class="k">class</span> <span class="nc">MissingHeadersError</span> <span class="o">&lt;</span> <span class="no">StandardError</span><span class="p">;</span> <span class="k">end</span></div></div></div><div class="segment"><div class="comments"><div class="wrapper"><p>Shortcut the encode method</p></div></div><div class="code"><div class="wrapper"> <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">encode</span><span class="p">(</span><span class="n">options</span> <span class="o">=</span> <span class="p">{},</span> <span class="o">&amp;</span><span class="n">block</span><span class="p">)</span>
+ <span class="no">CsvShaper</span><span class="o">::</span><span class="no">Shaper</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">block</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">configure</span><span class="p">(</span><span class="o">&amp;</span><span class="n">block</span><span class="p">)</span>
View
@@ -17,19 +17,25 @@
</div></div></div><div class="segment"><div class="comments"><div class="wrapper"><p>Public: converts the Shaper mapped headers and rows into
a CSV String</p>
-<p>Returns a String</p></div></div><div class="code"><div class="wrapper"> <span class="k">def</span> <span class="nf">to_csv</span>
+<p>Returns a String</p></div></div><div class="code"><div class="wrapper"> <span class="k">def</span> <span class="nf">to_csv</span><span class="p">(</span><span class="n">local_config</span> <span class="o">=</span> <span class="kp">nil</span><span class="p">)</span>
+ <span class="n">csv_options</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span><span class="n">local_options</span><span class="p">(</span><span class="n">local_config</span><span class="p">))</span>
+
<span class="n">rows</span> <span class="o">=</span> <span class="n">padded_rows</span><span class="o">.</span><span class="n">map</span> <span class="k">do</span> <span class="o">|</span><span class="n">data</span><span class="o">|</span>
<span class="no">CSV</span><span class="o">::</span><span class="no">Row</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="vi">@header</span><span class="o">.</span><span class="n">mapped_columns</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="kp">false</span><span class="p">)</span>
<span class="k">end</span>
-
+
<span class="n">table</span> <span class="o">=</span> <span class="no">CSV</span><span class="o">::</span><span class="no">Table</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">rows</span><span class="p">)</span>
- <span class="n">table</span><span class="o">.</span><span class="n">to_csv</span><span class="p">(</span><span class="n">options</span><span class="p">)</span>
+ <span class="n">table</span><span class="o">.</span><span class="n">to_csv</span><span class="p">(</span><span class="n">csv_options</span><span class="p">)</span>
<span class="k">end</span>
<span class="kp">private</span>
<span class="k">def</span> <span class="nf">options</span>
<span class="no">CsvShaper</span><span class="o">::</span><span class="no">Shaper</span><span class="o">.</span><span class="n">config</span> <span class="o">&amp;&amp;</span> <span class="no">CsvShaper</span><span class="o">::</span><span class="no">Shaper</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">options</span> <span class="o">||</span> <span class="p">{}</span>
+ <span class="k">end</span>
+
+ <span class="k">def</span> <span class="nf">local_options</span><span class="p">(</span><span class="n">local_config</span><span class="p">)</span>
+ <span class="n">local_config</span> <span class="o">&amp;&amp;</span> <span class="n">local_config</span><span class="o">.</span><span class="n">options</span> <span class="o">||</span> <span class="p">{}</span>
<span class="k">end</span></div></div></div><div class="segment"><div class="comments"><div class="wrapper"><p>Internal: make use of <code>CSV#values_at</code> to pad out the
cells into the correct columns for the headers</p>
View
@@ -51,13 +51,23 @@
<p><code>column</code> - Symbol of the column to add to value to
<code>value</code> - data to assign to the cell (default: nil)</p>
-<p>Returns an Array of the Row's cells</p></div></div><div class="code"><div class="wrapper"> <span class="k">def</span> <span class="nf">cell</span><span class="p">(</span><span class="n">column</span><span class="p">,</span> <span class="n">value</span> <span class="o">=</span> <span class="kp">nil</span><span class="p">)</span>
- <span class="n">column</span> <span class="o">=</span> <span class="n">column</span><span class="o">.</span><span class="n">to_sym</span>
+<p>Returns an Array of the Row's cells</p></div></div><div class="code"><div class="wrapper"> <span class="k">def</span> <span class="nf">cell</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">)</span>
+ <span class="k">if</span> <span class="n">args</span><span class="o">.</span><span class="n">empty?</span>
+ <span class="k">raise</span> <span class="no">ArgumentError</span><span class="p">,</span> <span class="s1">&#39;no args passed to #cell, you must pass at least a column name&#39;</span>
+ <span class="k">end</span>
+
+ <span class="n">column</span> <span class="o">=</span> <span class="n">args</span><span class="o">.</span><span class="n">first</span><span class="o">.</span><span class="n">to_sym</span>
- <span class="k">if</span> <span class="vi">@model</span> <span class="o">&amp;&amp;</span> <span class="vi">@model</span><span class="o">.</span><span class="n">respond_to?</span><span class="p">(</span><span class="n">column</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="n">value</span><span class="o">.</span><span class="n">nil?</span>
- <span class="vi">@cells</span><span class="o">[</span><span class="n">column</span><span class="o">]</span> <span class="o">=</span> <span class="vi">@model</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">column</span><span class="p">)</span>
+ <span class="k">if</span> <span class="n">args</span><span class="o">.</span><span class="n">size</span> <span class="o">==</span> <span class="mi">2</span>
+ <span class="vi">@cells</span><span class="o">[</span><span class="n">column</span><span class="o">]</span> <span class="o">=</span> <span class="n">args</span><span class="o">.</span><span class="n">last</span>
+ <span class="k">elsif</span> <span class="n">args</span><span class="o">.</span><span class="n">size</span> <span class="o">==</span> <span class="mi">1</span>
+ <span class="k">if</span> <span class="vi">@model</span> <span class="o">&amp;&amp;</span> <span class="vi">@model</span><span class="o">.</span><span class="n">respond_to?</span><span class="p">(</span><span class="n">column</span><span class="p">)</span>
+ <span class="vi">@cells</span><span class="o">[</span><span class="n">column</span><span class="o">]</span> <span class="o">=</span> <span class="vi">@model</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">column</span><span class="p">)</span>
+ <span class="k">else</span>
+ <span class="k">raise</span> <span class="no">ArgumentError</span><span class="p">,</span> <span class="s2">&quot;#</span><span class="si">#{</span><span class="n">column</span><span class="si">}</span><span class="s2"> is not a method on </span><span class="si">#{</span><span class="vi">@model</span><span class="o">.</span><span class="n">class</span><span class="o">.</span><span class="n">to_s</span><span class="si">}</span><span class="s2">, call `csv.cell </span><span class="si">#{</span><span class="n">column</span><span class="si">}</span><span class="s2">, value` instead&quot;</span>
+ <span class="k">end</span>
<span class="k">else</span>
- <span class="vi">@cells</span><span class="o">[</span><span class="n">column</span><span class="o">]</span> <span class="o">=</span> <span class="n">value</span>
+ <span class="k">raise</span> <span class="no">ArgumentError</span><span class="p">,</span> <span class="s1">&#39;you can pass a column or a column with a value to #cell&#39;</span>
<span class="k">end</span>
<span class="vi">@cells</span>
View
@@ -6,8 +6,9 @@
<span class="kp">attr_accessor</span> <span class="ss">:config</span>
<span class="k">end</span>
- <span class="k">def</span> <span class="nf">initialize</span>
+ <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">options</span> <span class="o">=</span> <span class="p">{})</span>
<span class="vi">@rows</span> <span class="o">=</span> <span class="o">[]</span>
+ <span class="n">local_configuration</span><span class="p">(</span><span class="n">options</span><span class="p">)</span>
<span class="k">yield</span> <span class="nb">self</span> <span class="k">if</span> <span class="nb">block_given?</span>
<span class="k">end</span>
</div></div></div><div class="segment"><div class="comments"><div class="wrapper"><p>Public: creates a new instance of Shaper taps it with
@@ -24,8 +25,8 @@
=&gt; "Name,Age,Gender\n'Joe Bloggs',25,'M'\n'John Smith',34,'M'"
</code></pre>
-<p>Returns a String</p></div></div><div class="code"><div class="wrapper"> <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">encode</span>
- <span class="kp">new</span><span class="o">.</span><span class="n">tap</span> <span class="p">{</span> <span class="o">|</span><span class="n">shaper</span><span class="o">|</span> <span class="k">yield</span> <span class="n">shaper</span> <span class="p">}</span><span class="o">.</span><span class="n">to_csv</span>
+<p>Returns a String</p></div></div><div class="code"><div class="wrapper"> <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">encode</span><span class="p">(</span><span class="n">options</span> <span class="o">=</span> <span class="p">{})</span>
+ <span class="kp">new</span><span class="p">(</span><span class="n">options</span><span class="p">)</span><span class="o">.</span><span class="n">tap</span> <span class="p">{</span> <span class="o">|</span><span class="n">shaper</span><span class="o">|</span> <span class="k">yield</span> <span class="n">shaper</span> <span class="p">}</span><span class="o">.</span><span class="n">to_csv</span>
<span class="k">end</span>
</div></div></div><div class="segment"><div class="comments"><div class="wrapper"><p>Public: creates a header row for the CSV
This is delegated to the Header class
@@ -61,10 +62,18 @@
of valid CSV data. Delegated to the Encoder class</p>
<p>Returns a String</p></div></div><div class="code"><div class="wrapper"> <span class="k">def</span> <span class="nf">to_csv</span>
- <span class="no">Encoder</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="vi">@header</span><span class="p">,</span> <span class="vi">@rows</span><span class="p">)</span><span class="o">.</span><span class="n">to_csv</span>
+ <span class="no">Encoder</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="vi">@header</span><span class="p">,</span> <span class="vi">@rows</span><span class="p">)</span><span class="o">.</span><span class="n">to_csv</span><span class="p">(</span><span class="vi">@local_config</span><span class="p">)</span>
<span class="k">end</span></div></div></div><div class="segment"><div class="comments"><div class="wrapper"><p>Public: Create an instance of the config and cache it
for reference by the Encoder later</p></div></div><div class="code"><div class="wrapper"> <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">configure</span><span class="p">(</span><span class="o">&amp;</span><span class="n">block</span><span class="p">)</span>
- <span class="vi">@config</span> <span class="o">||=</span> <span class="no">CsvShaper</span><span class="o">::</span><span class="no">Config</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="o">&amp;</span><span class="n">block</span><span class="p">)</span>
- <span class="k">end</span>
+ <span class="vi">@config</span> <span class="o">||=</span> <span class="no">CsvShaper</span><span class="o">::</span><span class="no">Config</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="o">&amp;</span><span class="n">block</span><span class="p">)</span>
+ <span class="k">end</span>
+
+ <span class="kp">private</span>
+
+ <span class="k">def</span> <span class="nf">local_configuration</span><span class="p">(</span><span class="n">options</span> <span class="o">=</span> <span class="p">{})</span>
+ <span class="vi">@local_config</span> <span class="o">=</span> <span class="no">CsvShaper</span><span class="o">::</span><span class="no">Config</span><span class="o">.</span><span class="n">new</span> <span class="k">do</span> <span class="o">|</span><span class="n">csv</span><span class="o">|</span>
+ <span class="n">options</span><span class="o">.</span><span class="n">each_pair</span> <span class="p">{</span> <span class="o">|</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="o">|</span> <span class="n">csv</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">#{</span><span class="n">k</span><span class="si">}</span><span class="s2">=&quot;</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="p">}</span>
+ <span class="k">end</span>
+ <span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span></div></div></div></div></body></html>
View
@@ -1,3 +1,3 @@
<!DOCTYPE html><html lang="en"><head><title>csv_shaper/version</title></head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"><meta name="groc-relative-root" content="../"><meta name="groc-document-path" content="csv_shaper/version"><meta name="groc-project-path" content="lib/csv_shaper/version.rb"><meta name="groc-github-url" content="https://github.com/paulspringett/paulspringett"><link rel="stylesheet" type="text/css" media="all" href="../assets/style.css"><script type="text/javascript" src="../assets/behavior.js"></script><body><div id="meta"><div class="file-path"><a href="https://github.com/paulspringett/paulspringett/blob/master/lib/csv_shaper/version.rb">lib/csv_shaper/version.rb</a></div></div><div id="document"><div class="segment"><div class="code"><div class="wrapper"><span class="k">module</span> <span class="nn">CsvShaper</span>
- <span class="no">VERSION</span> <span class="o">=</span> <span class="s2">&quot;0.1.1&quot;</span>
+ <span class="no">VERSION</span> <span class="o">=</span> <span class="s2">&quot;0.3.0&quot;</span>
<span class="k">end</span></div></div></div></div></body></html>
View
@@ -15,7 +15,7 @@
Set response headers with filename
Primarily calls CsvShaperTemplate.encode, passing through the context (self)</p></div></div><div class="code"><div class="wrapper"> <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">call</span><span class="p">(</span><span class="n">template</span><span class="p">)</span>
<span class="sx">%{</span>
-<span class="sx"> unless defined?(ActionMailer) &amp;&amp; defined?(ActionMailer::Base) &amp;&amp; controller.is_a?(ActionMailer::Base)</span>
+<span class="sx"> if ( controller.present? ) &amp;&amp; !( defined?(ActionMailer) &amp;&amp; defined?(ActionMailer::Base) &amp;&amp; controller.is_a?(ActionMailer::Base) )</span>
<span class="sx"> @filename ||= &quot;</span><span class="se">\#</span><span class="sx">{controller.action_name}.csv&quot;</span>
<span class="sx"> controller.response.headers[&quot;Content-Type&quot;] ||= &#39;text/csv&#39;</span>
<span class="sx"> controller.response.headers[&#39;Content-Disposition&#39;] = &quot;attachment; filename=\\</span><span class="se">\&quot;\#</span><span class="sx">{@filename}\\</span><span class="se">\&quot;</span><span class="sx">&quot;</span>
View
@@ -11,7 +11,7 @@
<h3 id="example-usage">Example Usage</h3>
<pre><code class="ruby">csv_string = CsvShaper.encode do |csv|
- csv.header :name, :age, :gender, :pet_names
+ csv.headers :name, :age, :gender, :pet_names
csv.rows @users do |csv, user|
csv.cells :name, :age, :gender
@@ -215,6 +215,13 @@ <h3 id="csv-configuration">CSV configuration</h3>
<p>If you're using Rails you can put this in an initializer.</p>
+<p>To configure CSV output locally to change global behavior you can define a configure hash, like so:</p>
+
+<pre><code class="ruby">CsvShaper.encode(col_sep: "\t") do |csv|
+ ...
+end
+</code></pre>
+
<h3 id="contributing">Contributing</h3>
<ol>

0 comments on commit 1c58f11

Please sign in to comment.