serialize_with_options 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +3 -1
- data/lib/serialize_with_options.rb +8 -15
- data/lib/serialize_with_options/version.rb +1 -1
- data/serialize_with_options.gemspec +1 -1
- data/test/serialize_with_options_test.rb +20 -6
- data/test/test_helper.rb +1 -1
- metadata +25 -10
data/README.markdown
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
SerializeWithOptions
|
2
2
|
====================
|
3
3
|
|
4
|
+
[![Code Climate](https://codeclimate.com/github/vigetlabs/serialize_with_options.png)](https://codeclimate.com/github/vigetlabs/serialize_with_options)
|
5
|
+
|
4
6
|
This plugin is designed to make creating XML and JSON APIs for your Rails apps dead simple. We noticed a lot of repetition when creating API responses in our controllers. With this plugin, you can set the serialization options for a model with a simple DSL, rather than repeating them in every controller that includes it.
|
5
7
|
|
6
8
|
|
@@ -76,7 +78,7 @@ And run "`bundle install`"
|
|
76
78
|
|
77
79
|
* * *
|
78
80
|
|
79
|
-
Copyright (c) 2009-
|
81
|
+
Copyright (c) 2009-2014 David Eisinger & Zachary Porter ([Viget Labs][vgt]), released under the MIT license.
|
80
82
|
|
81
83
|
[ser]: http://api.rubyonrails.org/classes/ActiveRecord/Serialization.html
|
82
84
|
[vgt]: http://www.viget.com/
|
@@ -67,25 +67,18 @@ module SerializeWithOptions
|
|
67
67
|
end
|
68
68
|
|
69
69
|
module InstanceMethods
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
def as_json(opts = nil)
|
76
|
-
set, opts = parse_serialization_options(opts)
|
77
|
-
super(self.class.serialization_options(set).deep_merge(opts || {}))
|
70
|
+
%w(to_xml to_json as_json).each do |method_name|
|
71
|
+
define_method method_name do |*args|
|
72
|
+
set, opts = parse_serialization_options(args)
|
73
|
+
super self.class.serialization_options(set).deep_merge(opts)
|
74
|
+
end
|
78
75
|
end
|
79
76
|
|
80
77
|
private
|
81
78
|
|
82
|
-
def parse_serialization_options(
|
83
|
-
|
84
|
-
|
85
|
-
opts = {}
|
86
|
-
else
|
87
|
-
set = :default
|
88
|
-
end
|
79
|
+
def parse_serialization_options(args)
|
80
|
+
opts = args.extract_options!
|
81
|
+
set = args.shift || :default
|
89
82
|
|
90
83
|
[set, opts]
|
91
84
|
end
|
@@ -19,7 +19,7 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
20
|
s.require_paths = ["lib"]
|
21
21
|
|
22
|
-
s.add_dependency "activerecord", "~>
|
22
|
+
s.add_dependency "activerecord", "~> 4.0"
|
23
23
|
|
24
24
|
s.add_development_dependency "shoulda"
|
25
25
|
s.add_development_dependency "sqlite3"
|
@@ -78,7 +78,7 @@ class Review < ActiveRecord::Base
|
|
78
78
|
end
|
79
79
|
end
|
80
80
|
|
81
|
-
class SerializeWithOptionsTest < Test
|
81
|
+
class SerializeWithOptionsTest < Minitest::Test
|
82
82
|
def self.should_serialize_with_options
|
83
83
|
should "include active_record attributes" do
|
84
84
|
assert_equal @user.name, @user_hash["name"]
|
@@ -181,19 +181,33 @@ class SerializeWithOptionsTest < Test::Unit::TestCase
|
|
181
181
|
|
182
182
|
should "serialize the associated object properly" do
|
183
183
|
review_hash = Hash.from_xml(@review.to_xml)
|
184
|
-
assert_equal @user.
|
184
|
+
assert_equal @user.name, review_hash["review"]["reviewable"]["name"]
|
185
185
|
end
|
186
186
|
end
|
187
187
|
|
188
188
|
context "being converted to JSON" do
|
189
189
|
setup do
|
190
|
-
@user_hash = JSON.parse(@user.to_json)
|
191
|
-
@post_hash = JSON.parse(@post.to_json)
|
192
|
-
@blog_post_hash = JSON.parse(@blog_post.to_json)
|
190
|
+
@user_hash = JSON.parse(@user.to_json)
|
191
|
+
@post_hash = JSON.parse(@post.to_json)
|
192
|
+
@blog_post_hash = JSON.parse(@blog_post.to_json)
|
193
193
|
end
|
194
194
|
|
195
195
|
should_serialize_with_options
|
196
196
|
end
|
197
|
+
|
198
|
+
context 'passing options to the serializer' do
|
199
|
+
setup do
|
200
|
+
@user_hash = @user.as_json(:with_email, :root => 'custom_root')['custom_root']
|
201
|
+
end
|
202
|
+
|
203
|
+
should "include active_record attributes" do
|
204
|
+
assert_equal @user.name, @user_hash["name"]
|
205
|
+
end
|
206
|
+
|
207
|
+
should "exclude specified attributes" do
|
208
|
+
assert_equal @user.email, @user_hash["email"]
|
209
|
+
end
|
210
|
+
end
|
197
211
|
|
198
212
|
context "serializing associated models" do
|
199
213
|
setup do
|
@@ -202,7 +216,7 @@ class SerializeWithOptionsTest < Test::Unit::TestCase
|
|
202
216
|
end
|
203
217
|
|
204
218
|
should "find associations with multi-word names" do
|
205
|
-
user_hash = JSON.parse(@user.to_json(:with_check_ins))
|
219
|
+
user_hash = JSON.parse(@user.to_json(:with_check_ins))
|
206
220
|
assert_equal @check_in.code_name, user_hash['check_ins'].first['code_name']
|
207
221
|
end
|
208
222
|
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: serialize_with_options
|
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,22 +9,27 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-09-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: '
|
21
|
+
version: '4.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: '4.0'
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: shoulda
|
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: sqlite3
|
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: Simple XML and JSON APIs for your Rails app
|
48
63
|
email:
|
49
64
|
- david.eisinger@gmail.com
|
@@ -82,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
97
|
version: '0'
|
83
98
|
requirements: []
|
84
99
|
rubyforge_project: serialize_with_options
|
85
|
-
rubygems_version: 1.8.
|
100
|
+
rubygems_version: 1.8.23
|
86
101
|
signing_key:
|
87
102
|
specification_version: 3
|
88
103
|
summary: Simple XML and JSON APIs for your Rails app
|