ixtlan-babel 0.5.0 → 0.7.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.
@@ -1,131 +0,0 @@
1
- require 'spec_helper'
2
- require 'virtus'
3
- require 'ixtlan/babel/serializer'
4
-
5
- class Address
6
- include Virtus
7
-
8
- attribute :street, String
9
- attribute :zipcode, String
10
- end
11
- class Area
12
- include Virtus
13
-
14
- attribute :code, String
15
- attribute :iso, String
16
- end
17
- class PhoneNumber
18
- include Virtus
19
-
20
- attribute :prefix, Integer
21
- attribute :number, String
22
- attribute :area, Area
23
- end
24
- class Person
25
- include Virtus
26
-
27
- attribute :id, String
28
- attribute :name, String
29
- attribute :address, Address
30
- attribute :phone_numbers, Array[PhoneNumber]
31
- attribute :children_names, Array[Symbol]
32
- end
33
-
34
- describe Ixtlan::Babel::ModelFilter do
35
- let( :person ) do
36
- Person.new(
37
- :id => 987,
38
- :name => 'me and the corner',
39
- :address => Address.new( :street => 'Foo 12', :zipcode => '12345' ),
40
- :phone_numbers => [PhoneNumber.new(
41
- :prefix => 12,
42
- :number => '123',
43
- :area => Area.new( :code => '001', :iso => 'us' )
44
- )],
45
- :children_names => [:adi, :aromal, :shreedev]
46
- )
47
- end
48
-
49
- let(:serializer) { Ixtlan::Babel::Serializer.new( person ) }
50
-
51
- it 'should serialize and deserialize without root' do
52
- json = serializer.to_json
53
- result = MultiJson.load(json)
54
- result.must_equal Hash['id' => person['id'], 'name' => person['name']]
55
- end
56
-
57
- it 'should serialize and deserialize with root' do
58
- json = serializer.to_json :root => 'my'
59
- result = MultiJson.load(json)[ 'my' ]
60
- result.must_equal Hash['id' => person['id'], 'name' => person['name']]
61
- end
62
-
63
- it 'should serialize and deserialize a hash with include list' do
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" } ] ]
74
- end
75
-
76
- it 'should serialize and deserialize with except' do
77
- json = serializer.to_json(:except => ['id'])
78
- result = MultiJson.load(json)
79
- result.must_equal Hash['name' => person['name']]
80
- end
81
-
82
- it 'should serialize and deserialize with only' do
83
- json = serializer.to_json(:only => ['name'])
84
- result = MultiJson.load(json)
85
-
86
- result.must_equal Hash['name' => person['name']]
87
- end
88
-
89
- it 'should serialize and deserialize with nested only' do
90
- json = serializer.to_json(:include => { 'address' => {:only => ['street']}})
91
- result = MultiJson.load(json)
92
- result.must_equal Hash[ "id"=>"987",
93
- "name"=>"me and the corner",
94
- "address"=>{ "street"=>"Foo 12" } ]
95
- end
96
-
97
- it 'should serialize and deserialize with nested except' do
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" } ]
104
- end
105
-
106
- it 'should serialize and deserialize with nested include' do
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
- } ] ]
123
- end
124
-
125
- it 'should convert elements from arrays wth custom serializer' do
126
- serializer.add_custom_serializers( "Symbol" =>
127
- Proc.new {|v| v.to_s.capitalize } )
128
- data = serializer.to_hash(:include => [ :children_names ])
129
- data[ "children_names"].must_equal( ["Adi", "Aromal", "Shreedev"] )
130
- end
131
- end
@@ -1,173 +0,0 @@
1
- require 'spec_helper'
2
- require 'virtus'
3
-
4
- class Address2
5
- include Virtus
6
-
7
- attribute :street, String
8
- attribute :zipcode, String
9
- end
10
- class Area2
11
- include Virtus
12
-
13
- attribute :code, String
14
- attribute :iso, String
15
- end
16
- class PhoneNumber2
17
- include Virtus
18
-
19
- attribute :prefix, Integer
20
- attribute :number, String
21
- attribute :area, Area2
22
- end
23
- class Person2
24
- include Virtus
25
-
26
- attribute :id, String
27
- attribute :name, String
28
- attribute :address, Address2
29
-
30
- attr_accessor :phone_numbers, :age, :children_names
31
-
32
- def phone_numbers
33
- @phone_numbers ||= [PhoneNumber2.new(
34
- :prefix => 12,
35
- :number => '123',
36
- :area => Area2.new( :code => '001', :iso => 'us' ) )]
37
- end
38
-
39
- def age
40
- @age ||= 123
41
- end
42
-
43
- def children_names
44
- @children_names ||= ['anna', 'jack', 'rama', 'mia']
45
- end
46
-
47
- def children_ages
48
- @children_ages ||= [12, 3, 6, 9]
49
- end
50
- end
51
-
52
- describe Ixtlan::Babel::ModelFilter.to_s + ':with_methods' do
53
- let( :person ) do
54
- Person2.new( :id => 987,
55
- :name => 'me and the corner',
56
- :address => Address2.new( :street => 'Foo 12',
57
- :zipcode => '12345' ) )
58
- end
59
-
60
- let( :serializer ) { factory.new_serializer( person ) }
61
-
62
- let( :factory ) { Ixtlan::Babel::Factory.new }
63
-
64
- it 'should serialize and deserialize with methods' do
65
- class Person2Serializer < Ixtlan::Babel::Serializer
66
- add_context( :nested ) do
67
- only :id, :name, :age, :children_names, :children_ages
68
- end
69
- end
70
- json = serializer.use( :nested ).to_json
71
- result = MultiJson.load(json)
72
- result.must_equal Hash[ "id"=>"987",
73
- "name"=>"me and the corner",
74
- "age"=>123,
75
- "children_names"=> [ "anna",
76
- "jack",
77
- "rama",
78
- "mia" ],
79
- "children_ages"=>[ 12, 3, 6, 9 ] ]
80
- end
81
-
82
- it 'should serialize and deserialize without root' do
83
- class Person2Serializer < Ixtlan::Babel::Serializer
84
- add_context( :plain ) do
85
- only :id, :name
86
- end
87
- end
88
- json = serializer.use( :plain ).to_json
89
- result = MultiJson.load(json)
90
- result.must_equal Hash[ "id"=>"987", "name"=>"me and the corner" ]
91
- end
92
-
93
- it 'should serialize and deserialize with root' do
94
- class Person2Serializer < Ixtlan::Babel::Serializer
95
- add_context( :root ) do
96
- root 'my'
97
- only :id, :name
98
- end
99
- end
100
- json = serializer.use( :root ).to_json
101
- result = MultiJson.load(json)[ 'my' ]
102
- result.must_equal Hash[ "id"=>"987", "name"=>"me and the corner" ]
103
- end
104
-
105
- it 'should serialize and deserialize a hash with include list' do
106
- class Person2Serializer < Ixtlan::Babel::Serializer
107
- add_context( :deep_nested) do
108
- only( :id, :name,
109
- :address => only( :street, :zipcode ),
110
- :phone_numbers => only( :prefix, :number ) )
111
- end
112
- end
113
- json = serializer.use( :deep_nested ).to_json
114
- result = MultiJson.load(json)
115
- result.must_equal Hash[ "id"=>"987", "name"=>"me and the corner" ,
116
- "address"=> {
117
- "street"=>"Foo 12",
118
- "zipcode"=>"12345"
119
- },
120
- "phone_numbers"=> [ { "prefix"=>12,
121
- "number"=>"123" } ] ]
122
- end
123
-
124
- it 'should serialize and deserialize with only' do
125
- class Person2Serializer < Ixtlan::Babel::Serializer
126
- add_context( :only ) do
127
- only :name
128
- end
129
- end
130
- json = serializer.use( :only ).to_json
131
- result = MultiJson.load(json)
132
- result.must_equal Hash[ "name"=>"me and the corner" ]
133
- end
134
-
135
- it 'should serialize and deserialize with nested only' do
136
- class Person2Serializer < Ixtlan::Babel::Serializer
137
- add_context( :nested_only ) do
138
- only :address => only( :street )
139
- end
140
- end
141
- json = serializer.use( :nested_only ).to_json
142
- result = MultiJson.load(json)
143
- result.must_equal Hash[ "address"=> {
144
- "street"=>"Foo 12"
145
- } ]
146
- end
147
-
148
- it 'should serialize and deserialize with nested include' do
149
- class Person2Serializer < Ixtlan::Babel::Serializer
150
- add_context( :nested_deep ) do
151
- only( :id, :name,
152
- :address => only( :street, :zipcode ),
153
- :phone_numbers => only( :prefix,
154
- :number,
155
- :area => only( :code, :iso) ) )
156
- end
157
- end
158
- json = serializer.use( :nested_deep ).to_json
159
- result = MultiJson.load(json)
160
- result.must_equal Hash[ "id"=>"987", "name"=>"me and the corner" ,
161
- "address"=> {
162
- "street"=>"Foo 12",
163
- "zipcode"=>"12345"
164
- },
165
- "phone_numbers"=> [ { "prefix"=>12,
166
- "number"=>"123",
167
- "area"=> {
168
- "code"=>"001",
169
- "iso"=>"us"
170
- }
171
- } ] ]
172
- end
173
- end
@@ -1,151 +0,0 @@
1
- require 'spec_helper'
2
- require 'virtus'
3
-
4
- class Address2
5
- include Virtus
6
-
7
- attribute :street, String
8
- attribute :zipcode, String
9
- end
10
- class Area2
11
- include Virtus
12
-
13
- attribute :code, String
14
- attribute :iso, String
15
- end
16
- class PhoneNumber2
17
- include Virtus
18
-
19
- attribute :prefix, Integer
20
- attribute :number, String
21
- attribute :area, Area2
22
- end
23
- class Person2
24
- include Virtus
25
-
26
- attribute :id, String
27
- attribute :name, String
28
- attribute :address, Address2
29
-
30
- attr_accessor :phone_numbers, :age, :children_names
31
-
32
- def phone_numbers
33
- @phone_numbers ||= [PhoneNumber2.new(
34
- :prefix => 12,
35
- :number => '123',
36
- :area => Area2.new( :code => '001', :iso => 'us' ) )]
37
- end
38
-
39
- def age
40
- @age ||= 123
41
- end
42
-
43
- def children_names
44
- @children_names ||= ['anna', 'jack', 'rama', 'mia']
45
- end
46
-
47
- def children_ages
48
- @children_ages ||= [12, 3, 6, 9]
49
- end
50
- end
51
-
52
- describe Ixtlan::Babel::ModelFilter.to_s + ':with_methods' do
53
- let( :person ) do
54
- Person2.new( :id => 987,
55
- :name => 'me and the corner',
56
- :address => Address2.new( :street => 'Foo 12',
57
- :zipcode => '12345' ) )
58
- end
59
-
60
- let(:serializer) { Ixtlan::Babel::Serializer.new( person ) }
61
-
62
- it 'should serialize and deserialize with methods' do
63
- json = serializer.to_json( :include =>
64
- [:age, :children_names, :children_ages])
65
- result = MultiJson.load(json)
66
- result.must_equal Hash[ "id"=>"987",
67
- "name"=>"me and the corner",
68
- "age"=>123,
69
- "children_names"=> [ "anna",
70
- "jack",
71
- "rama",
72
- "mia" ],
73
- "children_ages"=>[ 12, 3, 6, 9 ] ]
74
- end
75
-
76
- it 'should serialize and deserialize without root' do
77
- json = serializer.to_json
78
- result = MultiJson.load(json)
79
- result.must_equal Hash[ "id"=>"987", "name"=>"me and the corner" ]
80
- end
81
-
82
- it 'should serialize and deserialize with root' do
83
- json = serializer.to_json :root => 'my'
84
- result = MultiJson.load(json)[ 'my' ]
85
- result.must_equal Hash[ "id"=>"987", "name"=>"me and the corner" ]
86
- end
87
-
88
- it 'should serialize and deserialize a hash with include list' do
89
- json = serializer.to_json(:include => ['address', 'phone_numbers'])
90
- result = MultiJson.load(json)
91
- result.must_equal Hash[ "id"=>"987", "name"=>"me and the corner" ,
92
- "address"=> {
93
- "street"=>"Foo 12",
94
- "zipcode"=>"12345"
95
- },
96
- "phone_numbers"=> [ { "prefix"=>12,
97
- "number"=>"123" } ] ]
98
- end
99
-
100
- it 'shouldserialize and deserialize with except' do
101
- json = serializer.to_json(:except => ['id'])
102
- result = MultiJson.load(json)
103
- result.must_equal Hash[ "name"=>"me and the corner" ]
104
- end
105
-
106
- it 'should serialize and deserialize with only' do
107
- json = serializer.to_json(:only => ['name'])
108
- result = MultiJson.load(json)
109
- result.must_equal Hash[ "name"=>"me and the corner" ]
110
- end
111
-
112
- it 'should serialize and deserialize with nested only' do
113
- json = serializer.to_json(:include => { 'address' => {
114
- :only => ['street'] } } )
115
- result = MultiJson.load(json)
116
- result.must_equal Hash[ "id"=>"987", "name"=>"me and the corner" ,
117
- "address"=> {
118
- "street"=>"Foo 12"
119
- } ]
120
- end
121
-
122
- it 'should serialize and deserialize with nested except' do
123
- json = serializer.to_json(:include => {
124
- 'address' => {:except => ['zipcode'] } } )
125
- result = MultiJson.load(json)
126
- result.must_equal Hash[ "id"=>"987", "name"=>"me and the corner" ,
127
- "address"=> {
128
- "street"=>"Foo 12"
129
- } ]
130
- end
131
-
132
- it 'should serialize and deserialize with nested include' do
133
- json = serializer.to_json(:include => {
134
- 'address' => {},
135
- 'phone_numbers' => { :include => ['area'] }
136
- } )
137
- result = MultiJson.load(json)
138
- result.must_equal Hash[ "id"=>"987", "name"=>"me and the corner" ,
139
- "address"=> {
140
- "street"=>"Foo 12",
141
- "zipcode"=>"12345"
142
- },
143
- "phone_numbers"=> [ { "prefix"=>12,
144
- "number"=>"123",
145
- "area"=> {
146
- "code"=>"001",
147
- "iso"=>"us"
148
- }
149
- } ] ]
150
- end
151
- end