csv_shaper 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +2 -0
- data/README.md +27 -6
- data/csv_shaper.gemspec +3 -1
- data/lib/csv_shaper/config.rb +7 -1
- data/lib/csv_shaper/encoder.rb +11 -7
- data/lib/csv_shaper/header.rb +23 -10
- data/lib/csv_shaper/shaper.rb +10 -10
- data/lib/csv_shaper/version.rb +1 -1
- data/spec/config_spec.rb +29 -12
- data/spec/csv_shaper_spec.rb +6 -6
- data/spec/encoder_spec.rb +5 -5
- data/spec/header_spec.rb +30 -14
- data/spec/row_spec.rb +16 -16
- data/spec/spec_helper.rb +0 -5
- metadata +39 -30
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ebfdda4a218f9cf86f42c9158f3ea0baa77de655
|
4
|
+
data.tar.gz: 7cc018cfafa1d59ae8589f087f71e9556c960e8f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 644876cb037fa67e9f7bb79bd84880e74aac4b1b75143427f687db9ce794a5c75ae2f7b7d4323631792fb07618c579294ff42e2538af96fb5cd88994fa62040b
|
7
|
+
data.tar.gz: e71f61f64dedb5785e01ac9037d75f9e093ffed3b36238006da5f9d2a0c5466a8d9fc35918615284b53701b38537d5c1ab5b67489aafc7880c0475687f91f2a6
|
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.1.4
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -5,6 +5,7 @@ Beautiful DSL for creating CSV output in Ruby & Rails.
|
|
5
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
|
+
[![Code Climate](https://codeclimate.com/github/paulspringett/csv_shaper.png)](https://codeclimate.com/github/paulspringett/csv_shaper)
|
8
9
|
|
9
10
|
Annotated source: http://paulspringett.github.com/csv_shaper/
|
10
11
|
|
@@ -13,10 +14,10 @@ Annotated source: http://paulspringett.github.com/csv_shaper/
|
|
13
14
|
```ruby
|
14
15
|
csv_string = CsvShaper.encode do |csv|
|
15
16
|
csv.headers :name, :age, :gender, :pet_names
|
16
|
-
|
17
|
+
|
17
18
|
csv.rows @users do |csv, user|
|
18
19
|
csv.cells :name, :age, :gender
|
19
|
-
|
20
|
+
|
20
21
|
if user.pets.any?
|
21
22
|
csv.cell :pet_names
|
22
23
|
end
|
@@ -26,6 +27,8 @@ end
|
|
26
27
|
|
27
28
|
### Install
|
28
29
|
|
30
|
+
**Requires Ruby 1.9+**
|
31
|
+
|
29
32
|
Install using Rubygems
|
30
33
|
|
31
34
|
```bash
|
@@ -65,7 +68,7 @@ csv.headers :name, :age, :gender, :pet_names
|
|
65
68
|
|
66
69
|
csv.rows @users do |csv, user|
|
67
70
|
csv.cells :name, :age, :gender
|
68
|
-
|
71
|
+
|
69
72
|
if user.pets.any?
|
70
73
|
csv.cell :pet_names
|
71
74
|
end
|
@@ -75,7 +78,7 @@ end
|
|
75
78
|
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:
|
76
79
|
|
77
80
|
index.csv.shaper
|
78
|
-
|
81
|
+
|
79
82
|
then just start defining your headers and rows as per the examples.
|
80
83
|
|
81
84
|
### Headers
|
@@ -119,6 +122,24 @@ Full name,Age,Region
|
|
119
122
|
|
120
123
|
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
124
|
|
125
|
+
#### Specify the header inflector method
|
126
|
+
|
127
|
+
Sometimes you may wish to control how headers are transformed from the symbol form. The default `inflector` is set to `:humanize`.
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
csv.headers do |csv|
|
131
|
+
csv.columns :full_name, :age, :full_address
|
132
|
+
csv.inflector :titleize
|
133
|
+
end
|
134
|
+
```
|
135
|
+
|
136
|
+
This would create headers like so:
|
137
|
+
|
138
|
+
```csv
|
139
|
+
Full Name,Age,Full Address
|
140
|
+
```
|
141
|
+
|
142
|
+
|
122
143
|
### Rows & Cells
|
123
144
|
|
124
145
|
CSV Shaper allows you to define rows and cells in a variety of ways.
|
@@ -162,7 +183,7 @@ Any calls here to `cell` without a second argument are called on the model (`use
|
|
162
183
|
The `cells` method only takes a list of Symbols that are called as methods on the model (`user`).
|
163
184
|
|
164
185
|
The output from the above Ruby might look like:
|
165
|
-
|
186
|
+
|
166
187
|
```
|
167
188
|
Paul,27,Male,2012-07-25
|
168
189
|
```
|
@@ -257,4 +278,4 @@ end
|
|
257
278
|
* [Jbuilder](https://github.com/rails/jbuilder/) for inspiration for the DSL
|
258
279
|
* [CSV Builder](https://github.com/econsultancy/csv_builder) for headers and custom filenames
|
259
280
|
|
260
|
-
Copyright (c) Paul Springett 2012
|
281
|
+
Copyright (c) Paul Springett 2012
|
data/csv_shaper.gemspec
CHANGED
@@ -8,11 +8,13 @@ Gem::Specification.new do |gem|
|
|
8
8
|
gem.summary = %q{Beautiful DSL for creating CSV output in Ruby & Rails}
|
9
9
|
gem.description = %q{
|
10
10
|
Creating CSV files in Ruby is painful! CSV Shaper makes life easier! It's
|
11
|
-
ideal for converting database backed models with
|
11
|
+
ideal for converting database backed models with attributes into CSV output.
|
12
12
|
It can be used without Rails, but works great with ActiveRecord models and even
|
13
13
|
comes with support for it's own template handling.
|
14
14
|
}
|
15
15
|
|
16
|
+
gem.licenses = ['MIT']
|
17
|
+
|
16
18
|
gem.homepage = "http://github.com/paulspringett/csv_shaper"
|
17
19
|
|
18
20
|
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
data/lib/csv_shaper/config.rb
CHANGED
@@ -4,11 +4,15 @@ module CsvShaper
|
|
4
4
|
# Config
|
5
5
|
# Configure the standard CSV default options
|
6
6
|
# as well the option to output the header row
|
7
|
+
|
7
8
|
class Config
|
9
|
+
CUSTOM_DEFAULT_OPTIONS = { header_inflector: :humanize }
|
10
|
+
|
8
11
|
attr_reader :options
|
9
12
|
|
10
13
|
def initialize
|
11
14
|
@options = {}
|
15
|
+
@options.merge!(CUSTOM_DEFAULT_OPTIONS)
|
12
16
|
yield self if block_given?
|
13
17
|
end
|
14
18
|
|
@@ -47,7 +51,9 @@ module CsvShaper
|
|
47
51
|
#
|
48
52
|
# Returns a Hash
|
49
53
|
def defaults
|
50
|
-
@defaults ||= CSV::DEFAULT_OPTIONS.dup.
|
54
|
+
@defaults ||= CSV::DEFAULT_OPTIONS.dup.
|
55
|
+
merge(write_headers: true).
|
56
|
+
merge(CUSTOM_DEFAULT_OPTIONS)
|
51
57
|
end
|
52
58
|
end
|
53
59
|
end
|
data/lib/csv_shaper/encoder.rb
CHANGED
@@ -12,36 +12,40 @@ module CsvShaper
|
|
12
12
|
if header.nil?
|
13
13
|
raise MissingHeadersError, 'you must define some headers using csv.headers ...'
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
@header = header
|
17
17
|
@rows = rows
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
# Public: converts the Shaper mapped headers and rows into
|
21
21
|
# a CSV String
|
22
22
|
#
|
23
23
|
# Returns a String
|
24
24
|
def to_csv(local_config = nil)
|
25
25
|
csv_options = options.merge(local_options(local_config))
|
26
|
-
|
27
26
|
rows = padded_rows.map do |data|
|
28
27
|
CSV::Row.new(@header.mapped_columns, data, false)
|
29
28
|
end
|
30
|
-
|
29
|
+
|
31
30
|
table = CSV::Table.new(rows)
|
31
|
+
csv_options.except!(*custom_options.keys)
|
32
32
|
table.to_csv(csv_options)
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
private
|
36
36
|
|
37
37
|
def options
|
38
38
|
CsvShaper::Shaper.config && CsvShaper::Shaper.config.options || {}
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
def local_options(local_config)
|
42
42
|
local_config && local_config.options || {}
|
43
43
|
end
|
44
44
|
|
45
|
+
def custom_options
|
46
|
+
CsvShaper::Config::CUSTOM_DEFAULT_OPTIONS
|
47
|
+
end
|
48
|
+
|
45
49
|
# Internal: make use of `CSV#values_at` to pad out the
|
46
50
|
# cells into the correct columns for the headers
|
47
51
|
#
|
@@ -50,7 +54,7 @@ module CsvShaper
|
|
50
54
|
rows = @rows.map do |row|
|
51
55
|
CSV::Row.new(row.cells.keys, row.cells.values)
|
52
56
|
end
|
53
|
-
|
57
|
+
|
54
58
|
table = CSV::Table.new(rows)
|
55
59
|
table.values_at(*@header.columns)
|
56
60
|
end
|
data/lib/csv_shaper/header.rb
CHANGED
@@ -2,13 +2,13 @@ module CsvShaper
|
|
2
2
|
# Header
|
3
3
|
# Handles creating and mapping of the headers
|
4
4
|
# Examples:
|
5
|
-
# ```
|
5
|
+
# ```
|
6
6
|
# # assign the headers from the attributes of a class
|
7
7
|
# csv.headers User
|
8
|
-
#
|
8
|
+
#
|
9
9
|
# # assigns headers normally
|
10
10
|
# csv.headers :name, :age, :location
|
11
|
-
#
|
11
|
+
#
|
12
12
|
# # pass a block
|
13
13
|
# csv.headers do |csv|
|
14
14
|
# csv.columns :name, :age, :location
|
@@ -16,11 +16,12 @@ module CsvShaper
|
|
16
16
|
# end
|
17
17
|
# ```
|
18
18
|
class Header
|
19
|
-
attr_reader :klass, :mappings, :mapped_columns
|
19
|
+
attr_reader :klass, :mappings, :mapped_columns, :inflector
|
20
20
|
|
21
21
|
def initialize(*args)
|
22
22
|
@mappings = {}
|
23
23
|
@columns = []
|
24
|
+
@inflector = CsvShaper::Shaper.config.options[:header_inflector]
|
24
25
|
|
25
26
|
if block_given?
|
26
27
|
yield self
|
@@ -32,7 +33,7 @@ module CsvShaper
|
|
32
33
|
end
|
33
34
|
end
|
34
35
|
end
|
35
|
-
|
36
|
+
|
36
37
|
# Public: serves as the getter and setter for the Array
|
37
38
|
# of Symbol column names. Union join the existing column
|
38
39
|
# names with those passed
|
@@ -46,7 +47,7 @@ module CsvShaper
|
|
46
47
|
def columns(*args)
|
47
48
|
@columns = @columns | args.map(&:to_sym)
|
48
49
|
end
|
49
|
-
|
50
|
+
|
50
51
|
# Public: Define mappings of the Symbol column names
|
51
52
|
# to nicer, human names
|
52
53
|
# Example:
|
@@ -57,19 +58,31 @@ module CsvShaper
|
|
57
58
|
# and the value is the human readable value
|
58
59
|
#
|
59
60
|
# Returns a Hash of mappings
|
60
|
-
def mappings(hash = {})
|
61
|
+
def mappings(hash = {})
|
61
62
|
@mappings.merge!(hash)
|
62
63
|
end
|
63
|
-
|
64
|
+
|
65
|
+
# Public: Convert column names, default inflector is :humanize
|
66
|
+
# Example:
|
67
|
+
# ```
|
68
|
+
# header.inflector :titleize
|
69
|
+
# ```
|
70
|
+
# `:titleize` - one of ruby inflectors
|
71
|
+
def inflector(header_inflector)
|
72
|
+
@inflector = header_inflector
|
73
|
+
end
|
74
|
+
|
64
75
|
# Public: converts columns and mappings into mapped columns
|
65
76
|
# ready for encoding. If a mapped value is found that is used,
|
66
|
-
# else the Symbol column name is humanized
|
77
|
+
# else the Symbol column name is humanized by default or can be specified
|
78
|
+
# with header_inflector option
|
67
79
|
#
|
68
80
|
# Returns an Array of Strings
|
69
81
|
def mapped_columns
|
70
82
|
@columns.map do |column|
|
71
|
-
@mappings[column] || column.to_s.
|
83
|
+
@mappings[column] || column.to_s.send(@inflector)
|
72
84
|
end
|
73
85
|
end
|
86
|
+
|
74
87
|
end
|
75
88
|
end
|
data/lib/csv_shaper/shaper.rb
CHANGED
@@ -3,17 +3,17 @@ module CsvShaper
|
|
3
3
|
# Core CsvShaper class. Delegates header and row generating.
|
4
4
|
class Shaper
|
5
5
|
attr_reader :header, :rows
|
6
|
-
|
6
|
+
|
7
7
|
class << self
|
8
8
|
attr_accessor :config
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
def initialize(options = {})
|
12
12
|
@rows = []
|
13
13
|
local_configuration(options)
|
14
14
|
yield self if block_given?
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
# Public: creates a new instance of Shaper taps it with
|
18
18
|
# with the given block and encodes it to a String of CSV data
|
19
19
|
# Example:
|
@@ -25,14 +25,14 @@ module CsvShaper
|
|
25
25
|
# end
|
26
26
|
#
|
27
27
|
# puts data
|
28
|
-
# => "Name,Age,Gender\n'Joe Bloggs',25,'M'\n'John Smith',34,'M'"
|
28
|
+
# => "Name,Age,Gender\n'Joe Bloggs',25,'M'\n'John Smith',34,'M'"
|
29
29
|
# ```
|
30
30
|
#
|
31
31
|
# Returns a String
|
32
32
|
def self.encode(options = {})
|
33
33
|
new(options).tap { |shaper| yield shaper }.to_csv
|
34
34
|
end
|
35
|
-
|
35
|
+
|
36
36
|
# Public: creates a header row for the CSV
|
37
37
|
# This is delegated to the Header class
|
38
38
|
# see header.rb for usage examples
|
@@ -41,7 +41,7 @@ module CsvShaper
|
|
41
41
|
def headers(*args, &block)
|
42
42
|
@header = Header.new(*args, &block)
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
# Public: adds a row to the CSV
|
46
46
|
# This is delegated to the Row class
|
47
47
|
# See row.rb for usage examples
|
@@ -50,7 +50,7 @@ module CsvShaper
|
|
50
50
|
def row(*args, &block)
|
51
51
|
@rows.push Row.new(*args, &block)
|
52
52
|
end
|
53
|
-
|
53
|
+
|
54
54
|
# Public: adds several rows to the CSV
|
55
55
|
#
|
56
56
|
# `collection` - an Enumerable of objects to be passed to #row
|
@@ -58,7 +58,7 @@ module CsvShaper
|
|
58
58
|
# Returns an updated Array of Row objects
|
59
59
|
def rows(collection = nil, &block)
|
60
60
|
return @rows if collection.nil?
|
61
|
-
|
61
|
+
|
62
62
|
unless collection.respond_to?(:each)
|
63
63
|
raise ArgumentError, 'csv.rows only accepts Enumerable object (that respond to #each). Use csv.row for a single object.'
|
64
64
|
end
|
@@ -83,9 +83,9 @@ module CsvShaper
|
|
83
83
|
def self.configure(&block)
|
84
84
|
@config ||= CsvShaper::Config.new(&block)
|
85
85
|
end
|
86
|
-
|
86
|
+
|
87
87
|
private
|
88
|
-
|
88
|
+
|
89
89
|
def local_configuration(options = {})
|
90
90
|
@local_config = CsvShaper::Config.new do |csv|
|
91
91
|
options.each_pair { |k, v| csv.send("#{k}=", v) }
|
data/lib/csv_shaper/version.rb
CHANGED
data/spec/config_spec.rb
CHANGED
@@ -5,42 +5,59 @@ describe CsvShaper::Config do
|
|
5
5
|
CsvShaper::Config.new do |c|
|
6
6
|
c.write_headers = false
|
7
7
|
c.col_sep = "\t"
|
8
|
+
c.header_inflector = :titleize
|
8
9
|
end
|
9
10
|
}
|
10
|
-
|
11
|
+
|
11
12
|
it "should assign options to config" do
|
12
|
-
config.options.
|
13
|
+
expect(config.options).to eq({ write_headers: false, col_sep: "\t", header_inflector: :titleize })
|
13
14
|
end
|
14
|
-
|
15
|
+
|
15
16
|
it "should exclude the headers if specified" do
|
16
17
|
CsvShaper::Shaper.config = config
|
17
|
-
|
18
|
+
|
18
19
|
shaper = CsvShaper::Shaper.new do |csv|
|
19
20
|
csv.headers :name, :age, :gender
|
20
|
-
|
21
|
+
|
21
22
|
csv.row do |csv|
|
22
23
|
csv.cell :name, 'Paul'
|
23
24
|
csv.cell :age, '27'
|
24
25
|
csv.cell :gender, 'Male'
|
25
26
|
end
|
26
27
|
end
|
27
|
-
|
28
|
-
shaper.to_csv.
|
28
|
+
|
29
|
+
expect(shaper.to_csv).to eq "Paul\t27\tMale\n"
|
29
30
|
end
|
30
|
-
|
31
|
+
|
31
32
|
it "should allow change configuration locally" do
|
32
33
|
CsvShaper::Shaper.config = config
|
33
|
-
|
34
|
+
|
34
35
|
shaper = CsvShaper::Shaper.new(col_sep: ",") do |csv|
|
35
36
|
csv.headers :name, :age, :gender
|
36
|
-
|
37
|
+
|
37
38
|
csv.row do |csv|
|
38
39
|
csv.cell :name, 'Paul'
|
39
40
|
csv.cell :age, '27'
|
40
41
|
csv.cell :gender, 'Male'
|
41
42
|
end
|
42
43
|
end
|
43
|
-
|
44
|
-
shaper.to_csv.
|
44
|
+
|
45
|
+
expect(shaper.to_csv).to eq "Paul,27,Male\n"
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should allow change inflector locally" do
|
49
|
+
CsvShaper::Shaper.config = config
|
50
|
+
|
51
|
+
shaper = CsvShaper::Shaper.new(col_sep: ",", write_headers: true, header_inflector: :titleize) do |csv|
|
52
|
+
csv.headers :full_name, :age, :gender
|
53
|
+
|
54
|
+
csv.row do |csv|
|
55
|
+
csv.cell :full_name, 'Paul'
|
56
|
+
csv.cell :age, '27'
|
57
|
+
csv.cell :gender, 'Male'
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
expect(shaper.to_csv).to eq "Full Name,Age,Gender\nPaul,27,Male\n"
|
45
62
|
end
|
46
63
|
end
|
data/spec/csv_shaper_spec.rb
CHANGED
@@ -2,27 +2,27 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe CsvShaper::Shaper do
|
4
4
|
it "should return a version" do
|
5
|
-
CsvShaper::VERSION.
|
5
|
+
expect(CsvShaper::VERSION).to be_kind_of(String)
|
6
6
|
end
|
7
|
-
|
7
|
+
|
8
8
|
it "should raise an exception when passing a non-enumarble to rows" do
|
9
9
|
csv = CsvShaper::Shaper.new
|
10
10
|
expect {
|
11
11
|
csv.rows :name
|
12
12
|
}.to raise_exception(ArgumentError, 'csv.rows only accepts Enumerable object (that respond to #each). Use csv.row for a single object.')
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
it "should respond to to_csv" do
|
16
16
|
csv = CsvShaper::Shaper.new
|
17
|
-
csv.
|
17
|
+
expect(csv).to respond_to(:to_csv)
|
18
18
|
end
|
19
19
|
|
20
20
|
it "should provide a shortcut to the encode method" do
|
21
|
-
CsvShaper.
|
21
|
+
expect(CsvShaper).to respond_to(:encode)
|
22
22
|
|
23
23
|
CsvShaper.encode do |csv|
|
24
24
|
csv.headers :foo
|
25
|
-
csv.
|
25
|
+
expect(csv).to be_instance_of(CsvShaper::Shaper)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
data/spec/encoder_spec.rb
CHANGED
@@ -3,19 +3,19 @@ require 'fixtures/user'
|
|
3
3
|
|
4
4
|
describe CsvShaper::Encoder do
|
5
5
|
let(:user) { User.new(name: 'Paul', age: 27, gender: 'Male') }
|
6
|
-
|
6
|
+
|
7
7
|
let(:config) {
|
8
8
|
CsvShaper::Config.new do |c|
|
9
9
|
c.write_headers = true
|
10
10
|
end
|
11
11
|
}
|
12
|
-
|
12
|
+
|
13
13
|
it "should raise an exception if the headers are missing" do
|
14
14
|
expect {
|
15
15
|
CsvShaper::Encoder.new(nil)
|
16
16
|
}.to raise_exception(CsvShaper::MissingHeadersError, 'you must define some headers using csv.headers ...')
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
let(:users) { [User.new(name: 'Paul', age: 27, gender: 'Male'), User.new(name: 'Bob', age: 31, gender: 'Male'), User.new(name: 'Jane', age: 23, gender: 'Female')] }
|
20
20
|
let(:csv) {
|
21
21
|
CsvShaper::Shaper.new do |csv|
|
@@ -34,10 +34,10 @@ describe CsvShaper::Encoder do
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
}
|
37
|
-
|
37
|
+
|
38
38
|
it "should encode a Shaper instance to a CSV string" do
|
39
39
|
CsvShaper::Shaper.config = config
|
40
40
|
encoder = CsvShaper::Encoder.new(csv.header, csv.rows)
|
41
|
-
encoder.to_csv.
|
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
43
|
end
|
data/spec/header_spec.rb
CHANGED
@@ -2,35 +2,51 @@ require 'spec_helper'
|
|
2
2
|
require 'fixtures/user'
|
3
3
|
|
4
4
|
describe CsvShaper::Header do
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
config = double(:config, options: { header_inflector: :humanize })
|
8
|
+
allow(CsvShaper::Shaper).to receive(:config) { config }
|
9
|
+
end
|
10
|
+
|
5
11
|
it "should accept and store a list of symbols" do
|
6
12
|
header = CsvShaper::Header.new(:name, :age, :location)
|
7
|
-
|
8
|
-
header.columns.
|
13
|
+
|
14
|
+
expect(header.columns).to eq [:name, :age, :location]
|
9
15
|
end
|
10
|
-
|
16
|
+
|
11
17
|
it "should accept a Model and store it's attributes" do
|
12
18
|
header = CsvShaper::Header.new(User)
|
13
|
-
|
14
|
-
header.columns.
|
19
|
+
|
20
|
+
expect(header.columns).to eq [:name, :age, :gender]
|
15
21
|
end
|
16
|
-
|
22
|
+
|
17
23
|
it "should accept a block with columns and mappings" do
|
18
24
|
header = CsvShaper::Header.new do |csv|
|
19
25
|
csv.columns :name, :age, :foo
|
20
26
|
csv.mappings name: 'User name'
|
21
27
|
end
|
22
|
-
|
23
|
-
header.columns.
|
24
|
-
header.mapped_columns.
|
28
|
+
|
29
|
+
expect(header.columns).to eq [:name, :age, :foo]
|
30
|
+
expect(header.mapped_columns).to eq ['User name', 'Age', 'Foo']
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should titleize column names according to the chosen inflector" do
|
34
|
+
header = CsvShaper::Header.new do |csv|
|
35
|
+
csv.columns :first_name, :full_age
|
36
|
+
csv.inflector :titleize
|
37
|
+
end
|
38
|
+
|
39
|
+
expect(header.columns).to eq [:first_name, :full_age]
|
40
|
+
expect(header.mapped_columns).to eq ['First Name', 'Full Age']
|
25
41
|
end
|
26
|
-
|
42
|
+
|
27
43
|
it "should merge columns with a union join" do
|
28
44
|
header = CsvShaper::Header.new(:name, :age, :location)
|
29
45
|
header.columns :age, :gender
|
30
|
-
|
31
|
-
header.columns.
|
46
|
+
|
47
|
+
expect(header.columns).to eq [:name, :age, :location, :gender]
|
32
48
|
end
|
33
|
-
|
49
|
+
|
34
50
|
it "should merge mappings" do
|
35
51
|
header = CsvShaper::Header.new do |csv|
|
36
52
|
csv.columns :name, :age, :foo
|
@@ -38,6 +54,6 @@ describe CsvShaper::Header do
|
|
38
54
|
csv.mappings foo: 'Bar'
|
39
55
|
end
|
40
56
|
|
41
|
-
header.mapped_columns.
|
57
|
+
expect(header.mapped_columns).to eq ['User name', 'Age', 'Bar']
|
42
58
|
end
|
43
59
|
end
|
data/spec/row_spec.rb
CHANGED
@@ -3,52 +3,52 @@ require 'fixtures/user'
|
|
3
3
|
|
4
4
|
describe CsvShaper::Row do
|
5
5
|
let(:user) { User.new(name: 'Paul', age: 27, gender: 'Male') }
|
6
|
-
|
6
|
+
|
7
7
|
it "should assign a model" do
|
8
8
|
row = CsvShaper::Row.new user do |csv, user|
|
9
9
|
# ...
|
10
10
|
end
|
11
11
|
|
12
|
-
row.model.
|
12
|
+
expect(row.model).to eq user
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
it "should assign a model's attributes" do
|
16
16
|
row = CsvShaper::Row.new(user, :name, :age)
|
17
|
-
row.cells.
|
17
|
+
expect(row.cells).to eq({ name: 'Paul', age: 27 })
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
it "should yield to a block" do
|
21
21
|
CsvShaper::Row.new { |csv|
|
22
|
-
csv.
|
22
|
+
expect(csv).to be_kind_of(CsvShaper::Row)
|
23
23
|
}
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
describe "cells" do
|
27
27
|
it "should send parse an attribute of the model" do
|
28
28
|
row = CsvShaper::Row.new(user, :gender)
|
29
29
|
row.cell :name
|
30
|
-
row.cells.
|
30
|
+
expect(row.cells).to eq({ name: 'Paul', gender: 'Male' })
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
33
|
it "should send assign an unrelated value" do
|
34
34
|
row = CsvShaper::Row.new(user, :gender)
|
35
35
|
row.cell :foo, 'bar'
|
36
|
-
row.cells.
|
36
|
+
expect(row.cells).to eq({ foo: 'bar', gender: 'Male' })
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
it "ignore nil values passed" do
|
40
40
|
row = CsvShaper::Row.new(user, :gender)
|
41
41
|
row.cell :foo, nil
|
42
|
-
row.cells.
|
42
|
+
expect(row.cells).to eq({ foo: nil, gender: 'Male' })
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
it "should not send column to model if two args are passed" do
|
46
46
|
row = CsvShaper::Row.new(user, :gender)
|
47
47
|
row.cell :name, 'Another name'
|
48
|
-
row.cells.
|
48
|
+
expect(row.cells).to eq({ name: 'Another name', gender: 'Male' })
|
49
49
|
end
|
50
|
-
|
51
|
-
it "should raise an exception
|
50
|
+
|
51
|
+
it "should raise an exception if the model does not respond to column, and no value is passed" do
|
52
52
|
row = CsvShaper::Row.new(user, :gender)
|
53
53
|
expect {
|
54
54
|
row.cell :foo
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,62 +1,72 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv_shaper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
5
|
-
prerelease:
|
4
|
+
version: 1.1.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Paul Springett
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-11-02 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: activesupport
|
16
|
-
requirement:
|
17
|
-
none: false
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: 3.0.0
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
|
-
version_requirements:
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.0.0
|
25
27
|
- !ruby/object:Gem::Dependency
|
26
28
|
name: rspec
|
27
|
-
requirement:
|
28
|
-
none: false
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
29
30
|
requirements:
|
30
|
-
- -
|
31
|
+
- - ">="
|
31
32
|
- !ruby/object:Gem::Version
|
32
33
|
version: '0'
|
33
34
|
type: :development
|
34
35
|
prerelease: false
|
35
|
-
version_requirements:
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
36
41
|
- !ruby/object:Gem::Dependency
|
37
42
|
name: rake
|
38
|
-
requirement:
|
39
|
-
none: false
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
40
44
|
requirements:
|
41
|
-
- -
|
45
|
+
- - ">="
|
42
46
|
- !ruby/object:Gem::Version
|
43
47
|
version: '0'
|
44
48
|
type: :development
|
45
49
|
prerelease: false
|
46
|
-
version_requirements:
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: "\n Creating CSV files in Ruby is painful! CSV Shaper makes life easier!
|
56
|
+
It's\n ideal for converting database backed models with attributes into CSV output.\n
|
57
|
+
\ It can be used without Rails, but works great with ActiveRecord models and even\n
|
58
|
+
\ comes with support for it's own template handling.\n "
|
51
59
|
email:
|
52
60
|
- paul@springett.me
|
53
61
|
executables: []
|
54
62
|
extensions: []
|
55
63
|
extra_rdoc_files: []
|
56
64
|
files:
|
57
|
-
- .gitignore
|
58
|
-
- .groc.json
|
59
|
-
- .
|
65
|
+
- ".gitignore"
|
66
|
+
- ".groc.json"
|
67
|
+
- ".rspec"
|
68
|
+
- ".ruby-version"
|
69
|
+
- ".travis.yml"
|
60
70
|
- Gemfile
|
61
71
|
- LICENSE
|
62
72
|
- README.md
|
@@ -78,28 +88,28 @@ files:
|
|
78
88
|
- spec/row_spec.rb
|
79
89
|
- spec/spec_helper.rb
|
80
90
|
homepage: http://github.com/paulspringett/csv_shaper
|
81
|
-
licenses:
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
82
94
|
post_install_message:
|
83
95
|
rdoc_options: []
|
84
96
|
require_paths:
|
85
97
|
- lib
|
86
98
|
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
-
none: false
|
88
99
|
requirements:
|
89
|
-
- -
|
100
|
+
- - ">="
|
90
101
|
- !ruby/object:Gem::Version
|
91
102
|
version: '0'
|
92
103
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
-
none: false
|
94
104
|
requirements:
|
95
|
-
- -
|
105
|
+
- - ">="
|
96
106
|
- !ruby/object:Gem::Version
|
97
107
|
version: '0'
|
98
108
|
requirements: []
|
99
109
|
rubyforge_project:
|
100
|
-
rubygems_version:
|
110
|
+
rubygems_version: 2.2.2
|
101
111
|
signing_key:
|
102
|
-
specification_version:
|
112
|
+
specification_version: 4
|
103
113
|
summary: Beautiful DSL for creating CSV output in Ruby & Rails
|
104
114
|
test_files:
|
105
115
|
- spec/config_spec.rb
|
@@ -109,4 +119,3 @@ test_files:
|
|
109
119
|
- spec/header_spec.rb
|
110
120
|
- spec/row_spec.rb
|
111
121
|
- spec/spec_helper.rb
|
112
|
-
has_rdoc:
|