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 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-2011 David Eisinger ([Viget Labs][vgt]), released under the MIT license.
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
- def to_xml(opts = {})
71
- set, opts = parse_serialization_options(opts)
72
- super(self.class.serialization_options(set).deep_merge(opts))
73
- end
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(opts)
83
- if opts.is_a? Symbol
84
- set = opts
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
@@ -1,3 +1,3 @@
1
1
  module SerializeWithOptions
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  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", "~> 3.0"
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::Unit::TestCase
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.email, review_hash["review"]["reviewable"]["email"]
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)["user"]
191
- @post_hash = JSON.parse(@post.to_json)["post"]
192
- @blog_post_hash = JSON.parse(@blog_post.to_json)["blog_post"]
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))["user"]
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
@@ -3,7 +3,7 @@ $:.unshift(File.dirname(__FILE__) + '/../lib')
3
3
 
4
4
  require 'rubygems'
5
5
  require 'active_record'
6
- require 'test/unit'
6
+ require 'minitest/autorun'
7
7
  require 'shoulda'
8
8
  require 'json'
9
9
  require 'serialize_with_options'
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.2.1
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: 2011-12-02 00:00:00.000000000 Z
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: &70169796710200 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '3.0'
21
+ version: '4.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70169796710200
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: &70169796709760 !ruby/object:Gem::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: *70169796709760
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: &70169796709180 !ruby/object:Gem::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: *70169796709180
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.10
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