csv_shaper 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.md +45 -8
- data/lib/csv_shaper/version.rb +1 -1
- data/lib/csv_shaper_template.rb +1 -1
- metadata +24 -10
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Beautiful DSL for creating CSV output in Ruby & Rails.
|
4
4
|
|
5
|
-
Creating CSV files in Ruby is painful! CSV Shaper makes life easier! It's ideal for converting database backed models with
|
5
|
+
Creating CSV files in Ruby is painful! CSV Shaper makes life easier! It's ideal for converting database backed models with attributes into CSV output. It can be used without Rails, but works great with ActiveRecord models and even comes with support for its own template handling.
|
6
6
|
|
7
7
|
[![Build Status](https://secure.travis-ci.org/paulspringett/csv_shaper.png?branch=master)](http://travis-ci.org/paulspringett/csv_shaper)
|
8
8
|
|
@@ -12,7 +12,7 @@ Annotated source: http://paulspringett.github.com/csv_shaper/
|
|
12
12
|
|
13
13
|
```ruby
|
14
14
|
csv_string = CsvShaper::Shaper.encode do |csv|
|
15
|
-
csv.
|
15
|
+
csv.headers :name, :age, :gender, :pet_names
|
16
16
|
|
17
17
|
csv.rows @users do |csv, user|
|
18
18
|
csv.cells :name, :age, :gender
|
@@ -56,9 +56,23 @@ end
|
|
56
56
|
|
57
57
|
### Usage in Rails 3.0+
|
58
58
|
|
59
|
-
|
59
|
+
When using it in Rails your view template is rendered inside the `encode` block so you can just call the `csv` object directly.
|
60
60
|
|
61
|
-
|
61
|
+
In Rails the example at the top of the README would simply be:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
csv.headers :name, :age, :gender, :pet_names
|
65
|
+
|
66
|
+
csv.rows @users do |csv, user|
|
67
|
+
csv.cells :name, :age, :gender
|
68
|
+
|
69
|
+
if user.pets.any?
|
70
|
+
csv.cell :pet_names
|
71
|
+
end
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
75
|
+
Create a Rails view, set the content-type to `csv` and the handler to `shaper`. For the view of the `index` action the filename would be:
|
62
76
|
|
63
77
|
index.csv.shaper
|
64
78
|
|
@@ -103,6 +117,8 @@ This would create headers like so:
|
|
103
117
|
Full name,Age,Region
|
104
118
|
```
|
105
119
|
|
120
|
+
The mappings are useful for pretty-ing up the names when creating the CSV. When creating cells below you should still use the column names, not the mapping names. eg. `:name` not `'Full name'`
|
121
|
+
|
106
122
|
### Rows & Cells
|
107
123
|
|
108
124
|
CSV Shaper allows you to define rows and cells in a variety of ways.
|
@@ -122,22 +138,34 @@ end
|
|
122
138
|
csv.row @user, :name, :age, :location
|
123
139
|
```
|
124
140
|
|
125
|
-
This will call the column names (name, age...) on
|
141
|
+
This will call the column names (name, age...) on `@user` and assign them to the correct cells. The output from the above Ruby might look like:
|
142
|
+
|
143
|
+
```
|
144
|
+
Paul,27,United Kingdom
|
145
|
+
```
|
126
146
|
|
127
147
|
#### Passing a model to a block
|
128
148
|
|
129
149
|
```ruby
|
130
|
-
csv.row @user
|
150
|
+
csv.row @user do |csv, user|
|
131
151
|
csv.cells :name, :age
|
132
152
|
if user.show_gender?
|
133
153
|
csv.cell :gender
|
134
154
|
end
|
135
155
|
|
136
|
-
csv.cell :exported_at,
|
156
|
+
csv.cell :exported_at, Date.today.to_formatted_s(:db)
|
137
157
|
end
|
138
158
|
```
|
139
159
|
|
140
|
-
Any calls here to `cell`
|
160
|
+
Any calls here to `cell` without a second argument are called on the model (`user`), otherwise the second parameter is used as a static value.
|
161
|
+
|
162
|
+
The `cells` method only takes a list of Symbols that are called as methods on the model (`user`).
|
163
|
+
|
164
|
+
The output from the above Ruby might look like:
|
165
|
+
|
166
|
+
```
|
167
|
+
Paul,27,Male,2012-07-25
|
168
|
+
```
|
141
169
|
|
142
170
|
### Multiple Rows
|
143
171
|
|
@@ -191,6 +219,15 @@ def index
|
|
191
219
|
end
|
192
220
|
```
|
193
221
|
|
222
|
+
### Contributing
|
223
|
+
|
224
|
+
1. Fork it
|
225
|
+
2. Create a semantically named feature branch
|
226
|
+
3. Write your feature
|
227
|
+
4. Add some tests for it
|
228
|
+
5. Commit your changes & push to GitHub (do not change the gem's version number)
|
229
|
+
6. Submit a pull request with relevant details
|
230
|
+
|
194
231
|
##### Hat tips
|
195
232
|
|
196
233
|
* [Jbuilder](https://github.com/rails/jbuilder/) for inspiration for the DSL
|
data/lib/csv_shaper/version.rb
CHANGED
data/lib/csv_shaper_template.rb
CHANGED
@@ -27,7 +27,7 @@ class CsvShaperHandler
|
|
27
27
|
unless defined?(ActionMailer) && defined?(ActionMailer::Base) && controller.is_a?(ActionMailer::Base)
|
28
28
|
@filename ||= "\#{controller.action_name}.csv"
|
29
29
|
controller.response.headers["Content-Type"] ||= 'text/csv'
|
30
|
-
controller.response.headers['Content-Disposition'] = "attachment; filename
|
30
|
+
controller.response.headers['Content-Disposition'] = "attachment; filename=\\\"\#{@filename}\\\""
|
31
31
|
end
|
32
32
|
|
33
33
|
CsvShaperTemplate.encode(self) do |csv|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv_shaper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-08-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,15 @@ dependencies:
|
|
21
21
|
version: 3.0.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: rspec
|
27
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
28
33
|
none: false
|
29
34
|
requirements:
|
30
35
|
- - ! '>='
|
@@ -32,10 +37,15 @@ dependencies:
|
|
32
37
|
version: '0'
|
33
38
|
type: :development
|
34
39
|
prerelease: false
|
35
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
36
46
|
- !ruby/object:Gem::Dependency
|
37
47
|
name: rake
|
38
|
-
requirement:
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
39
49
|
none: false
|
40
50
|
requirements:
|
41
51
|
- - ! '>='
|
@@ -43,7 +53,12 @@ dependencies:
|
|
43
53
|
version: '0'
|
44
54
|
type: :development
|
45
55
|
prerelease: false
|
46
|
-
version_requirements:
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
47
62
|
description: ! "\n Creating CSV files in Ruby is painful! CSV Shaper makes life
|
48
63
|
easier! It's\n ideal for converting database backed models with attrbiutes into
|
49
64
|
CSV output.\n It can be used without Rails, but works great with ActiveRecord
|
@@ -95,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
95
110
|
version: '0'
|
96
111
|
requirements: []
|
97
112
|
rubyforge_project:
|
98
|
-
rubygems_version: 1.8.
|
113
|
+
rubygems_version: 1.8.23
|
99
114
|
signing_key:
|
100
115
|
specification_version: 3
|
101
116
|
summary: Beautiful DSL for creating CSV output in Ruby & Rails
|
@@ -106,4 +121,3 @@ test_files:
|
|
106
121
|
- spec/header_spec.rb
|
107
122
|
- spec/row_spec.rb
|
108
123
|
- spec/spec_helper.rb
|
109
|
-
has_rdoc:
|