potatosalad-csv_builder 2.0.2 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/csv_builder.gemspec +1 -1
- data/lib/csv_builder.rb +0 -1
- data/lib/csv_builder/filter_proxy.rb +15 -13
- data/lib/csv_builder/template_handler.rb +41 -40
- data/lib/csv_builder/version.rb +1 -1
- data/spec/controllers/csv_builder_spec.rb +1 -2
- data/spec/spec_helper.rb +6 -4
- metadata +37 -36
data/csv_builder.gemspec
CHANGED
@@ -15,7 +15,7 @@ Gem::Specification.new do |gem|
|
|
15
15
|
gem.name = "potatosalad-csv_builder"
|
16
16
|
gem.require_paths = ['lib']
|
17
17
|
gem.version = CsvBuilder::VERSION
|
18
|
-
gem.requirements = [%q{
|
18
|
+
gem.requirements = [%q{Ruby 1.9.x or FasterCSV}]
|
19
19
|
|
20
20
|
gem.add_dependency "rails", ">= 3.0.0"
|
21
21
|
end
|
data/lib/csv_builder.rb
CHANGED
@@ -1,33 +1,35 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
module CsvBuilder
|
4
|
+
|
5
|
+
DEFAULT_INPUT_ENCODING = Encoding::UTF_8
|
6
|
+
DEFAULT_OUTPUT_ENCODING = Encoding::ISO_8859_1
|
7
|
+
|
4
8
|
class FilterProxy < Proxy
|
5
9
|
|
6
10
|
# Transliterate into the required encoding if necessary
|
7
11
|
def initialize(data, options = {})
|
8
12
|
@options = options.dup
|
9
13
|
|
10
|
-
|
14
|
+
#@options.reverse_merge!(:input_encoding => 'UTF-8', :output_encoding => 'LATIN1')
|
15
|
+
@options.reverse_merge!(:input_encoding => DEFAULT_INPUT_ENCODING, :output_encoding => DEFAULT_OUTPUT_ENCODING)
|
11
16
|
|
12
17
|
@input_encoding = @options.delete(:input_encoding)
|
13
18
|
@output_encoding = @options.delete(:output_encoding)
|
14
|
-
|
15
|
-
super(data, @options)
|
16
19
|
end
|
17
20
|
|
18
21
|
# Transliterate before passing to CSV so that the right characters (e.g. quotes) get escaped
|
19
22
|
def <<(row)
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
23
|
+
begin
|
24
|
+
base << [*row].map do |value|
|
25
|
+
v = value.to_s
|
26
|
+
v.force_encoding(@input_encoding)
|
27
|
+
v.encode(@output_encoding, :undef => :replace)
|
28
|
+
v.encode!
|
29
|
+
end
|
30
|
+
rescue
|
31
|
+
base << [*row]
|
28
32
|
end
|
29
|
-
|
30
|
-
base << if @iconv then row.map { |value| @iconv.iconv(value.to_s) } else row end
|
31
33
|
end
|
32
34
|
|
33
35
|
alias :add_row :<<
|
@@ -2,46 +2,47 @@
|
|
2
2
|
|
3
3
|
module CsvBuilder # :nodoc:
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
5
|
+
# Template handler for csv templates
|
6
|
+
#
|
7
|
+
# Add rows to your CSV file in the template by pushing arrays of columns into csv
|
8
|
+
#
|
9
|
+
# # First row
|
10
|
+
# csv << [ 'cell 1', 'cell 2' ]
|
11
|
+
# # Second row
|
12
|
+
# csv << [ 'another cell value', 'and another' ]
|
13
|
+
# # etc...
|
14
|
+
#
|
15
|
+
# You can set the default filename for that a browser will use for 'save as' by
|
16
|
+
# setting <tt>@filename</tt> instance variable in your controller's action method
|
17
|
+
# e.g.
|
18
|
+
#
|
19
|
+
# @filename = 'report.csv'
|
20
|
+
#
|
21
|
+
# You can also set the input encoding and output encoding by setting
|
22
|
+
# <tt>@input_encoding</tt> and <tt>@output_encoding</tt> instance variables.
|
23
|
+
# These default to 'UTF-8' and 'LATIN1' respectively. e.g.
|
24
|
+
#
|
25
|
+
# @output_encoding = 'UTF-8'
|
26
|
+
class TemplateHandler
|
27
|
+
class_attribute :default_format
|
28
|
+
self.default_format = Mime::CSV
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
end
|
30
|
+
def call(template)
|
31
|
+
<<-EOV
|
32
|
+
begin;
|
33
|
+
self.output_buffer = String.new;
|
34
|
+
csv = CsvBuilder::CsvProxy.new(self.output_buffer, @csv_builder || {});
|
35
|
+
#{template.source};
|
36
|
+
if @csv_filename;
|
37
|
+
controller.response.headers['Content-Disposition'] = %Q{attachment; filename="\#{@csv_filename}"};
|
38
|
+
end;
|
39
|
+
self.output_buffer;
|
40
|
+
rescue Exception => e;
|
41
|
+
Rails.logger.warn("Exception \#{e} \#{e.message} with class \#{e.class.name} thrown when rendering CSV");
|
42
|
+
raise e;
|
43
|
+
end;
|
44
|
+
EOV
|
46
45
|
end
|
46
|
+
|
47
|
+
end
|
47
48
|
end
|
data/lib/csv_builder/version.rb
CHANGED
@@ -52,8 +52,7 @@ describe CsvBuilderReportsController do
|
|
52
52
|
describe "Layout with options" do
|
53
53
|
it "sets output encoding correctly" do
|
54
54
|
get 'encoding', :format => 'csv'
|
55
|
-
|
56
|
-
response.body.to_s.should == correct_output
|
55
|
+
response.body.to_s.should == generate({ :output_encoding => 'UTF-8' }, ['ąčęėįšųūž'])
|
57
56
|
end
|
58
57
|
|
59
58
|
it "passes csv options" do
|
data/spec/spec_helper.rb
CHANGED
@@ -14,9 +14,11 @@ TEST_DATA = [
|
|
14
14
|
]
|
15
15
|
|
16
16
|
def generate(options = {}, data = TEST_DATA)
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
17
|
+
output = String.new
|
18
|
+
output.force_encoding(options[:output_encoding] || CsvBuilder::DEFAULT_OUTPUT_ENCODING)
|
19
|
+
csv = CsvBuilder::CsvProxy.new(output, options)
|
20
|
+
data.each do |row|
|
21
|
+
csv << row
|
21
22
|
end
|
23
|
+
output
|
22
24
|
end
|
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: potatosalad-csv_builder
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.3
|
4
5
|
prerelease:
|
5
|
-
version: 2.0.2
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Econsultancy
|
9
9
|
- Vidmantas Kabosis
|
10
10
|
- Gabe da Silveira
|
@@ -12,31 +12,28 @@ authors:
|
|
12
12
|
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
- !ruby/object:Gem::Dependency
|
15
|
+
date: 2011-07-12 00:00:00.000000000 Z
|
16
|
+
dependencies:
|
17
|
+
- !ruby/object:Gem::Dependency
|
19
18
|
name: rails
|
20
|
-
|
21
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
requirement: &2151896480 !ruby/object:Gem::Requirement
|
22
20
|
none: false
|
23
|
-
requirements:
|
24
|
-
- -
|
25
|
-
- !ruby/object:Gem::Version
|
21
|
+
requirements:
|
22
|
+
- - ! '>='
|
23
|
+
- !ruby/object:Gem::Version
|
26
24
|
version: 3.0.0
|
27
25
|
type: :runtime
|
28
|
-
|
29
|
-
|
30
|
-
|
26
|
+
prerelease: false
|
27
|
+
version_requirements: *2151896480
|
28
|
+
description: CSV template handler for Rails. Enables :format => 'csv' in controllers,
|
29
|
+
with templates of the form report.csv.csvbuilder.
|
30
|
+
email:
|
31
31
|
- andrew@delorum.com
|
32
32
|
- gabe@websaviour.com
|
33
33
|
executables: []
|
34
|
-
|
35
34
|
extensions: []
|
36
|
-
|
37
35
|
extra_rdoc_files: []
|
38
|
-
|
39
|
-
files:
|
36
|
+
files:
|
40
37
|
- .gitignore
|
41
38
|
- .rspec
|
42
39
|
- CHANGELOG.rdoc
|
@@ -77,34 +74,38 @@ files:
|
|
77
74
|
- spec/templates/csv_builder_reports/simple.csv.csvbuilder
|
78
75
|
- spec/templates/csv_builder_reports/simple.html.erb
|
79
76
|
homepage: http://github.com/potatosalad/csv_builder
|
80
|
-
licenses:
|
77
|
+
licenses:
|
81
78
|
- MIT
|
82
79
|
post_install_message:
|
83
80
|
rdoc_options: []
|
84
|
-
|
85
|
-
require_paths:
|
81
|
+
require_paths:
|
86
82
|
- lib
|
87
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
84
|
none: false
|
89
|
-
requirements:
|
90
|
-
- -
|
91
|
-
- !ruby/object:Gem::Version
|
92
|
-
version:
|
93
|
-
|
85
|
+
requirements:
|
86
|
+
- - ! '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
segments:
|
90
|
+
- 0
|
91
|
+
hash: 4278657704769142768
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
93
|
none: false
|
95
|
-
requirements:
|
96
|
-
- -
|
97
|
-
- !ruby/object:Gem::Version
|
98
|
-
version:
|
99
|
-
|
100
|
-
-
|
94
|
+
requirements:
|
95
|
+
- - ! '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
segments:
|
99
|
+
- 0
|
100
|
+
hash: 4278657704769142768
|
101
|
+
requirements:
|
101
102
|
- Ruby 1.9.x or FasterCSV
|
102
103
|
rubyforge_project:
|
103
104
|
rubygems_version: 1.8.5
|
104
105
|
signing_key:
|
105
106
|
specification_version: 3
|
106
107
|
summary: CSV template handler for Rails
|
107
|
-
test_files:
|
108
|
+
test_files:
|
108
109
|
- spec/controllers/csv_builder_spec.rb
|
109
110
|
- spec/rails_app/.gitignore
|
110
111
|
- spec/rails_app/Gemfile
|