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.
@@ -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{iconv}, %q{Ruby 1.9.x or FasterCSV}]
18
+ gem.requirements = [%q{Ruby 1.9.x or FasterCSV}]
19
19
 
20
20
  gem.add_dependency "rails", ">= 3.0.0"
21
21
  end
@@ -11,7 +11,6 @@ module CsvBuilder
11
11
  end
12
12
 
13
13
  require 'action_view'
14
- require 'iconv'
15
14
  require 'csv_builder/proxy'
16
15
  require 'csv_builder/filter_proxy'
17
16
  require 'csv_builder/csv_proxy'
@@ -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
- @options.reverse_merge!(:input_encoding => 'UTF-8', :output_encoding => 'LATIN1')
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
- if @input_encoding != @output_encoding
21
- # TODO: do some checking to make sure iconv works correctly in
22
- # current environment. See ActiveSupport::Inflector#transliterate
23
- # definition for details
24
- #
25
- # Not using the more standard //IGNORE//TRANSLIT because it raises
26
- # Iconv::IllegalSequence for some inputs
27
- @iconv = Iconv.new("#{@output_encoding}//TRANSLIT//IGNORE", @input_encoding)
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
- # 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
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
- 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
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
@@ -1,3 +1,3 @@
1
1
  module CsvBuilder
2
- VERSION = '2.0.2'
2
+ VERSION = '2.0.3'
3
3
  end
@@ -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
- correct_output = generate({}, [Iconv.iconv('UTF-16//TRANSLIT//IGNORE', 'UTF-8', 'ąčęėįšųūž')])
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
@@ -14,9 +14,11 @@ TEST_DATA = [
14
14
  ]
15
15
 
16
16
  def generate(options = {}, data = TEST_DATA)
17
- CsvBuilder::CSV.generate(options) do |csv|
18
- data.each do |row|
19
- csv << row
20
- end
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
- date: 2011-06-28 00:00:00 Z
17
- dependencies:
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
- prerelease: false
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
- version_requirements: *id001
29
- description: CSV template handler for Rails. Enables :format => 'csv' in controllers, with templates of the form report.csv.csvbuilder.
30
- email:
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: "0"
93
- required_rubygems_version: !ruby/object:Gem::Requirement
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: "0"
99
- requirements:
100
- - iconv
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