csv_shaper 0.2.0 → 0.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.
- data/README.md +9 -1
- data/lib/csv_shaper/encoder.rb +9 -3
- data/lib/csv_shaper/shaper.rb +15 -6
- data/lib/csv_shaper/version.rb +1 -1
- data/lib/csv_shaper.rb +2 -2
- data/spec/config_spec.rb +16 -0
- metadata +8 -8
data/README.md
CHANGED
@@ -12,7 +12,7 @@ Annotated source: http://paulspringett.github.com/csv_shaper/
|
|
12
12
|
|
13
13
|
```ruby
|
14
14
|
csv_string = CsvShaper.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
|
@@ -235,6 +235,14 @@ Setting this to `false` will exclude the headers from the final CSV output.
|
|
235
235
|
|
236
236
|
If you're using Rails you can put this in an initializer.
|
237
237
|
|
238
|
+
To configure CSV output locally to change global behavior you can define a configure hash, like so:
|
239
|
+
|
240
|
+
```ruby
|
241
|
+
CsvShaper.encode(col_sep: "\t") do |csv|
|
242
|
+
...
|
243
|
+
end
|
244
|
+
```
|
245
|
+
|
238
246
|
### Contributing
|
239
247
|
|
240
248
|
1. Fork it
|
data/lib/csv_shaper/encoder.rb
CHANGED
@@ -21,13 +21,15 @@ module CsvShaper
|
|
21
21
|
# a CSV String
|
22
22
|
#
|
23
23
|
# Returns a String
|
24
|
-
def to_csv
|
24
|
+
def to_csv(local_config = nil)
|
25
|
+
csv_options = options.merge(local_options(local_config))
|
26
|
+
|
25
27
|
rows = padded_rows.map do |data|
|
26
28
|
CSV::Row.new(@header.mapped_columns, data, false)
|
27
29
|
end
|
28
|
-
|
30
|
+
|
29
31
|
table = CSV::Table.new(rows)
|
30
|
-
table.to_csv(
|
32
|
+
table.to_csv(csv_options)
|
31
33
|
end
|
32
34
|
|
33
35
|
private
|
@@ -35,6 +37,10 @@ module CsvShaper
|
|
35
37
|
def options
|
36
38
|
CsvShaper::Shaper.config && CsvShaper::Shaper.config.options || {}
|
37
39
|
end
|
40
|
+
|
41
|
+
def local_options(local_config)
|
42
|
+
local_config && local_config.options || {}
|
43
|
+
end
|
38
44
|
|
39
45
|
# Internal: make use of `CSV#values_at` to pad out the
|
40
46
|
# cells into the correct columns for the headers
|
data/lib/csv_shaper/shaper.rb
CHANGED
@@ -8,8 +8,9 @@ module CsvShaper
|
|
8
8
|
attr_accessor :config
|
9
9
|
end
|
10
10
|
|
11
|
-
def initialize
|
11
|
+
def initialize(options = {})
|
12
12
|
@rows = []
|
13
|
+
local_configuration(options)
|
13
14
|
yield self if block_given?
|
14
15
|
end
|
15
16
|
|
@@ -28,8 +29,8 @@ module CsvShaper
|
|
28
29
|
# ```
|
29
30
|
#
|
30
31
|
# Returns a String
|
31
|
-
def self.encode
|
32
|
-
new.tap { |shaper| yield shaper }.to_csv
|
32
|
+
def self.encode(options = {})
|
33
|
+
new(options).tap { |shaper| yield shaper }.to_csv
|
33
34
|
end
|
34
35
|
|
35
36
|
# Public: creates a header row for the CSV
|
@@ -74,13 +75,21 @@ module CsvShaper
|
|
74
75
|
#
|
75
76
|
# Returns a String
|
76
77
|
def to_csv
|
77
|
-
Encoder.new(@header, @rows).to_csv
|
78
|
+
Encoder.new(@header, @rows).to_csv(@local_config)
|
78
79
|
end
|
79
80
|
|
80
81
|
# Public: Create an instance of the config and cache it
|
81
82
|
# for reference by the Encoder later
|
82
83
|
def self.configure(&block)
|
83
|
-
|
84
|
-
|
84
|
+
@config ||= CsvShaper::Config.new(&block)
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
|
89
|
+
def local_configuration(options = {})
|
90
|
+
@local_config = CsvShaper::Config.new do |csv|
|
91
|
+
options.each_pair { |k, v| csv.send("#{k}=", v) }
|
92
|
+
end
|
93
|
+
end
|
85
94
|
end
|
86
95
|
end
|
data/lib/csv_shaper/version.rb
CHANGED
data/lib/csv_shaper.rb
CHANGED
@@ -12,8 +12,8 @@ module CsvShaper
|
|
12
12
|
class MissingHeadersError < StandardError; end
|
13
13
|
|
14
14
|
# Shortcut the encode method
|
15
|
-
def self.encode(&block)
|
16
|
-
CsvShaper::Shaper.encode(&block)
|
15
|
+
def self.encode(options = {}, &block)
|
16
|
+
CsvShaper::Shaper.encode(options, &block)
|
17
17
|
end
|
18
18
|
|
19
19
|
def self.configure(&block)
|
data/spec/config_spec.rb
CHANGED
@@ -27,4 +27,20 @@ describe CsvShaper::Config do
|
|
27
27
|
|
28
28
|
shaper.to_csv.should eq "Paul\t27\tMale\n"
|
29
29
|
end
|
30
|
+
|
31
|
+
it "should allow change configuration locally" do
|
32
|
+
CsvShaper::Shaper.config = config
|
33
|
+
|
34
|
+
shaper = CsvShaper::Shaper.new(col_sep: ",") do |csv|
|
35
|
+
csv.headers :name, :age, :gender
|
36
|
+
|
37
|
+
csv.row do |csv|
|
38
|
+
csv.cell :name, 'Paul'
|
39
|
+
csv.cell :age, '27'
|
40
|
+
csv.cell :gender, 'Male'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
shaper.to_csv.should eq "Paul,27,Male\n"
|
45
|
+
end
|
30
46
|
end
|
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.
|
4
|
+
version: 0.3.0
|
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-10-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &70134819722080 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 3.0.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70134819722080
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70134819721660 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70134819721660
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rake
|
38
|
-
requirement: &
|
38
|
+
requirement: &70134819721200 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70134819721200
|
47
47
|
description: ! "\n Creating CSV files in Ruby is painful! CSV Shaper makes life
|
48
48
|
easier! It's\n ideal for converting database backed models with attrbiutes into
|
49
49
|
CSV output.\n It can be used without Rails, but works great with ActiveRecord
|