ixtlan-babel 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -22,14 +22,14 @@ module Ixtlan
22
22
  module Babel
23
23
  class Factory
24
24
 
25
- NANOSECONDS_IN_DAY = Rational(1, 86400*10**9)
25
+ NANOSECONDS_IN_DAY = 86400*10**6
26
26
 
27
27
  TIME_TO_S = Proc.new do |t|
28
28
  t.strftime('%Y-%m-%dT%H:%M:%S.') + ("%06d" % t.usec) + t.strftime('%z')
29
29
  end
30
30
 
31
31
  DATE_TIME_TO_S = Proc.new do |dt|
32
- dt.strftime('%Y-%m-%dT%H:%M:%S.') + ("%06d" % (dt.sec_fraction / NANOSECONDS_IN_DAY / 1000)) + dt.strftime('%z')
32
+ dt.strftime('%Y-%m-%dT%H:%M:%S.') + ("%06d" % (dt.sec_fraction * NANOSECONDS_IN_DAY ) ) + dt.strftime('%z')
33
33
  end
34
34
 
35
35
  DEFAULT_MAP = {
@@ -89,4 +89,4 @@ module Ixtlan
89
89
  end
90
90
  end
91
91
  end
92
- end
92
+ end
@@ -37,7 +37,7 @@ module Ixtlan
37
37
  if item.is_a?( Array ) || item.is_a?( Hash )
38
38
  filter_data( item, options )
39
39
  else
40
- item
40
+ serialize( item )
41
41
  end
42
42
  end
43
43
  end
@@ -59,4 +59,4 @@ module Ixtlan
59
59
  end
60
60
  end
61
61
  end
62
- end
62
+ end
@@ -39,7 +39,7 @@ module Ixtlan
39
39
  if i.respond_to? :attributes
40
40
  filter_data(i, block.call(i), options, &block)
41
41
  else
42
- i
42
+ serialize( i )
43
43
  end
44
44
  end
45
45
  end
@@ -70,4 +70,4 @@ module Ixtlan
70
70
  end
71
71
  end
72
72
  end
73
- end
73
+ end
@@ -59,14 +59,21 @@ module Ixtlan
59
59
  @context_or_options = context_or_options
60
60
  self
61
61
  end
62
+
62
63
  def filter_it( data )
63
64
  filter.options = self.class.config.single_options( @context_or_options )
65
+ data = data.dup
64
66
  data = data[ filter.options[ :root ] ] if filter.options[ :root ]
65
67
  keeps = {}
66
68
  ( filter.options[ :keep ] || [] ).each do |k|
67
- keeps[ k.to_s ] = data[ k.to_s ] || data[ k.to_sym ]
69
+ keep = data[ k.to_s ] || data[ k.to_sym ]
70
+ keeps[ k.to_s ] = data.delete( k.to_s ) || data.delete( k.to_sym ) unless keep.is_a? Hash
71
+ end
72
+ filtered_data = filter.filter( data )
73
+ ( filter.options[ :keep ] || [] ).each do |k|
74
+ keeps[ k.to_s ] = filtered_data.delete( k.to_s ) || filtered_data.delete( k.to_sym ) unless keeps.member?( k.to_s )
68
75
  end
69
- [ filter.filter( data ), keeps ]
76
+ [ filtered_data, keeps ]
70
77
  end
71
78
 
72
79
  def new( data )
@@ -74,4 +81,4 @@ module Ixtlan
74
81
  end
75
82
  end
76
83
  end
77
- end
84
+ end
@@ -0,0 +1,211 @@
1
+ require 'spec_helper'
2
+ require 'virtus'
3
+ require 'ixtlan/babel/deserializer'
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
+ # # let(:deserializer) { Ixtlan::Babel::Deserializer.new( Person ) }
51
+
52
+ # # it 'should serialize and deserialize without root' do
53
+ # # json = serializer.to_json
54
+ # # result = deserializer.from_json(json)
55
+
56
+ # # # travis produces [] and locally there is a nil - filter empty as well :(
57
+
58
+ attributes = result.attributes.delete_if { |k,v| v.nil? || v.empty? }
59
+
60
+ attributes.must_equal Hash[:id => person['id'], :name => person['name']]
61
+ end
62
+
63
+ it 'should serialize and deserialize with root' do
64
+ json = serializer.to_json :root => 'my'
65
+ result = deserializer.from_json(json, :root => 'my')
66
+
67
+ # travis produces [] and locally there is a nil - filter empty as well :(
68
+ attributes = result.attributes.delete_if { |k,v| v.nil? || v.empty? }
69
+
70
+ attributes.must_equal Hash[:id => person['id'], :name => person['name']]
71
+ end
72
+
73
+ it 'should serialize and deserialize a hash with include list' do
74
+ json = serializer.to_json(:include => ['address', 'phone_numbers'])
75
+ result = deserializer.from_json(json, :include => ['address', 'phone_numbers'])
76
+ result.object_id.wont_equal person.object_id
77
+ result.address.attributes.must_equal person.address.attributes
78
+ result.phone_numbers[0].area.must_be_nil
79
+ person.phone_numbers[0].area = nil
80
+ result.phone_numbers[0].attributes.must_equal person.phone_numbers[0].attributes
81
+ result.name.must_equal person.name
82
+ result.id.must_equal person.id
83
+ end
84
+
85
+ it 'should serialize and deserialize with except' do
86
+ json = serializer.to_json(:except => ['id'])
87
+ result = deserializer.from_json(json, :except => ['id'])
88
+
89
+ expected = Hash[:name => person['name'], :address=>nil, :phone_numbers=>[], :id => nil]
90
+
91
+ # travis sees empty array and locally it is nil :(
92
+ result.phone_numbers ||= []
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
+
103
+ result.attributes.keys.dup.each do |k|
104
+ result.attributes[ k ].must_equal expected[ k ]
105
+ end
106
+ end
107
+
108
+ it 'should serialize and deserialize with only' do
109
+ json = serializer.to_json(:only => ['name'])
110
+ result = deserializer.from_json(json, :only => ['name'])
111
+
112
+ # travis sees empty array and locally it is nil :(
113
+ result.phone_numbers ||= []
114
+
115
+ expected = Hash[:name => person['name'], :address=>nil, :phone_numbers=>[], :id => nil]
116
+ result.attributes.keys.dup.each do |k|
117
+ result.attributes[ k ].must_equal expected[ k ]
118
+ end
119
+
120
+ result = deserializer.from_json(json)
121
+
122
+ # travis sees empty array and locally it is nil :(
123
+ result.phone_numbers ||= []
124
+ result.attributes.keys.dup.each do |k|
125
+ result.attributes[ k ].must_equal expected[ k ]
126
+ end
127
+ end
128
+
129
+ it 'should serialize and deserialize with nested only' do
130
+ json = serializer.to_json(:include => { 'address' => {:only => ['street']}})
131
+ result = deserializer.from_json(json, :include => { 'address' => {:only => ['street']}})
132
+
133
+ json['phone_numbers'].must_be_nil
134
+ json['address']['zipcode'].must_be_nil
135
+
136
+ # travis produces [] and locally there is a nil :(
137
+ (result.phone_numbers || []).must_equal []
138
+
139
+ result.address.zipcode.must_be_nil
140
+
141
+ result.name.must_equal person.name
142
+ result.id.must_equal person.id
143
+ end
144
+
145
+ it 'should serialize and deserialize with nested only (array includes)' do
146
+ json = serializer.to_json(:include => { 'address' => {:only => ['street']}})
147
+ result = deserializer.from_json(json, :include => ['address'])
148
+
149
+ json['phone_numbers'].must_be_nil
150
+ json['address']['zipcode'].must_be_nil
151
+
152
+ # travis produces [] and locally there is a nil :(
153
+ (result.phone_numbers || []).must_equal []
154
+
155
+ result.address.zipcode.must_be_nil
156
+
157
+ result.name.must_equal person.name
158
+ result.id.must_equal person.id
159
+ end
160
+
161
+ it 'should serialize and deserialize with nested except' do
162
+ json = serializer.to_json(:include => { 'address' => {:except => ['zipcode']}})
163
+ result = deserializer.from_json(json, :include => { 'address' => {:except => ['zipcode']}})
164
+
165
+ json['phone_numbers'].must_be_nil
166
+ json['address']['zipcode'].must_be_nil
167
+
168
+ # travis produces [] and locally there is a nil :(
169
+ (result.phone_numbers || []).must_equal []
170
+
171
+ result.address.zipcode.must_be_nil
172
+
173
+ result.name.must_equal person.name
174
+ result.id.must_equal person.id
175
+ end
176
+
177
+ it 'should serialize and deserialize with nested except (array includes)' do
178
+ json = serializer.to_json(:include => { 'address' => {:except => ['zipcode']}})
179
+ result = deserializer.from_json(json, :include => ['address'])
180
+
181
+ json['phone_numbers'].must_be_nil
182
+ json['address']['zipcode'].must_be_nil
183
+
184
+ # travis produces [] and locally there is a nil :(
185
+ (result.phone_numbers || []).must_equal []
186
+
187
+ result.address.zipcode.must_be_nil
188
+
189
+ result.name.must_equal person.name
190
+ result.id.must_equal person.id
191
+ end
192
+
193
+ it 'should serialize and deserialize with nested include' do
194
+ json = serializer.to_json(:include => { 'address' => {}, 'phone_numbers' => { :include => ['area']}})
195
+ result = deserializer.from_json(json, :include => { 'address' => {}, 'phone_numbers' => { :include => ['area']}})
196
+
197
+ result.object_id.wont_equal person.object_id
198
+ result.address.attributes.must_equal person.address.attributes
199
+ result.phone_numbers[0].area.attributes.must_equal person.phone_numbers[0].area.attributes
200
+ result.phone_numbers[0].prefix.must_equal person.phone_numbers[0].prefix
201
+ result.phone_numbers[0].number.must_equal person.phone_numbers[0].number
202
+ result.name.must_equal person.name
203
+ result.id.must_equal person.id
204
+ end
205
+
206
+ it 'should convert elements from arrays wth custom serializer' do
207
+ serializer.add_custom_serializers( "Symbol" => Proc.new {|v| v.to_s.capitalize } )
208
+ data = serializer.to_hash(:include => [ :children_names ])
209
+ data[ "children_names"].must_equal( ["Adi", "Aromal", "Shreedev"] )
210
+ end
211
+ end
@@ -97,4 +97,11 @@ describe Ixtlan::Babel::HashFilter do
97
97
  result = deserializer.from_json(json, :include => { 'address' => {}, 'phone_numbers' => { :include => ['area']}})
98
98
  result.must_equal data
99
99
  end
100
+
101
+ it 'should convert elements from arrays wth custom serializer' do
102
+ serializer.add_custom_serializers( "Symbol" => Proc.new {|v| v.to_s.capitalize } )
103
+ data['children_names'] = [:adi, :aromal, :shreedev]
104
+ d = serializer.to_hash(:include => [ :children_names ])
105
+ d[ "children_names"].must_equal( ["Adi", "Aromal", "Shreedev"] )
106
+ end
100
107
  end
@@ -28,6 +28,7 @@ class Person
28
28
  attribute :name, String
29
29
  attribute :address, Address
30
30
  attribute :phone_numbers, Array[PhoneNumber]
31
+ attribute :children_names, Array[Symbol]
31
32
  end
32
33
 
33
34
  describe Ixtlan::Babel::ModelFilter do
@@ -40,7 +41,8 @@ describe Ixtlan::Babel::ModelFilter do
40
41
  :prefix => 12,
41
42
  :number => '123',
42
43
  :area => Area.new( :code => '001', :iso => 'us' )
43
- )]
44
+ )],
45
+ :children_names => [:adi, :aromal, :shreedev]
44
46
  )
