DefV-csv_builder 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (6) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README +52 -0
  3. data/Rakefile +11 -0
  4. data/lib/csv_builder.rb +77 -0
  5. data/rails/init.rb +3 -0
  6. metadata +57 -0
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Econsultancy.com
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,52 @@
1
+ = CSV Builder
2
+
3
+ The CSV Builder Rails plugin provides a simple templating system for serving
4
+ dynamically generated CSV files from your application.
5
+
6
+
7
+ == Requirements
8
+
9
+ CSV Builder requires Rails v2.1.
10
+
11
+ It also depends upon the FasterCSV gem http://fastercsv.rubyforge.org,
12
+ which you can install with
13
+
14
+ $ sudo gem install fastercsv
15
+
16
+
17
+ == Example
18
+
19
+ CSV template files are suffixed with '.csv.csvbuilder', for example
20
+ 'index.csv.csvbuilder'
21
+
22
+ Add rows to your CSV file in the template by pushing arrays of columns into the
23
+ csv object.
24
+
25
+ # First row
26
+ csv << [ 'cell 1', 'cell 2' ]
27
+ # Second row
28
+ csv << [ 'another cell value', 'and another' ]
29
+ # etc...
30
+
31
+ You can set the default filename for that a browser will use for 'save as' by
32
+ setting <tt>@filename</tt> instance variable in your controller's action method
33
+ e.g.
34
+
35
+ @filename = 'report.csv'
36
+
37
+ You can also set the input encoding and output encoding by setting
38
+ <tt>@input_encoding</tt> and <tt>@output_encoding</tt> instance variables.
39
+ These default to 'UTF-8' and 'LATIN1' respectively. e.g.
40
+
41
+ @output_encoding = 'UTF-8'
42
+
43
+ You can also attach a csv file to mail sent out by your application by
44
+ including a snippet like the following in your mailer method
45
+
46
+ attachment "text/csv" do |attachment|
47
+ attachment.body = render(:file => 'example/index.csv.csvbuilder')
48
+ attachment.filename = 'report.csv'
49
+ end
50
+
51
+
52
+ Copyright (c) 2008 Econsultancy.com, released under the MIT license
@@ -0,0 +1,11 @@
1
+ require 'rake'
2
+ require 'rake/rdoctask'
3
+
4
+ desc 'Generate documentation for the csv_builder plugin.'
5
+ Rake::RDocTask.new(:rdoc) do |rdoc|
6
+ rdoc.rdoc_dir = 'rdoc'
7
+ rdoc.title = 'CSV Builder'
8
+ rdoc.options << '--line-numbers' << '--inline-source'
9
+ rdoc.rdoc_files.include('README')
10
+ rdoc.rdoc_files.include('lib/**/*.rb')
11
+ end
@@ -0,0 +1,77 @@
1
+ require 'fastercsv'
2
+ require 'iconv'
3
+
4
+ module ActionView # :nodoc:
5
+ module TemplateHandlers
6
+ # Template handler for csv templates
7
+ #
8
+ # Add rows to your CSV file in the template by pushing arrays of columns into csv
9
+ #
10
+ # # First row
11
+ # csv << [ 'cell 1', 'cell 2' ]
12
+ # # Second row
13
+ # csv << [ 'another cell value', 'and another' ]
14
+ # # etc...
15
+ #
16
+ # You can set the default filename for that a browser will use for 'save as' by
17
+ # setting <tt>@filename</tt> instance variable in your controller's action method
18
+ # e.g.
19
+ #
20
+ # @filename = 'report.csv'
21
+ #
22
+ # You can also set the input encoding and output encoding by setting
23
+ # <tt>@input_encoding</tt> and <tt>@output_encoding</tt> instance variables.
24
+ # These default to 'UTF-8' and 'LATIN1' respectively. e.g.
25
+ #
26
+ # @output_encoding = 'UTF-8'
27
+
28
+ class CsvBuilder < TemplateHandler
29
+
30
+ include Compilable
31
+
32
+ def self.line_offset
33
+ 9
34
+ end
35
+
36
+ def compile(template)
37
+ <<-EOV
38
+ begin
39
+
40
+ unless defined?(ActionMailer) && defined?(ActionMailer::Base) && controller.is_a?(ActionMailer::Base)
41
+ @filename ||= "\#{controller.action_name}.csv"
42
+ controller.response.headers["Content-Type"] ||= 'text/csv'
43
+ controller.response.headers['Content-Disposition'] = "attachment; filename=\#{@filename}"
44
+ end
45
+
46
+ result = FasterCSV.generate do |csv|
47
+ #{template.source}
48
+ end
49
+
50
+ # Transliterate into the required encoding if necessary
51
+ # TODO: make defaults configurable
52
+ @input_encoding ||= 'UTF-8'
53
+ @output_encoding ||= 'LATIN1'
54
+
55
+ if @input_encoding == @output_encoding
56
+ result
57
+ else
58
+ # TODO: do some checking to make sure iconv works correctly in
59
+ # current environment. See ActiveSupport::Inflector#transliterate
60
+ # definition for details
61
+ #
62
+ # Not using the more standard //IGNORE//TRANLIST because it raises
63
+ # Iconv::IllegalSequence for some inputs
64
+ c = Iconv.new("\#{@output_encoding}//TRANSLIT//IGNORE", @input_encoding)
65
+ c.iconv(result)
66
+ end
67
+
68
+ rescue Exception => e
69
+ RAILS_DEFAULT_LOGGER.warn("Exception \#{e} \#{e.message} with class \#{e.class.name} thrown when rendering CSV")
70
+ raise e
71
+ end
72
+ EOV
73
+ end
74
+
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,3 @@
1
+ require 'csv_builder'
2
+
3
+ ActionView::Template.register_template_handler 'csvbuilder', ActionView::TemplateHandlers::CsvBuilder
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: DefV-csv_builder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Econsultancy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-26 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: CSV template Rails plugin
17
+ email: code@econsultancy.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - MIT-LICENSE
26
+ - README
27
+ - Rakefile
28
+ - rails/init.rb
29
+ - lib/csv_builder.rb
30
+ has_rdoc: false
31
+ homepage: http://github.com/DefV/csv_builder/tree/master
32
+ post_install_message:
33
+ rdoc_options: []
34
+
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project:
52
+ rubygems_version: 1.2.0
53
+ signing_key:
54
+ specification_version: 2
55
+ summary: CSV template Rails plugin
56
+ test_files: []
57
+