csv_shaper 1.1.0 → 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: ebfdda4a218f9cf86f42c9158f3ea0baa77de655
4
- data.tar.gz: 7cc018cfafa1d59ae8589f087f71e9556c960e8f
2
+ SHA256:
3
+ metadata.gz: c06a84401d676f513ab8f089bfcea8cb244d806aabf77f4656e44ecc8d9a4f6e
4
+ data.tar.gz: 9e227b8653d95f8ab32b5d330fa7b14045c4e67db35e53f20406bb17a4e195fa
5
5
  SHA512:
6
- metadata.gz: 644876cb037fa67e9f7bb79bd84880e74aac4b1b75143427f687db9ce794a5c75ae2f7b7d4323631792fb07618c579294ff42e2538af96fb5cd88994fa62040b
7
- data.tar.gz: e71f61f64dedb5785e01ac9037d75f9e093ffed3b36238006da5f9d2a0c5466a8d9fc35918615284b53701b38537d5c1ab5b67489aafc7880c0475687f91f2a6
6
+ metadata.gz: c9b10a4737247def56c00ddd6897b96d3547dafca91c041a93c50a2270efb5b2b215adaf8cda876b111fa59a637441e92f542a2c63f7f8d1cbe0140871b250d6
7
+ data.tar.gz: 2ad0ae7d740549839707e44273a6577ef5db6227e69198234d6cad784909c65055302d57dcad8413a096ca87febef1d9c5cb739cb9949e0346db0149eba52a21
@@ -1 +1 @@
1
- 2.1.4
1
+ 2.3.1
@@ -0,0 +1 @@
1
+ ruby 2.7.0
@@ -1,4 +1,4 @@
1
1
  rvm:
2
- - 1.9.3
3
- - 2.0.0
4
- - 2.1.0
2
+ - 2.7.1
3
+ - 2.6.6
4
+ - 2.5.8
data/README.md CHANGED
@@ -27,7 +27,7 @@ end
27
27
 
28
28
  ### Install
29
29
 
30
- **Requires Ruby 1.9+**
30
+ **Requires Ruby 2.2+**
31
31
 
32
32
  Install using Rubygems
33
33
 
@@ -264,6 +264,12 @@ CsvShaper.encode(col_sep: "\t") do |csv|
264
264
  end
