csverizer 0.0.3 → 0.0.4
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/README.md +3 -3
- data/csverizer.gemspec +1 -1
- data/lib/active_model/csv_array_serializer.rb +3 -3
- data/lib/active_model/csverizer.rb +2 -2
- data/lib/active_model/csverizer/railtie.rb +2 -2
- data/lib/active_model/csverizer/version.rb +2 -2
- data/lib/active_model/csverizer_factory.rb +3 -3
- data/spec/active_model/array_serializer_spec.rb +1 -1
- data/spec/active_model/attributes_spec.rb +3 -3
- data/spec/active_model/csverizer_factory_spec.rb +6 -6
- data/spec/active_model/has_many_spec.rb +1 -1
- data/spec/active_model/has_one_spec.rb +2 -2
- data/spec/active_model/header_spec.rb +11 -11
- data/spec/spec_helper.rb +6 -6
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 16b06c779db7788f399aad871980e12cd1b742b63401977a6d7b9b79a5e5cce1
|
4
|
+
data.tar.gz: 519d54257b3265c374f87d8427acf6ed1202da8812555905c2bd20cd7338800d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cff1a2fed7ca64ff9a1af5519ffe4c63e0bc2db2363ff2520326362954ceaad6749b0ca6b4a31a9c96fbf7784c72bf05687f995506016df01eda2ace1fae656d
|
7
|
+
data.tar.gz: d2fb310c8a6dcda75c3bdc9d7f4569ba4a63b1e58172c0d18acebd2674e641f4c07657dd01b2f3a0475bcd226a7cefcdd23994f5ba3defe645a309b8848fa9aa
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# Csvrizer
|
2
2
|
|
3
3
|
`ActiveModel::Serializers` style CSV serialization.
|
4
4
|
|
@@ -22,10 +22,10 @@ Or install it yourself as:
|
|
22
22
|
class Post
|
23
23
|
include ActiveModel::Serialization
|
24
24
|
attr_accessor :title, :body
|
25
|
-
def attributes; end # not necessary for ActiveModel::
|
25
|
+
def attributes; end # not necessary for ActiveModel::Csvrizer
|
26
26
|
end
|
27
27
|
|
28
|
-
class PostSerializer < ActiveModel::
|
28
|
+
class PostSerializer < ActiveModel::Csvrizer
|
29
29
|
attributes :title, :body
|
30
30
|
end
|
31
31
|
```
|
data/csverizer.gemspec
CHANGED
@@ -5,7 +5,7 @@ require 'active_model/csverizer/version'
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = 'csverizer'
|
8
|
-
spec.version = ActiveModel::
|
8
|
+
spec.version = ActiveModel::Csvrizer::VERSION
|
9
9
|
spec.authors = ['William Cunningham', 'Peter Hankiewicz']
|
10
10
|
spec.email = ['w.a.cunningham.ii@gmail.com', 'peter.hankiewicz@gmail.com']
|
11
11
|
spec.summary = 'Serialize models as CSV.'
|
@@ -10,9 +10,9 @@ module ActiveModel
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def to_a
|
13
|
-
return ActiveModel::
|
13
|
+
return ActiveModel::Csvrizer.new(nil).to_a if @objects.nil?
|
14
14
|
@objects.collect do |object|
|
15
|
-
serializer = @each_serializer || ActiveModel::
|
15
|
+
serializer = @each_serializer || ActiveModel::CsvrizerFactory
|
16
16
|
serializer.new(object, @options).to_a
|
17
17
|
end
|
18
18
|
end
|
@@ -23,7 +23,7 @@ module ActiveModel
|
|
23
23
|
|
24
24
|
def attribute_names
|
25
25
|
return [] unless @objects
|
26
|
-
serializer = @each_serializer || ActiveModel::
|
26
|
+
serializer = @each_serializer || ActiveModel::CsvrizerFactory
|
27
27
|
serializer.new(@objects.first, @options).attribute_names
|
28
28
|
end
|
29
29
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'csv'
|
2
2
|
|
3
3
|
module ActiveModel
|
4
|
-
class
|
4
|
+
class Csvrizer
|
5
5
|
@_attributes = []
|
6
6
|
@associations = []
|
7
7
|
@root = true
|
@@ -23,7 +23,7 @@ module ActiveModel
|
|
23
23
|
def self.has_one(associated)
|
24
24
|
@associations << {
|
25
25
|
associated: associated,
|
26
|
-
serializer: ActiveModel::
|
26
|
+
serializer: ActiveModel::CsvrizerFactory
|
27
27
|
}
|
28
28
|
end
|
29
29
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
class ActiveModel::
|
1
|
+
class ActiveModel::Csvrizer::Railtie < Rails::Railtie
|
2
2
|
initializer 'csverizer' do
|
3
3
|
ActiveSupport.on_load(:action_controller) do
|
4
4
|
ActionController::Renderers.add :csv do |object, options|
|
@@ -10,7 +10,7 @@ class ActiveModel::CSVerizer::Railtie < Rails::Railtie
|
|
10
10
|
ActiveModel::CsvArraySerializer.new(object, hash)
|
11
11
|
else
|
12
12
|
hash = options.slice(:serializer)
|
13
|
-
ActiveModel::
|
13
|
+
ActiveModel::CsvrizerFactory.new(object, hash)
|
14
14
|
end
|
15
15
|
data = serializer.to_csv
|
16
16
|
# TODO: probably should implement a specialized exception for this
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'active_support/inflections'
|
2
2
|
|
3
3
|
module ActiveModel
|
4
|
-
module
|
4
|
+
module CsvrizerFactory
|
5
5
|
def self.new(object, options = {})
|
6
|
-
return ActiveModel::
|
7
|
-
klass = object.model_name.name + '
|
6
|
+
return ActiveModel::Csvrizer.new(nil) if object.nil?
|
7
|
+
klass = object.model_name.name + 'Csvrizer'
|
8
8
|
klass.constantize.new(object, options)
|
9
9
|
end
|
10
10
|
end
|
@@ -9,7 +9,7 @@ describe 'ArraySerializer' do
|
|
9
9
|
it 'renders csv using specified serializer' do
|
10
10
|
array_serializer = ActiveModel::CsvArraySerializer.new(
|
11
11
|
comments,
|
12
|
-
each_serializer:
|
12
|
+
each_serializer: CommentCsvrizer
|
13
13
|
)
|
14
14
|
csv = array_serializer.to_csv
|
15
15
|
|
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe 'attributes' do
|
4
4
|
it 'pulls attributes off associated model' do
|
5
5
|
post = Post.new(name: 'Samwise', body: 'Hobbit extraordinaire.')
|
6
|
-
serializer =
|
6
|
+
serializer = PostCsvrizer.new(post)
|
7
7
|
csv = serializer.to_csv
|
8
8
|
|
9
9
|
expect(csv).to include(post.name)
|
@@ -12,7 +12,7 @@ describe 'attributes' do
|
|
12
12
|
|
13
13
|
it 'favors methods defined on serializer' do
|
14
14
|
post = Post.new(name: 'Samwise', body: 'Hobbit extraordinaire.')
|
15
|
-
serializer =
|
15
|
+
serializer = Post2Csvrizer.new(post)
|
16
16
|
csv = serializer.to_csv
|
17
17
|
|
18
18
|
expect(csv).to include('pie')
|
@@ -22,7 +22,7 @@ describe 'attributes' do
|
|
22
22
|
|
23
23
|
it 'allows attributes declaration to be split up' do
|
24
24
|
post = Post.new(name: 'Samwise', body: 'Hobbit extraordinaire.')
|
25
|
-
serializer =
|
25
|
+
serializer = Post3Csvrizer.new(post)
|
26
26
|
csv = serializer.to_csv
|
27
27
|
|
28
28
|
expect(csv).to include(post.name)
|
@@ -1,22 +1,22 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe ActiveModel::
|
3
|
+
describe ActiveModel::CsvrizerFactory do
|
4
4
|
let(:post) { Post.new(name: 'a', body: 'b') }
|
5
5
|
|
6
6
|
describe '#new' do
|
7
7
|
it 'builds a serializer with type based on singular association' do
|
8
|
-
serializer = ActiveModel::
|
9
|
-
expect(serializer).to be_a(
|
8
|
+
serializer = ActiveModel::CsvrizerFactory.new(post)
|
9
|
+
expect(serializer).to be_a(PostCsvrizer)
|
10
10
|
end
|
11
11
|
|
12
12
|
it 'builds a serializer with type based on plural association' do
|
13
|
-
serializer = ActiveModel::
|
14
|
-
expect(serializer).to be_a(
|
13
|
+
serializer = ActiveModel::CsvrizerFactory.new(post)
|
14
|
+
expect(serializer).to be_a(PostCsvrizer)
|
15
15
|
end
|
16
16
|
|
17
17
|
it 'raises name error when expected serializer not found' do
|
18
18
|
expect do
|
19
|
-
ActiveModel::
|
19
|
+
ActiveModel::CsvrizerFactory.new(Photo.new)
|
20
20
|
end.to raise_error(NameError)
|
21
21
|
end
|
22
22
|
end
|
@@ -9,7 +9,7 @@ describe 'has_many' do
|
|
9
9
|
Comment.new(text: 'e')
|
10
10
|
]
|
11
11
|
end
|
12
|
-
let(:serializer) {
|
12
|
+
let(:serializer) { PostCsvrizer.new(post, root: false) }
|
13
13
|
|
14
14
|
it 'appends associated objects csv to multiple copies of this' do
|
15
15
|
csv = serializer.to_csv
|
@@ -4,8 +4,8 @@ describe 'has_one' do
|
|
4
4
|
let(:post) { Post.new(name: 'a', body: 'b', author: author) }
|
5
5
|
let(:author) { Author.new(name: 'd') }
|
6
6
|
let(:category) { Category.new(name: 'e') }
|
7
|
-
let(:post_serializer) {
|
8
|
-
let(:author_serializer) {
|
7
|
+
let(:post_serializer) { PostCsvrizer.new(post) }
|
8
|
+
let(:author_serializer) { AuthorCsvrizer.new(author) }
|
9
9
|
|
10
10
|
it 'appends the associated objects csv data to this' do
|
11
11
|
csv = post_serializer.to_csv
|
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe 'headers' do
|
4
4
|
it 'includes headers based on attribute names by default' do
|
5
5
|
post = Post.new(name: 'Samwise', body: 'Hobbit extraordinaire.')
|
6
|
-
serializer =
|
6
|
+
serializer = PostCsvrizer.new(post)
|
7
7
|
csv = serializer.to_csv
|
8
8
|
|
9
9
|
expect(csv).to include('name')
|
@@ -13,7 +13,7 @@ describe 'headers' do
|
|
13
13
|
context 'when root option set to false during serializer instantiation' do
|
14
14
|
it 'does not include headers' do
|
15
15
|
post = Post.new(name: 'Samwise', body: 'Hobbit extraordinaire.')
|
16
|
-
serializer =
|
16
|
+
serializer = PostCsvrizer.new(post, root: false)
|
17
17
|
csv = serializer.to_csv
|
18
18
|
|
19
19
|
expect(csv).to_not include('name')
|
@@ -22,12 +22,12 @@ describe 'headers' do
|
|
22
22
|
end
|
23
23
|
|
24
24
|
context 'when root set to false during class definition' do
|
25
|
-
before {
|
26
|
-
after {
|
25
|
+
before { PostCsvrizer.root = false }
|
26
|
+
after { PostCsvrizer.root = true }
|
27
27
|
|
28
28
|
it 'does not include headers' do
|
29
29
|
post = Post.new(name: 'Samwise', body: 'Hobbit extraordinaire.')
|
30
|
-
serializer =
|
30
|
+
serializer = PostCsvrizer.new(post, root: false)
|
31
31
|
csv = serializer.to_csv
|
32
32
|
|
33
33
|
expect(csv).to_not include('name')
|
@@ -36,12 +36,12 @@ describe 'headers' do
|
|
36
36
|
end
|
37
37
|
|
38
38
|
context 'when parent serializer root set to false' do
|
39
|
-
before { ActiveModel::
|
40
|
-
after { ActiveModel::
|
39
|
+
before { ActiveModel::Csvrizer.root = false }
|
40
|
+
after { ActiveModel::Csvrizer.root = true }
|
41
41
|
|
42
42
|
it 'does not include headers' do
|
43
43
|
post = Post.new(name: 'Samwise', body: 'Hobbit extraordinaire.')
|
44
|
-
serializer =
|
44
|
+
serializer = PostCsvrizer.new(post, root: false)
|
45
45
|
csv = serializer.to_csv
|
46
46
|
|
47
47
|
expect(csv).to_not include('name')
|
@@ -53,7 +53,7 @@ describe 'headers' do
|
|
53
53
|
it 'renders associated attributes prepended with its name' do
|
54
54
|
category = Category.new(name: 'a')
|
55
55
|
author = Author.new(name: 'b', category: category)
|
56
|
-
serializer =
|
56
|
+
serializer = AuthorCsvrizer.new(author)
|
57
57
|
csv = serializer.to_csv
|
58
58
|
|
59
59
|
expect(csv).to include('name')
|
@@ -66,7 +66,7 @@ describe 'headers' do
|
|
66
66
|
category = Category.new(name: 'a')
|
67
67
|
author = Author.new(name: 'b', category: category)
|
68
68
|
post = Post.new(name: 'a', body: 'b', author: author)
|
69
|
-
serializer =
|
69
|
+
serializer = PostCsvrizer.new(post)
|
70
70
|
csv = serializer.to_csv
|
71
71
|
|
72
72
|
expect(csv).to include('name')
|
@@ -84,7 +84,7 @@ describe 'headers' do
|
|
84
84
|
Comment.new(text: 'e')
|
85
85
|
]
|
86
86
|
post = Post.new(name: 'a', body: 'b', comments: comments)
|
87
|
-
serializer =
|
87
|
+
serializer = PostCsvrizer.new(post)
|
88
88
|
csv = serializer.to_csv
|
89
89
|
|
90
90
|
expect(csv).to include('name')
|
data/spec/spec_helper.rb
CHANGED
@@ -26,14 +26,14 @@ class Post
|
|
26
26
|
attr_accessor :name, :body, :comments, :author, :category
|
27
27
|
end
|
28
28
|
|
29
|
-
class
|
29
|
+
class PostCsvrizer < ActiveModel::Csvrizer
|
30
30
|
attributes :name, :body
|
31
31
|
has_many :comments
|
32
32
|
has_one :author
|
33
33
|
has_one :category
|
34
34
|
end
|
35
35
|
|
36
|
-
class
|
36
|
+
class Post2Csvrizer < PostCsvrizer
|
37
37
|
attributes :name, :body
|
38
38
|
|
39
39
|
def name
|
@@ -41,7 +41,7 @@ class Post2CSVerizer < PostCSVerizer
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
-
class
|
44
|
+
class Post3Csvrizer < ActiveModel::Csvrizer
|
45
45
|
attributes :name
|
46
46
|
attributes :body
|
47
47
|
end
|
@@ -55,7 +55,7 @@ class Comment
|
|
55
55
|
attr_accessor :text
|
56
56
|
end
|
57
57
|
|
58
|
-
class
|
58
|
+
class CommentCsvrizer < ActiveModel::Csvrizer
|
59
59
|
attributes :text
|
60
60
|
end
|
61
61
|
|
@@ -75,7 +75,7 @@ class Author
|
|
75
75
|
attr_accessor :name, :category
|
76
76
|
end
|
77
77
|
|
78
|
-
class
|
78
|
+
class AuthorCsvrizer < ActiveModel::Csvrizer
|
79
79
|
attributes :name
|
80
80
|
|
81
81
|
has_one :category
|
@@ -90,6 +90,6 @@ class Category
|
|
90
90
|
attr_accessor :name
|
91
91
|
end
|
92
92
|
|
93
|
-
class
|
93
|
+
class CategoryCsvrizer < ActiveModel::Csvrizer
|
94
94
|
attributes :name
|
95
95
|
end
|