prepor-beefcake 1.0.0 → 1.1.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c84028ca26869888306b5bf5c48b48dfb79909b2
4
- data.tar.gz: 7396b01b5432a3452ec5d59b3f19e3b140c66e19
3
+ metadata.gz: 915e8345407108ce58720d3ad23e18f213bf958e
4
+ data.tar.gz: 134cfad1c7d35baa319d865f8746b8a39b85aea5
5
5
  SHA512:
6
- metadata.gz: ffea398a17e0efa2bf8e6278c895caea789a086aff759ebb06bd57f25fff28da763fce39ac618c2cc13a6b814897079257322d6973bf330bc8e1ade0953a980a
7
- data.tar.gz: 719b223605e7c5acaa0b795df704caf16576478cea4dd870225f7118d31613ac171f9ca62601fc242d0ba91d33942e9c919451d486038a6059f80defc8db4e67
6
+ metadata.gz: 8fe0fad9ce257efb62ea6be7709fdbeee3121e0868213ab33765dbe7131d16a5ace2d48529815d8268f112304e3eedb9ddf9f0410e5613f4a7184fb21d2b8f8e
7
+ data.tar.gz: cf2294ec52db492116034ba0290b0c2def5075f1ac7c9c7c44ce28ac7f68808b3928b518b1b471e66841f8c07e281b2428cfb29921c618ce8e58d477b9a685d5
data/.travis.yml CHANGED
@@ -3,6 +3,13 @@ rvm:
3
3
  - 1.9.3
4
4
  - 2.0.0
5
5
  - 2.1.0
6
- - 2.1.1
7
- - 2.1.2
6
+ - 2.1.5
7
+ - 2.2.0
8
8
  - jruby-19mode
9
+ - rbx-2.2.7
10
+ - ruby-head
11
+ - jruby-head
12
+ matrix:
13
+ allow_failures:
14
+ - rvm: jruby-head
15
+ - rvm: ruby-head
data/lib/beefcake.rb CHANGED
@@ -33,6 +33,18 @@ module Beefcake
33
33
  def <=>(o)
34
34
  fn <=> o.fn
35
35
  end
36
+
37
+ def same_type?(obj)
38
+ type == obj
39
+ end
40
+
41
+ def is_protobuf?
42
+ type.is_a?(Class) and type.include?(Beefcake::Message)
43
+ end
44
+
45
+ def required? ; rule == :required end
46
+ def repeated? ; rule == :repeated end
47
+ def optional? ; rule == :optional end
36
48
  end
37
49
 
38
50
 
@@ -185,7 +197,8 @@ module Beefcake
185
197
  end
186
198
  elsif fld.rule == :repeated
187
199
  val = buf.read(fld.type)
188
- (o[fld.name] ||= []) << val
200
+ o[fld.name] ||= []
201
+ o[fld.name] << val
189
202
  else
190
203
  val = buf.read(fld.type)
191
204
  o[fld.name] = val
@@ -224,10 +237,66 @@ module Beefcake
224
237
  o.send(:include, Encode)
225
238
  end
226
239
 
240
+ # (see #assign)
227
241
  def initialize(attrs={})
242
+ assign attrs
243
+ end
244
+
245
+ # Handles filling a protobuf message from a hash. Embedded messages can
246
+ # be passed in two ways, by a pure hash or as an instance of embedded class(es).
247
+ #
248
+ # @example By a pure hash.
249
+ # {:field1 => 2, :embedded => {:embedded_f1 => 'lala'}}
250
+ #
251
+ # @example Repeated embedded message by a pure hash.
252
+ # {:field1 => 2, :embedded => [
253
+ # {:embedded_f1 => 'lala'},
254
+ # {:embedded_f1 => 'lulu'}
255
+ # ]}
256
+ #
257
+ # @example As an instance of embedded class.
258
+ # {:field1 => 2, :embedded => EmbeddedMsg.new({:embedded_f1 => 'lala'})}
259
+ #
260
+ # @param [Hash] data to fill a protobuf message with.
261
+ def assign(attrs)
228
262
  __beefcake_fields__.values.each do |fld|