265
265
  ```
266
266
 
267
+ To configure Rails template-specific CSV output, use the `config` method on the `csv` object:
268
+
269
+ ```ruby
270
+ csv.config.col_sep = "\t"
271
+ ```
272
+
267
273
  ### Contributing
268
274
 
269
275
  1. Fork it
@@ -21,4 +21,4 @@ module CsvShaper
21
21
  end
22
22
  end
23
23
 
24
- require "csv_shaper_template" if defined?(ActionView::Template)
24
+ require "csv_shaper_handler" if defined?(ActionView::Template)
@@ -27,9 +27,13 @@ module CsvShaper
27
27
  CSV::Row.new(@header.mapped_columns, data, false)
28
28
  end
29
29
 
30
+ if csv_options[:write_headers] && rows.empty?
31
+ rows << CSV::Row.new(@header.mapped_columns, [], true)
32
+ end
33
+
30
34
  table = CSV::Table.new(rows)
31
35
  csv_options.except!(*custom_options.keys)
32
- table.to_csv(csv_options)
36
+ table.to_csv(**csv_options)
33
37
  end
34
38
 
35
39
  private
@@ -4,8 +4,12 @@ module CsvShaper
4
4
  class Shaper
5
5
  attr_reader :header, :rows
6
6
 
7
- class << self
8
- attr_accessor :config
7
+ def self.config
8
+ @config ||= Config.new
9
+ end
10
+
11
+ def self.config=(new_config)
12
+ @config = new_config
9
13
  end
10
14
 
11
15
  def initialize(options = {})
@@ -1,3 +1,3 @@
1
1
  module CsvShaper
2
- VERSION = "1.1.0"
2
+ VERSION = "1.3.2"
3
3
  end
@@ -0,0 +1,27 @@
1
+ require 'csv_shaper_template'
2
+
3
+ # CsvShaperHandler
4
+ # Template handler for Rails
5
+ class CsvShaperHandler
6
+ cattr_accessor :default_format
7
+ self.default_format = Mime[:csv]
8
+
9
+ # Expected `call` class method
10
+ # Set response headers with filename
11
+ # Primarily calls CsvShaperTemplate.encode, passing through the context (self)
12
+ def self.call(template, source = nil)
13
+ %{
14
+ if ( controller.present? ) && !( defined?(ActionMailer) && defined?(ActionMailer::Base) && controller.is_a?(ActionMailer::Base) )
15
+ @filename ||= "\#{controller.action_name}.csv"
16
+ controller.response.headers["Content-Type"] ||= 'text/csv'
17
+ controller.response.headers['Content-Disposition'] = "attachment; filename=\\\"\#{@filename}\\\""
18
+ end
19
+
20
+ CsvShaperTemplate.encode(self) do |csv|
21
+ #{source || template.source}
22
+ end
23
+ }
24
+ end
25
+ end
26
+
27
+ ActionView::Template.register_template_handler :shaper, CsvShaperHandler
@@ -11,30 +11,8 @@ class CsvShaperTemplate < CsvShaper::Shaper
11
11
  @context = context
12
12
  super()
13
13
  end
14
- end
15
-
16
- # CsvShaperHandler
17
- # Template handler for Rails
18
- class CsvShaperHandler
19
- cattr_accessor :default_format
20
- self.default_format = Mime::CSV
21
14
 
22
- # Expected `call` class method
23
- # Set response headers with filename
24
- # Primarily calls CsvShaperTemplate.encode, passing through the context (self)
25
- def self.call(template)
26
- %{
27
- if ( controller.present? ) && !( defined?(ActionMailer) && defined?(ActionMailer::Base) && controller.is_a?(ActionMailer::Base) )
28
- @filename ||= "\#{controller.action_name}.csv"
29
- controller.response.headers["Content-Type"] ||= 'text/csv'
30
- controller.response.headers['Content-Disposition'] = "attachment; filename=\\\"\#{@filename}\\\""
31
- end
32
-
33
- CsvShaperTemplate.encode(self) do |csv|
34
- #{template.source}
35
- end
36
- }
15
+ def config
16
+ @local_config
37
17
  end
38
18
  end
39
-
40
- ActionView::Template.register_template_handler :shaper, CsvShaperHandler
@@ -13,6 +13,20 @@ describe CsvShaper::Config do
13
13
  expect(config.options).to eq({ write_headers: false, col_sep: "\t", header_inflector: :titleize })
14
14
  end
15
15
 
16
+ it "does not require setting up the config before generating a CSV file" do
17
+ shaper = CsvShaper::Shaper.new do |csv|
18
+ csv.headers :name, :age, :gender
19
+
20
+ csv.row do |csv|
21
+ csv.cell :name, 'Paul'
22
+ csv.cell :age, '27'
23
+ csv.cell :gender, 'Male'
24
+ end
25
+ end
26
+
27
+ expect(shaper.to_csv).to eq "Name,Age,Gender\nPaul,27,Male\n"
28
+ end
29
+
16
30
  it "should exclude the headers if specified" do
17
31
  CsvShaper::Shaper.config = config
18
32
 
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+ require "csv_shaper_template"
3
+
4
+ describe CsvShaperTemplate do
5
+ it "should allow configuration via #config method" do
6
+ csv_string = CsvShaperTemplate.encode(double('context')) do |csv|
7
+ csv.config.write_headers = false
8
+ csv.config.col_sep = ';'
9
+
10
+ csv.headers :name, :age, :gender
11
+
12
+ csv.row do |csv|
13
+ csv.cell :name, 'Paul'
14
+ csv.cell :age, '27'
15
+ csv.cell :gender, 'Male'
16
+ end
17
+ end
18
+
19
+ expect(csv_string).to eq "Paul;27;Male\n"
20
+ end
21
+
22
+ it "should override global configuration with local configuration" do
23
+ CsvShaper::Shaper.config = CsvShaper::Config.new do |c|
24
+ c.write_headers = false
25
+ c.col_sep = "\t"
26
+ end
27
+
28
+ csv_string = CsvShaperTemplate.encode(double('context')) do |csv|
29
+ csv.config.col_sep = ','
30
+
31
+ csv.headers :name, :age, :gender
32
+
33
+ csv.row do |csv|
34
+ csv.cell :name, 'Paul'
35
+ csv.cell :age, '27'
36
+ csv.cell :gender, 'Male'
37
+ end
38
+ end
39
+
40
+ expect(csv_string).to eq "Paul,27,Male\n"
41
+ end
42
+ end
@@ -40,4 +40,10 @@ describe CsvShaper::Encoder do
40
40
  encoder = CsvShaper::Encoder.new(csv.header, csv.rows)
41
41
  expect(encoder.to_csv).to eq("Full name,Sex,Age\nPaul,Male,27\nBob,Male,31\nJane,Female,23\n,,81\n")
42
42
  end
43
+
44
+ it "should encode a Shaper instance with no rows to a CSV string" do
45
+ CsvShaper::Shaper.config = config
46
+ encoder = CsvShaper::Encoder.new(csv.header, [])
47
+ expect(encoder.to_csv).to eq("Full name,Sex,Age\n")
48
+ end
43
49
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csv_shaper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Springett
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-02 00:00:00.000000000 Z
11
+ date: 2020-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -66,6 +66,7 @@ files:
66
66
  - ".groc.json"
67
67
  - ".rspec"
68
68
  - ".ruby-version"
69
+ - ".tool-versions"
69
70
  - ".travis.yml"
70
71
  - Gemfile
71
72
  - LICENSE
@@ -79,9 +80,11 @@ files:
79
80
  - lib/csv_shaper/row.rb
80
81
  - lib/csv_shaper/shaper.rb
81
82
  - lib/csv_shaper/version.rb
83
+ - lib/csv_shaper_handler.rb
82
84
  - lib/csv_shaper_template.rb
83
85
  - spec/config_spec.rb
84
86
  - spec/csv_shaper_spec.rb
87
+ - spec/csv_shaper_template_spec.rb
85
88
  - spec/encoder_spec.rb
86
89
  - spec/fixtures/user.rb
87
90
  - spec/header_spec.rb
@@ -106,14 +109,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
109
  - !ruby/object:Gem::Version
107
110
  version: '0'
108
111
  requirements: []
109
- rubyforge_project:
110
- rubygems_version: 2.2.2
112
+ rubygems_version: 3.1.2
111
113
  signing_key:
112
114
  specification_version: 4
113
115
  summary: Beautiful DSL for creating CSV output in Ruby & Rails
114
116
  test_files:
115
117
  - spec/config_spec.rb
116
118
  - spec/csv_shaper_spec.rb
119
+ - spec/csv_shaper_template_spec.rb
117
120
  - spec/encoder_spec.rb
118
121
  - spec/fixtures/user.rb
119
122
  - spec/header_spec.rb