ixtlan-babel 0.3.5 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -1
- data/lib/ixtlan/babel/abstract_filter.rb +3 -3
- data/lib/ixtlan/babel/dm_validation_errors_serializer.rb +1 -1
- data/lib/ixtlan/babel/factory.rb +3 -2
- data/lib/ixtlan/babel/filter_config.rb +8 -6
- data/lib/ixtlan/babel/hash_filter.rb +6 -4
- data/lib/ixtlan/babel/model_filter.rb +11 -5
- data/lib/ixtlan/babel/params_filter.rb +36 -7
- data/lib/ixtlan/babel/serializer.rb +7 -6
- data/spec/hash_filter_spec.rb +45 -19
- data/spec/model_filter_spec.rb +53 -137
- data/spec/model_filter_with_methods_spec.rb +68 -101
- data/spec/params_filter_spec.rb +42 -36
- metadata +27 -46
- data/lib/ixtlan/babel/deserializer.rb +0 -88
- data/lib/ixtlan/babel/hash_filter.rb- +0 -170
- data/lib/ixtlan/babel/no_timestamp_serializer.rb +0 -40
data/Gemfile
CHANGED
data/lib/ixtlan/babel/factory.rb
CHANGED
@@ -32,7 +32,7 @@ module Ixtlan
|
|
32
32
|
DATE_TIME_TO_S = Proc.new do |dt|
|
33
33
|
dt.strftime('%Y-%m-%dT%H:%M:%S.') + ("%06d" % (dt.sec_fraction * NANOSECONDS_IN_DAY ) )[0..6] + dt.strftime('%z')
|
34
34
|
end
|
35
|
-
|
35
|
+
|
36
36
|
DEFAULT_MAP = {
|
37
37
|
'DateTime' => DATE_TIME_TO_S,
|
38
38
|
'ActiveSupport::TimeWithZone' => TIME_TO_S,
|
@@ -57,7 +57,8 @@ module Ixtlan
|
|
57
57
|
def new_serializer( resource )
|
58
58
|
if resource.respond_to?(:model)
|
59
59
|
model = resource.model
|
60
|
-
elsif resource.respond_to?( :collect) &&
|
60
|
+
elsif resource.respond_to?( :collect) &&
|
61
|
+
!resource.respond_to?( :to_hash)
|
61
62
|
if resource.empty?
|
62
63
|
return EmptyArraySerializer.new
|
63
64
|
else
|
@@ -22,7 +22,7 @@ require 'ixtlan/babel/context'
|
|
22
22
|
module Ixtlan
|
23
23
|
module Babel
|
24
24
|
class FilterConfig
|
25
|
-
|
25
|
+
|
26
26
|
private
|
27
27
|
|
28
28
|
def context
|
@@ -32,7 +32,7 @@ module Ixtlan
|
|
32
32
|
def context_options( context_or_options )
|
33
33
|
if context_or_options
|
34
34
|
case context_or_options
|
35
|
-
when Symbol
|
35
|
+
when Symbol
|
36
36
|
if opts = context[ context_or_options ]
|
37
37
|
opts.dup
|
38
38
|
end
|
@@ -44,7 +44,7 @@ module Ixtlan
|
|
44
44
|
|
45
45
|
public
|
46
46
|
|
47
|
-
def default_context_key( single = :single,
|
47
|
+
def default_context_key( single = :single,
|
48
48
|
collection = :collection )
|
49
49
|
@single, @collection = single, collection
|
50
50
|
end
|
@@ -58,15 +58,17 @@ module Ixtlan
|
|
58
58
|
end
|
59
59
|
|
60
60
|
def single_options( context_or_options )
|
61
|
-
context_options( context_or_options ) ||
|
61
|
+
context_options( context_or_options ) ||
|
62
|
+
context[ default_context_key[ 0 ] ] || {}
|
62
63
|
end
|
63
64
|
|
64
65
|
def collection_options( context_or_options )
|
65
|
-
context_options( context_or_options ) ||
|
66
|
+
context_options( context_or_options ) ||
|
67
|
+
context[ default_context_key[ 1 ] ] || {}
|
66
68
|
end
|
67
69
|
|
68
70
|
attr_accessor :root
|
69
71
|
|
70
72
|
end
|
71
73
|
end
|
72
|
-
end
|
74
|
+
end
|
@@ -25,7 +25,7 @@ module Ixtlan
|
|
25
25
|
|
26
26
|
def filter( data )
|
27
27
|
if data
|
28
|
-
filter_data( data,
|
28
|
+
filter_data( data,
|
29
29
|
Context.new( options ) )
|
30
30
|
end
|
31
31
|
end
|
@@ -33,7 +33,7 @@ module Ixtlan
|
|
33
33
|
private
|
34
34
|
|
35
35
|
def filter_array( array, options )
|
36
|
-
array.collect do |item|
|
36
|
+
array.collect do |item|
|
37
37
|
if item.is_a?( Array ) || item.is_a?( Hash )
|
38
38
|
filter_data( item, options )
|
39
39
|
else
|
@@ -48,9 +48,11 @@ module Ixtlan
|
|
48
48
|
k = k.to_s
|
49
49
|
case v
|
50
50
|
when Hash
|
51
|
-
result[ k ] = filter_data( v,
|
51
|
+
result[ k ] = filter_data( v,
|
52
|
+
context[ k ] ) if context.include?( k )
|
52
53
|
when Array
|
53
|
-
result[ k ] = filter_array( v,
|
54
|
+
result[ k ] = filter_array( v,
|
55
|
+
context[ k ] ) if context.include?( k )
|
54
56
|
else
|
55
57
|
result[ k ] = serialize( v ) if context.allowed?( k )
|
56
58
|
end
|
@@ -28,7 +28,7 @@ module Ixtlan
|
|
28
28
|
data = block.call( model )
|
29
29
|
filter_data( model, data,
|
30
30
|
Context.new( options ),
|
31
|
-
&block )
|
31
|
+
&block )
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
@@ -54,16 +54,22 @@ module Ixtlan
|
|
54
54
|
|
55
55
|
def filter_data(model, data, context, &block)
|
56
56
|
setup_data(model, data, context)
|
57
|
-
|
57
|
+
|
58
58
|
result = {}
|
59
59
|
data.each do |k,v|
|
60
60
|
k = k.to_s
|
61
61
|
if v.respond_to? :attributes
|
62
|
-
result[ k ] = filter_data( v,
|
62
|
+
result[ k ] = filter_data( v,
|
63
|
+
block.call(v),
|
64
|
+
context[ k ],
|
65
|
+
&block ) if context.include?( k )
|
63
66
|
elsif v.is_a? Array
|
64
|
-
result[ k ] = filter_array( v,
|
67
|
+
result[ k ] = filter_array( v,
|
68
|
+
context[ k ],
|
69
|
+
&block ) if context.include?( k )
|
65
70
|
else
|
66
|
-
result[ k ] = serialize( v ) if context.allowed?( k ) &&
|
71
|
+
result[ k ] = serialize( v ) if context.allowed?( k ) &&
|
72
|
+
! v.respond_to?( :attributes )
|
67
73
|
end
|
68
74
|
end
|
69
75
|
result
|
@@ -60,6 +60,37 @@ module Ixtlan
|
|
60
60
|
self
|
61
61
|
end
|
62
62
|
|
63
|
+
class FilterResult
|
64
|
+
|
65
|
+
def initialize( model, params, keeps )
|
66
|
+
@model = model
|
67
|
+
@data = keeps
|
68
|
+
@data[ :params ] = params
|
69
|
+
end
|
70
|
+
|
71
|
+
def new_model
|
72
|
+
@model.send( :new, params )
|
73
|
+
end
|
74
|
+
|
75
|
+
def params
|
76
|
+
@data[ :params ]
|
77
|
+
end
|
78
|
+
|
79
|
+
def method_missing( method, *args )
|
80
|
+
if respond_to?( method )
|
81
|
+
@data[ method ] || @data[ method.to_s ]
|
82
|
+
elsif @data.respond_to?( method )
|
83
|
+
@data.send( method, *args )
|
84
|
+
else
|
85
|
+
super
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def respond_to?( method )
|
90
|
+
@data.key?( method ) || @data.key?( method.to_s )
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
63
94
|
def filter_it( data )
|
64
95
|
filter.options = self.class.config.single_options( @context_or_options )
|
65
96
|
data = data.dup
|
@@ -67,17 +98,15 @@ module Ixtlan
|
|
67
98
|
keeps = {}
|
68
99
|
( filter.options[ :keep ] || [] ).each do |k|
|
69
100
|
keep = data[ k.to_s ] || data[ k.to_sym ]
|
70
|
-
keeps[ k.to_s ] = data.delete( k.to_s ) ||
|
101
|
+
keeps[ k.to_s ] = data.delete( k.to_s ) ||
|
102
|
+
data.delete( k.to_sym ) unless keep.is_a? Hash
|
71
103
|
end
|
72
104
|
filtered_data = filter.filter( data )
|
73
105
|
( filter.options[ :keep ] || [] ).each do |k|
|
74
|
-
keeps[ k.to_s ] = filtered_data.delete( k.to_s ) ||
|
106
|
+
keeps[ k.to_s ] = filtered_data.delete( k.to_s ) ||
|
107
|
+
filtered_data.delete( k.to_sym ) unless keeps.member?( k.to_s )
|
75
108
|
end
|
76
|
-
|
77
|
-
end
|
78
|
-
|
79
|
-
def new( data )
|
80
|
-
@model_class.new( filter( data ) )
|
109
|
+
FilterResult.new( @model_class, filtered_data, keeps )
|
81
110
|
end
|
82
111
|
end
|
83
112
|
end
|
@@ -37,7 +37,7 @@ module Ixtlan
|
|
37
37
|
def initialize(model_or_models)
|
38
38
|
@model_or_models = model_or_models
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
def respond_to?(method)
|
42
42
|
@model_or_models.respond_to?(method)
|
43
43
|
end
|
@@ -45,7 +45,7 @@ module Ixtlan
|
|
45
45
|
def method_missing(method, *args, &block)
|
46
46
|
@model_or_models.send(method, *args, &block)
|
47
47
|
end
|
48
|
-
|
48
|
+
|
49
49
|
def add_custom_serializers(map)
|
50
50
|
filter.add_custom_serializers(map)
|
51
51
|
end
|
@@ -57,7 +57,7 @@ module Ixtlan
|
|
57
57
|
end
|
58
58
|
|
59
59
|
def filter
|
60
|
-
@filter ||=
|
60
|
+
@filter ||= ModelFilter.new
|
61
61
|
end
|
62
62
|
|
63
63
|
protected
|
@@ -119,7 +119,8 @@ module Ixtlan
|
|
119
119
|
private :setup_filter
|
120
120
|
|
121
121
|
def collection?
|
122
|
-
@is_collection ||= @model_or_models.respond_to?(:collect) &&
|
122
|
+
@is_collection ||= @model_or_models.respond_to?(:collect) &&
|
123
|
+
! @model_or_models.is_a?(Hash)
|
123
124
|
end
|
124
125
|
private :collection?
|
125
126
|
|
@@ -139,7 +140,7 @@ module Ixtlan
|
|
139
140
|
def to_yaml(options = nil)
|
140
141
|
to_hash(options).to_yaml
|
141
142
|
end
|
142
|
-
|
143
|
+
|
143
144
|
protected
|
144
145
|
|
145
146
|
def attr(model)
|
@@ -154,7 +155,7 @@ module Ixtlan
|
|
154
155
|
else
|
155
156
|
filter.filter( model ){ |model| attr(model) }
|
156
157
|
end
|
157
|
-
end
|
158
|
+
end
|
158
159
|
end
|
159
160
|
end
|
160
161
|
end
|
data/spec/hash_filter_spec.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
require 'ixtlan/babel/deserializer'
|
3
2
|
|
4
3
|
class Hash
|
5
4
|
def attributes
|
@@ -16,17 +15,17 @@ describe Ixtlan::Babel::HashFilter do
|
|
16
15
|
'id' => 987,
|
17
16
|
'name' => 'me and the corner',
|
18
17
|
'address' => { 'street' => 'Foo 12', 'zipcode' => '12345' },
|
19
|
-
'phone_numbers' => {
|
20
|
-
'prefix' => 12,
|
18
|
+
'phone_numbers' => {
|
19
|
+
'prefix' => 12,
|
21
20
|
'number' => '123',
|
22
21
|
'area' => { 'code' => '001', 'iso' => 'us'}
|
23
22
|
}
|
24
23
|
}
|
25
24
|
class Hash
|
26
|
-
def self.new(hash = nil, &block)
|
25
|
+
def self.new(hash = nil, &block)
|
27
26
|
if hash
|
28
27
|
self[hash]
|
29
|
-
else
|
28
|
+
else
|
30
29
|
super &block
|
31
30
|
end
|
32
31
|
end
|
@@ -35,7 +34,26 @@ describe Ixtlan::Babel::HashFilter do
|
|
35
34
|
end
|
36
35
|
|
37
36
|
let(:serializer) { Ixtlan::Babel::Serializer.new(data) }
|
38
|
-
let(:deserializer)
|
37
|
+
let(:deserializer) do
|
38
|
+
f = Ixtlan::Babel::HashFilter.new
|
39
|
+
def f.from_json( json, options = nil )
|
40
|
+
data = MultiJson.load(json)
|
41
|
+
self.options = options || {}
|
42
|
+
if data.is_a? Array
|
43
|
+
if filter.options[:root]
|
44
|
+
data.collect do |d|
|
45
|
+
Hash.new( self.filter( d[ self.options[:root] ] ) )
|
46
|
+
end
|
47
|
+
else
|
48
|
+
data.collect{ |d| Hash.new( self.filter( d ) ) }
|
49
|
+
end
|
50
|
+
else
|
51
|
+
data = data[ self.options[:root] ] if self.options[:root]
|
52
|
+
Hash.new( self.filter( data ) )
|
53
|
+
end
|
54
|
+
end
|
55
|
+
f
|
56
|
+
end
|
39
57
|
|
40
58
|
it 'should serialize and deserialize a hash' do
|
41
59
|
json = serializer.to_json
|
@@ -47,25 +65,26 @@ describe Ixtlan::Babel::HashFilter do
|
|
47
65
|
json = serializer.to_json :root => 'my'
|
48
66
|
result = deserializer.from_json(json, :root => 'my')
|
49
67
|
result.must_equal Hash['id' => data['id'], 'name' => data['name']]
|
50
|
-
end
|
68
|
+
end
|
51
69
|
|
52
70
|
it 'should serialize and deserialize a hash with include list' do
|
53
|
-
json = serializer.to_json(:include => ['address', 'phone_numbers'])
|
54
|
-
result = deserializer.from_json(json, :include =>
|
71
|
+
json = serializer.to_json(:include => ['address', 'phone_numbers'])
|
72
|
+
result = deserializer.from_json(json, :include =>
|
73
|
+
['address', 'phone_numbers'])
|
55
74
|
data['phone_numbers'].delete('area')
|
56
75
|
result.must_equal Hash[data]
|
57
76
|
end
|
58
77
|
|
59
78
|
it 'should serialize and deserialize a hash with except' do
|
60
|
-
json = serializer.to_json(:except => ['id'])
|
61
|
-
result = deserializer.from_json(json, :except => ['id'])
|
79
|
+
json = serializer.to_json(:except => ['id'])
|
80
|
+
result = deserializer.from_json(json, :except => ['id'])
|
62
81
|
result.must_equal Hash['name' => data['name']]
|
63
|
-
result = deserializer.from_json(json)
|
82
|
+
result = deserializer.from_json(json)
|
64
83
|
result.must_equal Hash['name' => data['name']]
|
65
84
|
end
|
66
85
|
|
67
86
|
it 'should serialize and deserialize a hash with only' do
|
68
|
-
json = serializer.to_json(:only => ['name'])
|
87
|
+
json = serializer.to_json(:only => ['name'])
|
69
88
|
result = deserializer.from_json(json, :only => ['name'])
|
70
89
|
result.must_equal Hash['name' => data['name']]
|
71
90
|
result = deserializer.from_json(json)
|
@@ -76,30 +95,37 @@ describe Ixtlan::Babel::HashFilter do
|
|
76
95
|
json = serializer.to_json(:include => { 'address' => {:only => ['street']}})
|
77
96
|
data.delete('phone_numbers')
|
78
97
|
data['address'].delete('zipcode')
|
79
|
-
result = deserializer.from_json(json, :include =>
|
98
|
+
result = deserializer.from_json(json, :include =>
|
99
|
+
{ 'address' => {:only => ['street']}})
|
80
100
|
result.must_equal data
|
81
101
|
result = deserializer.from_json(json, :include => ['address'])
|
82
102
|
result.must_equal data
|
83
103
|
end
|
84
104
|
|
85
105
|
it 'should serialize and deserialize a hash with nested except' do
|
86
|
-
json = serializer.to_json(:include => { 'address' =>
|
106
|
+
json = serializer.to_json(:include => { 'address' =>
|
107
|
+
{:except => ['zipcode']}})
|
87
108
|
data.delete('phone_numbers')
|
88
109
|
data['address'].delete('zipcode')
|
89
|
-
result = deserializer.from_json(json, :include => { 'address' =>
|
110
|
+
result = deserializer.from_json(json, :include => { 'address' =>
|
111
|
+
{:except => ['zipcode']}})
|
90
112
|
result.must_equal data
|
91
113
|
result = deserializer.from_json(json, :include => ['address'])
|
92
114
|
result.must_equal data
|
93
115
|
end
|
94
116
|
|
95
117
|
it 'should serialize and deserialize a hash with nested include' do
|
96
|
-
json = serializer.to_json(:include => { 'address' => {},
|
97
|
-
|
118
|
+
json = serializer.to_json(:include => { 'address' => {},
|
119
|
+
'phone_numbers' => { :include => ['area']}})
|
120
|
+
result = deserializer.from_json(json, :include => { 'address' => {},
|
121
|
+
'phone_numbers' => {
|
122
|
+
:include => ['area']}})
|
98
123
|
result.must_equal data
|
99
124
|
end
|
100
125
|
|
101
126
|
it 'should convert elements from arrays wth custom serializer' do
|
102
|
-
serializer.add_custom_serializers( "Symbol" =>
|
127
|
+
serializer.add_custom_serializers( "Symbol" =>
|
128
|
+
Proc.new {|v| v.to_s.capitalize } )
|
103
129
|
data['children_names'] = [:adi, :aromal, :shreedev]
|
104
130
|
d = serializer.to_hash(:include => [ :children_names ])
|
105
131
|
d[ "children_names"].must_equal( ["Adi", "Aromal", "Shreedev"] )
|
data/spec/model_filter_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'virtus'
|
3
|
-
require 'ixtlan/babel/
|
3
|
+
require 'ixtlan/babel/serializer'
|
4
4
|
|
5
5
|
class Address
|
6
6
|
include Virtus
|
@@ -12,7 +12,7 @@ class Area
|
|
12
12
|
include Virtus
|
13
13
|
|
14
14
|
attribute :code, String
|
15
|
-
attribute :iso, String
|
15
|
+
attribute :iso, String
|
16
16
|
end
|
17
17
|
class PhoneNumber
|
18
18
|
include Virtus
|
@@ -37,8 +37,8 @@ describe Ixtlan::Babel::ModelFilter do
|
|
37
37
|
:id => 987,
|
38
38
|
:name => 'me and the corner',
|
39
39
|
:address => Address.new( :street => 'Foo 12', :zipcode => '12345' ),
|
40
|
-
:phone_numbers => [PhoneNumber.new(
|
41
|
-
:prefix => 12,
|
40
|
+
:phone_numbers => [PhoneNumber.new(
|
41
|
+
:prefix => 12,
|
42
42
|
:number => '123',
|
43
43
|
:area => Area.new( :code => '001', :iso => 'us' )
|
44
44
|
)],
|
@@ -47,168 +47,84 @@ describe Ixtlan::Babel::ModelFilter do
|
|
47
47
|
end
|
48
48
|
|
49
49
|
let(:serializer) { Ixtlan::Babel::Serializer.new( person ) }
|
50
|
-
let(:deserializer) { Ixtlan::Babel::Deserializer.new( Person ) }
|
51
50
|
|
52
51
|
it 'should serialize and deserialize without root' do
|
53
52
|
json = serializer.to_json
|
54
|
-
result =
|
55
|
-
|
56
|
-
# travis produces [] and locally there is a nil - filter empty as well :(
|
57
|
-
attributes = result.attributes.delete_if { |k,v| v.nil? || v.empty? }
|
58
|
-
|
59
|
-
attributes.must_equal Hash[:id => person['id'], :name => person['name']]
|
53
|
+
result = MultiJson.load(json)
|
54
|
+
result.must_equal Hash['id' => person['id'], 'name' => person['name']]
|
60
55
|
end
|
61
56
|
|
62
57
|
it 'should serialize and deserialize with root' do
|
63
58
|
json = serializer.to_json :root => 'my'
|
64
|
-
result =
|
65
|
-
|
66
|
-
|
67
|
-
attributes = result.attributes.delete_if { |k,v| v.nil? || v.empty? }
|
68
|
-
|
69
|
-
attributes.must_equal Hash[:id => person['id'], :name => person['name']]
|
70
|
-
end
|
59
|
+
result = MultiJson.load(json)[ 'my' ]
|
60
|
+
result.must_equal Hash['id' => person['id'], 'name' => person['name']]
|
61
|
+
end
|
71
62
|
|
72
63
|
it 'should serialize and deserialize a hash with include list' do
|
73
|
-
json = serializer.to_json(:include => ['address', 'phone_numbers'])
|
74
|
-
result =
|
75
|
-
result.
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
64
|
+
json = serializer.to_json(:include => ['address', 'phone_numbers'])
|
65
|
+
result = MultiJson.load(json)
|
66
|
+
result.must_equal Hash[ "id"=>"987",
|
67
|
+
"name"=>"me and the corner",
|
68
|
+
"address"=>{
|
69
|
+
"street"=>"Foo 12",
|
70
|
+
"zipcode"=>"12345"
|
71
|
+
},
|
72
|
+
"phone_numbers"=> [ {"prefix"=>12,
|
73
|
+
"number"=>"123" } ] ]
|
82
74
|
end
|
83
75
|
|
84
76
|
it 'should serialize and deserialize with except' do
|
85
|
-
json = serializer.to_json(:except => ['id'])
|
86
|
-
result =
|
87
|
-
|
88
|
-
expected = Hash[:name => person['name'], :address=>nil, :phone_numbers=>[], :id => nil, :children_names => []]
|
89
|
-
|
90
|
-
# travis sees empty array and locally it is nil :(
|
91
|
-
result.phone_numbers ||= []
|
92
|
-
result.children_names ||= []
|
93
|
-
|
94
|
-
result.attributes.keys.dup.each do |k|
|
95
|
-
result.attributes[ k ].must_equal expected[ k ]
|
96
|
-
end
|
97
|
-
|
98
|
-
result = deserializer.from_json(json)
|
99
|
-
|
100
|
-
# travis sees empty array and locally it is nil :(
|
101
|
-
result.phone_numbers ||= []
|
102
|
-
result.children_names ||= []
|
103
|
-
|
104
|
-
result.attributes.keys.dup.each do |k|
|
105
|
-
result.attributes[ k ].must_equal expected[ k ]
|
106
|
-
end
|
77
|
+
json = serializer.to_json(:except => ['id'])
|
78
|
+
result = MultiJson.load(json)
|
79
|
+
result.must_equal Hash['name' => person['name']]
|
107
80
|
end
|
108
81
|
|
109
82
|
it 'should serialize and deserialize with only' do
|
110
|
-
json = serializer.to_json(:only => ['name'])
|
111
|
-
result =
|
112
|
-
|
113
|
-
# travis sees empty array and locally it is nil :(
|
114
|
-
result.phone_numbers ||= []
|
115
|
-
result.children_names ||= []
|
116
|
-
|
117
|
-
expected = Hash[:name => person['name'], :address=>nil, :phone_numbers=>[], :id => nil, :children_names => []]
|
118
|
-
result.attributes.keys.dup.each do |k|
|
119
|
-
result.attributes[ k ].must_equal expected[ k ]
|
120
|
-
end
|
121
|
-
|
122
|
-
result = deserializer.from_json(json)
|
123
|
-
|
124
|
-
# travis sees empty array and locally it is nil :(
|
125
|
-
result.phone_numbers ||= []
|
126
|
-
result.children_names ||= []
|
83
|
+
json = serializer.to_json(:only => ['name'])
|
84
|
+
result = MultiJson.load(json)
|
127
85
|
|
128
|
-
result.
|
129
|
-
result.attributes[ k ].must_equal expected[ k ]
|
130
|
-
end
|
86
|
+
result.must_equal Hash['name' => person['name']]
|
131
87
|
end
|
132
88
|
|
133
89
|
it 'should serialize and deserialize with nested only' do
|
134
90
|
json = serializer.to_json(:include => { 'address' => {:only => ['street']}})
|
135
|
-
result =
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
# travis produces [] and locally there is a nil :(
|
141
|
-
(result.phone_numbers || []).must_equal []
|
142
|
-
|
143
|
-
result.address.zipcode.must_be_nil
|
144
|
-
|
145
|
-
result.name.must_equal person.name
|
146
|
-
result.id.must_equal person.id
|
147
|
-
end
|
148
|
-
|
149
|
-
it 'should serialize and deserialize with nested only (array includes)' do
|
150
|
-
json = serializer.to_json(:include => { 'address' => {:only => ['street']}})
|
151
|
-
result = deserializer.from_json(json, :include => ['address'])
|
152
|
-
|
153
|
-
json['phone_numbers'].must_be_nil
|
154
|
-
json['address']['zipcode'].must_be_nil
|
155
|
-
|
156
|
-
# travis produces [] and locally there is a nil :(
|
157
|
-
(result.phone_numbers || []).must_equal []
|
158
|
-
|
159
|
-
result.address.zipcode.must_be_nil
|
160
|
-
|
161
|
-
result.name.must_equal person.name
|
162
|
-
result.id.must_equal person.id
|
91
|
+
result = MultiJson.load(json)
|
92
|
+
result.must_equal Hash[ "id"=>"987",
|
93
|
+
"name"=>"me and the corner",
|
94
|
+
"address"=>{ "street"=>"Foo 12" } ]
|
163
95
|
end
|
164
96
|
|
165
97
|
it 'should serialize and deserialize with nested except' do
|
166
|
-
json = serializer.to_json(:include =>
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
# travis produces [] and locally there is a nil :(
|
173
|
-
(result.phone_numbers || []).must_equal []
|
174
|
-
|
175
|
-
result.address.zipcode.must_be_nil
|
176
|
-
|
177
|
-
result.name.must_equal person.name
|
178
|
-
result.id.must_equal person.id
|
179
|
-
end
|
180
|
-
|
181
|
-
it 'should serialize and deserialize with nested except (array includes)' do
|
182
|
-
json = serializer.to_json(:include => { 'address' => {:except => ['zipcode']}})
|
183
|
-
result = deserializer.from_json(json, :include => ['address'])
|
184
|
-
|
185
|
-
json['phone_numbers'].must_be_nil
|
186
|
-
json['address']['zipcode'].must_be_nil
|
187
|
-
|
188
|
-
# travis produces [] and locally there is a nil :(
|
189
|
-
(result.phone_numbers || []).must_equal []
|
190
|
-
|
191
|
-
result.address.zipcode.must_be_nil
|
192
|
-
|
193
|
-
result.name.must_equal person.name
|
194
|
-
result.id.must_equal person.id
|
98
|
+
json = serializer.to_json(:include =>
|
99
|
+
{ 'address' => {:except => ['zipcode']}})
|
100
|
+
result = MultiJson.load(json)
|
101
|
+
result.must_equal Hash[ "id"=>"987",
|
102
|
+
"name"=>"me and the corner",
|
103
|
+
"address"=>{ "street"=>"Foo 12" } ]
|
195
104
|
end
|
196
105
|
|
197
106
|
it 'should serialize and deserialize with nested include' do
|
198
|
-
json = serializer.to_json(:include => {
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
result
|
203
|
-
result.
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
107
|
+
json = serializer.to_json( :include => {
|
108
|
+
'address' => {},
|
109
|
+
'phone_numbers' => { :include => ['area'] }
|
110
|
+
} )
|
111
|
+
result = MultiJson.load(json)
|
112
|
+
result.must_equal Hash[ "id"=>"987",
|
113
|
+
"name"=>"me and the corner",
|
114
|
+
"address"=>{ "street"=>"Foo 12",
|
115
|
+
"zipcode"=>"12345" },
|
116
|
+
"phone_numbers"=>[ { "prefix"=>12,
|
117
|
+
"number"=>"123",
|
118
|
+
"area"=>{
|
119
|
+
"code"=>"001",
|
120
|
+
"iso"=>"us"
|
121
|
+
}
|
122
|
+
} ] ]
|
208
123
|
end
|
209
124
|
|
210
125
|
it 'should convert elements from arrays wth custom serializer' do
|
211
|
-
serializer.add_custom_serializers( "Symbol" =>
|
126
|
+
serializer.add_custom_serializers( "Symbol" =>
|
127
|
+
Proc.new {|v| v.to_s.capitalize } )
|
212
128
|
data = serializer.to_hash(:include => [ :children_names ])
|
213
129
|
data[ "children_names"].must_equal( ["Adi", "Aromal", "Shreedev"] )
|
214
130
|
end
|