229
- self[fld.name] = attrs[fld.name]
263
+ attribute = attrs[fld.name]
264
+
265
+ if attribute.nil?
266
+ self[fld.name] = nil
267
+ next
268
+ end
269
+
270
+ unless fld.is_protobuf?
271
+ self[fld.name] = attribute
272
+ next
273
+ end
274
+
275
+ if fld.repeated? && attribute.is_a?(Hash)
276
+ self[fld.name] = fld.type.new(attribute)
277
+ next
278
+ end
279
+
280
+ if fld.repeated? && attribute.is_a?(fld.type)
281
+ self[fld.name] = [attribute]
282
+ next
283
+ end
284
+
285
+ if fld.repeated?
286
+ self[fld.name] = attribute.map do |i|
287
+ fld.same_type?(i) ? i : fld.type.new(i)
288
+ end
289
+ next
290
+ end
291
+
292
+ if fld.same_type? attribute
293
+ self[fld.name] = attribute
294
+ next
295
+ end
296
+
297
+ self[fld.name] = fld.type.new(attribute)
230
298
  end
299
+ self
231
300
  end
232
301
 
233
302
  def __beefcake_fields__
@@ -270,10 +339,22 @@ module Beefcake
270
339
 
271
340
  def to_hash
272
341
  __beefcake_fields__.values.inject({}) do |h, fld|
273
- value = self[fld.name]
274
- unless value.nil?
275
- h[fld.name] = value
276
- end
342
+ v = self[fld.name]
343
+ next h if v.nil?
344
+
345
+ h[fld.name] =
346
+ case
347
+ when v.respond_to?(:to_hash)
348
+ # A nested protobuf message, so let's call its 'to_hash' method.
349
+ v.to_hash
350
+ when v.is_a?(Array)
351
+ # There can be two field types stored in array.
352
+ # Primitive type or nested another protobuf message.
353
+ # The later one has got a 'to_hash' method.
354
+ v.map { |i| i.respond_to?(:to_hash) ? i.to_hash : i }
355
+ else
356
+ v
357
+ end
277
358
  h
278
359
  end
279
360
  end
@@ -1,3 +1,3 @@
1
1
  module Beefcake
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0.pre1"
3
3
  end
data/test/message_test.rb CHANGED
@@ -102,6 +102,70 @@ class FieldsMessage
102
102
  repeated :fields, :string, 1
103
103
  end
104
104
 
105
+ class FieldTest < Minitest::Test
106
+ FIELD = Beefcake::Message::Field
107
+
108
+ def test_required?
109
+ f = FIELD.new :required, :field1, :string, 1
110
+ assert f.required?
111
+ refute f.optional?
112
+ refute f.repeated?
113
+
114
+ f = FIELD.new :optional, :field1, :int32, 1
115
+ refute f.required?
116
+
117
+ f = FIELD.new :repeated, :field1, :int32, 1
118
+ refute f.required?
119
+ end
120
+
121
+ def test_optional?
122
+ f = FIELD.new :optional, :field1, :string, 1
123
+ assert f.optional?
124
+ refute f.required?
125
+ refute f.repeated?
126
+
127
+ f = FIELD.new :required, :field1, :int32, 1
128
+ refute f.optional?
129
+
130
+ f = FIELD.new :repeated, :field1, :int32, 1
131
+ refute f.optional?
132
+ end
133
+
134
+ def test_repeated?
135
+ f = FIELD.new :repeated, :field1, :string, 1
136
+ assert f.repeated?
137
+ refute f.required?
138
+ refute f.optional?
139
+
140
+ f = FIELD.new :optional, :field1, :int32, 1
141
+ refute f.repeated?
142
+
143
+ f = FIELD.new :required, :field1, :int32, 1
144
+ refute f.optional?
145
+ end
146
+
147
+ def test_same_type
148
+ f = FIELD.new(:required, :field1, :int32, 1)
149
+ assert f.same_type?(:int32)
150
+ refute f.same_type?(:int64)
151
+ refute f.same_type?(SimpleMessage)
152
+
153
+ f = FIELD.new(:required, :field1, SimpleMessage, 1)
154
+ assert f.same_type?(SimpleMessage)
155
+ refute f.same_type?(:int32)
156
+ refute f.same_type?(:string)
157
+ refute f.same_type?(NumericsMessage)
158
+ end
159
+
160
+ def test_is_protobuf?
161
+ f = FIELD.new(:required, :field1, SimpleMessage, 1)
162
+ assert f.is_protobuf?
163
+
164
+ f = FIELD.new(:required, :field1, :string, 1)
165
+ refute f.is_protobuf?
166
+ end
167
+ end
168
+
105
169
  class MessageTest < Minitest::Test
