csv_shaper 0.0.3 → 0.1.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/.groc.json +4 -0
- data/README.md +2 -0
- data/lib/csv_shaper/encoder.rb +7 -1
- data/lib/csv_shaper/header.rb +19 -20
- data/lib/csv_shaper/row.rb +16 -21
- data/lib/csv_shaper/shaper.rb +11 -7
- data/lib/csv_shaper/version.rb +1 -1
- data/lib/csv_shaper_template.rb +9 -0
- metadata +9 -7
data/.groc.json
ADDED
data/README.md
CHANGED
@@ -6,6 +6,8 @@ Creating CSV files in Ruby is painful! CSV Shaper makes life easier! It's ideal
|
|
6
6
|
|
7
7
|
[](http://travis-ci.org/paulspringett/csv_shaper)
|
8
8
|
|
9
|
+
Annotated source: http://paulspringett.github.com/csv_shaper/
|
10
|
+
|
9
11
|
### Example Usage
|
10
12
|
|
11
13
|
```ruby
|
data/lib/csv_shaper/encoder.rb
CHANGED
@@ -1,6 +1,12 @@
|
|
1
1
|
require 'csv'
|
2
2
|
|
3
3
|
module CsvShaper
|
4
|
+
# Encoder
|
5
|
+
# Takes a Header and Array of Rows and converts them to a valid CSV formatted String
|
6
|
+
# Example:
|
7
|
+
# ```
|
8
|
+
# CsvShaper::Encoder.new(@header, @rows).to_csv
|
9
|
+
# ```
|
4
10
|
class Encoder
|
5
11
|
def initialize(header, rows = [])
|
6
12
|
if header.nil?
|
@@ -26,7 +32,7 @@ module CsvShaper
|
|
26
32
|
|
27
33
|
private
|
28
34
|
|
29
|
-
# Internal: make use of CSV#values_at to pad out the
|
35
|
+
# Internal: make use of `CSV#values_at` to pad out the
|
30
36
|
# cells into the correct columns for the headers
|
31
37
|
#
|
32
38
|
# Returns an Array of Arrays
|
data/lib/csv_shaper/header.rb
CHANGED
@@ -1,20 +1,20 @@
|
|
1
1
|
module CsvShaper
|
2
|
-
|
3
2
|
# Header
|
4
3
|
# Handles creating and mapping of the headers
|
5
4
|
# Examples:
|
6
|
-
#
|
7
|
-
#
|
5
|
+
# ```
|
6
|
+
# # assign the headers from the attributes of a class
|
7
|
+
# csv.headers User
|
8
8
|
#
|
9
|
-
#
|
10
|
-
#
|
9
|
+
# # assigns headers normally
|
10
|
+
# csv.headers :name, :age, :location
|
11
11
|
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
12
|
+
# # pass a block
|
13
|
+
# csv.headers do |csv|
|
14
|
+
# csv.columns :name, :age, :location
|
15
|
+
# csv.mappings name: 'Full name, location: 'Region'
|
16
|
+
# end
|
17
|
+
# ```
|
18
18
|
class Header
|
19
19
|
attr_reader :klass, :mappings, :mapped_columns
|
20
20
|
|
@@ -22,14 +22,11 @@ module CsvShaper
|
|
22
22
|
@mappings = {}
|
23
23
|
@columns = []
|
24
24
|
|
25
|
-
# csv.headers do |head|
|
26
25
|
if block_given?
|
27
26
|
yield self
|
28
27
|
elsif args.any?
|
29
|
-
# csv.headers User
|
30
28
|
if (@klass = args.first.respond_to?(:attribute_names) && args.first)
|
31
29
|
columns(*@klass.attribute_names)
|
32
|
-
# csv.headers :name, :age, :location
|
33
30
|
else
|
34
31
|
columns(*args)
|
35
32
|
end
|
@@ -40,9 +37,10 @@ module CsvShaper
|
|
40
37
|
# of Symbol column names. Union join the existing column
|
41
38
|
# names with those passed
|
42
39
|
# Example:
|
43
|
-
#
|
44
|
-
#
|
45
|
-
#
|
40
|
+
# ```
|
41
|
+
# header.columns :name, :age, :location
|
42
|
+
# ```
|
43
|
+
# `args` - Array of Symbol arguments passed
|
46
44
|
#
|
47
45
|
# Returns as Array of Symbols
|
48
46
|
def columns(*args)
|
@@ -52,9 +50,10 @@ module CsvShaper
|
|
52
50
|
# Public: Define mappings of the Symbol column names
|
53
51
|
# to nicer, human names
|
54
52
|
# Example:
|
55
|
-
#
|
56
|
-
#
|
57
|
-
#
|
53
|
+
# ```
|
54
|
+
# header.mappings name: 'Full name', age: 'Age of person'
|
55
|
+
# ```
|
56
|
+
# `hash` - Hash of mappings where the key is the column name to map
|
58
57
|
# and the value is the human readable value
|
59
58
|
#
|
60
59
|
# Returns a Hash of mappings
|
data/lib/csv_shaper/row.rb
CHANGED
@@ -4,36 +4,32 @@ module CsvShaper
|
|
4
4
|
# Handles creating of cells within a row and
|
5
5
|
# assigning of the model's values to cells
|
6
6
|
# Examples:
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
7
|
+
# ```
|
8
|
+
# # pass a model to the row
|
9
|
+
# csv.row @model do |csv, model|
|
10
|
+
# ...
|
11
|
+
# end
|
11
12
|
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
13
|
+
# # create an empty row instance
|
14
|
+
# csv.row do |csv|
|
15
|
+
# ...
|
16
|
+
# end
|
16
17
|
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
18
|
+
# # create a row with prefilled cells from a model
|
19
|
+
# # note no block is passed
|
20
|
+
# csv.row @model, :name, :age, :location
|
21
|
+
# ```
|
21
22
|
class Row
|
22
23
|
attr_reader :model, :cells
|
23
24
|
|
24
25
|
def initialize(*args)
|
25
26
|
@cells = ActiveSupport::OrderedHash.new
|
26
27
|
|
27
|
-
# csv.row @user do |csv, user|
|
28
28
|
if args.one? && block_given?
|
29
29
|
@model = args.first
|
30
30
|
yield self, @model
|
31
|
-
|
32
|
-
# csv.row do |csv|
|
33
31
|
elsif args.empty? && block_given?
|
34
32
|
yield self
|
35
|
-
|
36
|
-
# csv.row @user, :name, :age, :location
|
37
33
|
elsif args.length > 1
|
38
34
|
@model = args.shift
|
39
35
|
args.each { |col| cell(col) }
|
@@ -44,7 +40,7 @@ module CsvShaper
|
|
44
40
|
|
45
41
|
# Public: assign the given Array of args to cells in this Row
|
46
42
|
#
|
47
|
-
#
|
43
|
+
# `args` - Array of the arguments passed (expected to be Symbols)
|
48
44
|
#
|
49
45
|
# Returns an Array of the Cells in this row
|
50
46
|
def cells(*args)
|
@@ -57,8 +53,8 @@ module CsvShaper
|
|
57
53
|
# call that method on the @model and assign it to a column of
|
58
54
|
# the same name. Otherwise a value will need to be passed also
|
59
55
|
#
|
60
|
-
#
|
61
|
-
#
|
56
|
+
# `column` - Symbol of the column to add to value to
|
57
|
+
# `value` - data to assign to the cell (default: nil)
|
62
58
|
#
|
63
59
|
# Returns an Array of the Row's cells
|
64
60
|
def cell(column, value = nil)
|
@@ -72,6 +68,5 @@ module CsvShaper
|
|
72
68
|
|
73
69
|
@cells
|
74
70
|
end
|
75
|
-
|
76
71
|
end
|
77
72
|
end
|
data/lib/csv_shaper/shaper.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
module CsvShaper
|
2
|
+
# Shaper
|
3
|
+
# Core CsvShaper class. Delegates header and row generating.
|
2
4
|
class Shaper
|
3
5
|
attr_reader :header, :rows
|
4
6
|
|
@@ -10,14 +12,16 @@ module CsvShaper
|
|
10
12
|
# Public: creates a new instance of Shaper taps it with
|
11
13
|
# with the given block and encodes it to a String of CSV data
|
12
14
|
# Example:
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
15
|
+
# ```
|
16
|
+
# data = CsvShaper::Shaper.encode do |csv|
|
17
|
+
# csv.rows @users do |csv, user|
|
18
|
+
# csv.cells :name, :age, :gender
|
17
19
|
# end
|
20
|
+
# end
|
18
21
|
#
|
19
|
-
#
|
20
|
-
#
|
22
|
+
# puts data
|
23
|
+
# => "Name,Age,Gender\n'Joe Bloggs',25,'M'\n'John Smith',34,'M'"
|
24
|
+
# ```
|
21
25
|
#
|
22
26
|
# Returns a String
|
23
27
|
def self.encode
|
@@ -44,7 +48,7 @@ module CsvShaper
|
|
44
48
|
|
45
49
|
# Public: adds several rows to the CSV
|
46
50
|
#
|
47
|
-
#
|
51
|
+
# `collection` - an Enumerable of objects to be passed to #row
|
48
52
|
#
|
49
53
|
# Returns an updated Array of Row objects
|
50
54
|
def rows(collection = nil, &block)
|
data/lib/csv_shaper/version.rb
CHANGED
data/lib/csv_shaper_template.rb
CHANGED
@@ -1,4 +1,8 @@
|
|
1
|
+
# CsvShaperTemplate
|
2
|
+
# Rails view template class
|
1
3
|
class CsvShaperTemplate < CsvShaper::Shaper
|
4
|
+
# Expected `encode` call
|
5
|
+
# Instantiates a new CsvShaperTemplate object and calls `to_csv` on it
|
2
6
|
def self.encode(context)
|
3
7
|
new(context).tap { |shaper| yield shaper }.to_csv
|
4
8
|
end
|
@@ -9,10 +13,15 @@ class CsvShaperTemplate < CsvShaper::Shaper
|
|
9
13
|
end
|
10
14
|
end
|
11
15
|
|
16
|
+
# CsvShaperHandler
|
17
|
+
# Template handler for Rails
|
12
18
|
class CsvShaperHandler
|
13
19
|
cattr_accessor :default_format
|
14
20
|
self.default_format = Mime::CSV
|
15
21
|
|
22
|
+
# Expected `call` class method
|
23
|
+
# Set response headers with filename
|
24
|
+
# Primarily calls CsvShaperTemplate.encode, passing through the context (self)
|
16
25
|
def self.call(template)
|
17
26
|
%{
|
18
27
|
unless defined?(ActionMailer) && defined?(ActionMailer::Base) && controller.is_a?(ActionMailer::Base)
|
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.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-07-23 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &70169242549620 !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: *70169242549620
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70169242545580 !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: *70169242545580
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rake
|
38
|
-
requirement: &
|
38
|
+
requirement: &70169242544220 !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: *70169242544220
|
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
|
@@ -55,6 +55,7 @@ extensions: []
|
|
55
55
|
extra_rdoc_files: []
|
56
56
|
files:
|
57
57
|
- .gitignore
|
58
|
+
- .groc.json
|
58
59
|
- .travis.yml
|
59
60
|
- Gemfile
|
60
61
|
- LICENSE
|
@@ -105,3 +106,4 @@ test_files:
|
|
105
106
|
- spec/header_spec.rb
|
106
107
|
- spec/row_spec.rb
|
107
108
|
- spec/spec_helper.rb
|
109
|
+
has_rdoc:
|