csv_shaper 1.2.0 → 1.3.0
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.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/.travis.yml +1 -2
- data/README.md +7 -1
- data/lib/csv_shaper.rb +1 -1
- data/lib/csv_shaper/version.rb +1 -1
- data/lib/csv_shaper_handler.rb +27 -0
- data/lib/csv_shaper_template.rb +2 -24
- data/spec/csv_shaper_template_spec.rb +42 -0
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 08c09e47a1ca21b104be8218ff8d7fa7eca5b034
|
4
|
+
data.tar.gz: 20ddc9b2f52d0e23743fb63814d4ac0afe060d53
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e1280a83e280ad4184a4336babdb2ac50c89d8feb68ccf8625e36624f13964ed3bffc61255d33aabe91f4d7ec3a9d1ba418661d677f8cf84b4af7c32cf7e7c38
|
7
|
+
data.tar.gz: cdb6b968d4d3482f0c982c84a8ca1f5e48af6f5a7964791b31cfac8f4ee6ca9f2dabf49018873efa049bb505cca95a5b893e73181d98f1a8435c892c0985d4f9
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.1
|
1
|
+
2.3.1
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -27,7 +27,7 @@ end
|
|
27
27
|
|
28
28
|
### Install
|
29
29
|
|
30
|
-
**Requires Ruby
|
30
|
+
**Requires Ruby 2.0+**
|
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
|
data/lib/csv_shaper.rb
CHANGED
data/lib/csv_shaper/version.rb
CHANGED
@@ -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)
|
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
|
+
#{template.source}
|
22
|
+
end
|
23
|
+
}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
ActionView::Template.register_template_handler :shaper, CsvShaperHandler
|
data/lib/csv_shaper_template.rb
CHANGED
@@ -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
|
-
|
23
|
-
|
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
|
@@ -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
|
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.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Springett
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-08-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -79,9 +79,11 @@ files:
|
|
79
79
|
- lib/csv_shaper/row.rb
|
80
80
|
- lib/csv_shaper/shaper.rb
|
81
81
|
- lib/csv_shaper/version.rb
|
82
|
+
- lib/csv_shaper_handler.rb
|
82
83
|
- lib/csv_shaper_template.rb
|
83
84
|
- spec/config_spec.rb
|
84
85
|
- spec/csv_shaper_spec.rb
|
86
|
+
- spec/csv_shaper_template_spec.rb
|
85
87
|
- spec/encoder_spec.rb
|
86
88
|
- spec/fixtures/user.rb
|
87
89
|
- spec/header_spec.rb
|
@@ -107,13 +109,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
109
|
version: '0'
|
108
110
|
requirements: []
|
109
111
|
rubyforge_project:
|
110
|
-
rubygems_version: 2.
|
112
|
+
rubygems_version: 2.5.1
|
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
|