106
170
  B = Beefcake::Buffer
107
171
 
@@ -163,6 +227,12 @@ class MessageTest < Minitest::Test
163
227
  )
164
228
 
165
229
  assert_equal buf2.to_s, msg.encode.to_s
230
+
231
+ msg = CompositeMessage.new(
232
+ :encodable => {:a => 123}
233
+ )
234
+ assert_equal buf2.to_s, msg.encode.to_s
235
+
166
236
  end
167
237
 
168
238
  def test_encode_to_string
@@ -196,6 +266,9 @@ class MessageTest < Minitest::Test
196
266
 
197
267
  msg = EnumsMessage.new :a => EnumsMessage::X::A
198
268
  assert_equal "\b\001", msg.encode.to_s
269
+
270
+ msg = EnumsMessage.new :a => 1
271
+ assert_equal "\b\001", msg.encode.to_s
199
272
  end
200
273
 
201
274
  def test_encode_invalid_enum_value
@@ -269,6 +342,16 @@ class MessageTest < Minitest::Test
269
342
  msg.encode.to_s
270
343
  end
271
344
 
345
+ def test_repeated_with_hash
346
+ before = { simple: { b: 'hello' } }
347
+ RepeatedNestedMessage.new before
348
+ end
349
+
350
+ def test_repeated_with_scalar
351
+ inner = SimpleMessage.new(b: 'hello')
352
+ RepeatedNestedMessage.new simple: inner
353
+ end
354
+
272
355
  ## Decoding
273
356
  def test_decode_numerics
274
357
  msg = NumericsMessage.new({
@@ -361,6 +444,16 @@ class MessageTest < Minitest::Test
361
444
  assert_equal 2, got.simple.size
362
445
  assert_equal 1, got.simple[0].a
363
446
  assert_equal "hello", got.simple[1].b
447
+
448
+ simple_pure = [
449
+ {:a => 1},
450
+ {:b => "hello"}
451
+ ]
452
+ msg = RepeatedNestedMessage.new(:simple => simple_pure).encode
453
+ got = RepeatedNestedMessage.decode(msg)
454
+ assert_equal 2, got.simple.size
455
+ assert_equal 1, got.simple[0].a
456
+ assert_equal "hello", got.simple[1].b
364
457
  end
365
458
 
366
459
  def test_equality
@@ -409,7 +502,23 @@ class MessageTest < Minitest::Test
409
502
  def test_to_hash
410
503
  msg = SimpleMessage.new :a => 1
411
504
  exp = { :a => 1 }
412
- assert_equal(exp, msg.to_hash)
505
+ assert_equal exp, msg.to_hash
506
+
507
+ msg = RepeatedNestedMessage.new(
508
+ :simple => [
509
+ SimpleMessage.new(:a => 1),
510
+ SimpleMessage.new(:b => 'abc dfg'),
511
+ SimpleMessage.new(:a => 2, :b => 'ijk lmn')
512
+ ]
513
+ )
514
+ exp = {
515
+ :simple =>[
516
+ {:a => 1},
517
+ {:b => 'abc dfg'},
518
+ {:a => 2, :b => 'ijk lmn'}
519
+ ]
520
+ }
521
+ assert_equal exp, msg.to_hash
413
522
  end
414
523
 
415
524
  def test_duplicate_index
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prepor-beefcake
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0.pre1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blake Mizerany
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-04-04 00:00:00.000000000 Z
13
+ date: 2015-04-03 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -89,9 +89,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
89
89
  version: 1.9.3
90
90
  required_rubygems_version: !ruby/object:Gem::Requirement
91
91
  requirements:
92
- - - ">="
92
+ - - ">"
93
93
  - !ruby/object:Gem::Version
94
- version: '0'
94
+ version: 1.3.1
95
95
  requirements: []
96
96
  rubyforge_project:
97
97
  rubygems_version: 2.4.3