45
47
  end
46
48
 
@@ -199,4 +201,10 @@ describe Ixtlan::Babel::ModelFilter do
199
201
  result.name.must_equal person.name
200
202
  result.id.must_equal person.id
201
203
  end
204
+
205
+ it 'should convert elements from arrays wth custom serializer' do
206
+ serializer.add_custom_serializers( "Symbol" => Proc.new {|v| v.to_s.capitalize } )
207
+ data = serializer.to_hash(:include => [ :children_names ])
208
+ data[ "children_names"].must_equal( ["Adi", "Aromal", "Shreedev"] )
209
+ end
202
210
  end
@@ -41,7 +41,7 @@ describe Ixtlan::Babel::ParamsFilter do
41
41
 
42
42
  it 'should filter a hash with keep' do
43
43
  json = filter.use( :keep => ['id'] ).filter_it( data )
44
- json[0].must_equal Hash[ 'id' => data['id'], 'name' => data['name'] ]
44
+ json[0].must_equal Hash[ 'name' => data['name'] ]
45
45
  json[1].must_equal Hash[ 'id' => data['id'] ]
46
46
  end
47
47
 
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: ixtlan-babel
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.1
5
+ version: 0.3.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Christian Meier
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-28 00:00:00.000000000 Z
12
+ date: 2013-02-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -127,6 +127,7 @@ files:
127
127
  - spec/hash_filter_spec.rb~
128
128
  - spec/spec_helper.rb
129
129
  - spec/model_filter_with_methods_spec.rb
130
+ - spec/#model_filter_spec.rb#
130
131
  - spec/params_filter_spec.rb~
131
132
  - MIT-LICENSE
132
133
  - README.md