active_remote 2.0.0 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/active_remote/serializers/protobuf.rb +41 -2
- data/lib/active_remote/version.rb +1 -1
- data/spec/lib/active_remote/serializers/protobuf_spec.rb +33 -0
- data/spec/support/definitions/serializer.proto +17 -0
- data/spec/support/protobuf.rb +1 -0
- data/spec/support/protobuf/serializer.pb.rb +33 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 75e05a76039dc8d851f60fce3e0ccf7f0a0b9640
|
4
|
+
data.tar.gz: 39a54ee4bfc9f1fc2b5ed191c3a40b6dfa2e910b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 861f57dbc0304deb5534596447c517169529c8738d128d2c29f6d170b43f23abbb101975979f019eb6df836706a9d0d6c5e9c2bca840eb60c5dd15f73603393b
|
7
|
+
data.tar.gz: d57cde44a3b9a2cff12db7c0ce34aa4aa78a75f17b6eaeb1ed54de3ee99d8e75a0219e3f885cbc2a191b247855fc5171cd6a063c0f6849a4334eff50471574d2
|
@@ -3,6 +3,24 @@ module ActiveRemote
|
|
3
3
|
module Protobuf
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
|
6
|
+
TYPECASTER_MAP = {
|
7
|
+
::Protobuf::Field::BoolField => ActiveAttr::Typecasting::BooleanTypecaster,
|
8
|
+
::Protobuf::Field::BytesField => ActiveAttr::Typecasting::StringTypecaster,
|
9
|
+
::Protobuf::Field::DoubleField => ActiveAttr::Typecasting::FloatTypecaster,
|
10
|
+
::Protobuf::Field::Fixed32Field => ActiveAttr::Typecasting::IntegerTypecaster,
|
11
|
+
::Protobuf::Field::Fixed64Field => ActiveAttr::Typecasting::IntegerTypecaster,
|
12
|
+
::Protobuf::Field::FloatField => ActiveAttr::Typecasting::FloatTypecaster,
|
13
|
+
::Protobuf::Field::Int32Field => ActiveAttr::Typecasting::IntegerTypecaster,
|
14
|
+
::Protobuf::Field::Int64Field => ActiveAttr::Typecasting::IntegerTypecaster,
|
15
|
+
::Protobuf::Field::Sfixed32Field => ActiveAttr::Typecasting::IntegerTypecaster,
|
16
|
+
::Protobuf::Field::Sfixed64Field => ActiveAttr::Typecasting::IntegerTypecaster,
|
17
|
+
::Protobuf::Field::Sint32Field => ActiveAttr::Typecasting::IntegerTypecaster,
|
18
|
+
::Protobuf::Field::Sint64Field => ActiveAttr::Typecasting::IntegerTypecaster,
|
19
|
+
::Protobuf::Field::StringField => ActiveAttr::Typecasting::StringTypecaster,
|
20
|
+
::Protobuf::Field::Uint32Field => ActiveAttr::Typecasting::IntegerTypecaster,
|
21
|
+
::Protobuf::Field::Uint64Field => ActiveAttr::Typecasting::IntegerTypecaster
|
22
|
+
}
|
23
|
+
|
6
24
|
module ClassMethods
|
7
25
|
def fields_from_attributes(message_class, attributes)
|
8
26
|
Fields.from_attributes(message_class, attributes)
|
@@ -75,9 +93,9 @@ module ActiveRemote
|
|
75
93
|
when field.message? then
|
76
94
|
message_value
|
77
95
|
when field.repeated? then
|
78
|
-
repeated_value
|
96
|
+
repeated_value.map { |value| typecast(value) }
|
79
97
|
else
|
80
|
-
|
98
|
+
typecasted_value
|
81
99
|
end
|
82
100
|
end
|
83
101
|
|
@@ -97,6 +115,27 @@ module ActiveRemote
|
|
97
115
|
def repeated_value
|
98
116
|
value.is_a?(Array) ? value : [ value ]
|
99
117
|
end
|
118
|
+
|
119
|
+
def typecast(value)
|
120
|
+
return value if value.nil?
|
121
|
+
|
122
|
+
typecaster? ? typecaster.call(value) : value
|
123
|
+
end
|
124
|
+
|
125
|
+
def typecasted_value
|
126
|
+
typecast(value)
|
127
|
+
end
|
128
|
+
|
129
|
+
def typecaster
|
130
|
+
@typecaster ||= begin
|
131
|
+
typecaster = TYPECASTER_MAP[field.type_class]
|
132
|
+
typecaster.new if typecaster
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def typecaster?
|
137
|
+
typecaster.present?
|
138
|
+
end
|
100
139
|
end
|
101
140
|
end
|
102
141
|
end
|
@@ -51,5 +51,38 @@ describe ActiveRemote::Serializers::Protobuf::Field do
|
|
51
51
|
end
|
52
52
|
end
|
53
53
|
end
|
54
|
+
|
55
|
+
# TODO: Find a better way to write this. It works for now, but it's not
|
56
|
+
# very clear and is hard to track down since failures don't include a
|
57
|
+
# description.
|
58
|
+
#
|
59
|
+
# TODO: Consider adding specific specs for typecasting dates to integers
|
60
|
+
# or strings since that's what prompted the re-introduction of the
|
61
|
+
# typecasting.
|
62
|
+
#
|
63
|
+
context "when typecasting fields" do
|
64
|
+
let(:string_value) { double(:string, :to_s => '') }
|
65
|
+
|
66
|
+
def typecasts(field, conversion)
|
67
|
+
field = Serializer.get_field(field, true)
|
68
|
+
described_class.from_attribute(field, conversion.first[0]).should eq conversion.first[1]
|
69
|
+
end
|
70
|
+
|
71
|
+
it { typecasts(:bool_field, '0' => false) }
|
72
|
+
it { typecasts(:bytes_field, string_value => '') }
|
73
|
+
it { typecasts(:double_field, 0 => 0.0) }
|
74
|
+
it { typecasts(:fixed32_field, '0' => 0) }
|
75
|
+
it { typecasts(:fixed64_field, '0' => 0) }
|
76
|
+
it { typecasts(:float_field, 0 => 0.0) }
|
77
|
+
it { typecasts(:int32_field, '0' => 0) }
|
78
|
+
it { typecasts(:int64_field, '0' => 0) }
|
79
|
+
it { typecasts(:sfixed32_field, '0' => 0) }
|
80
|
+
it { typecasts(:sfixed64_field, '0' => 0) }
|
81
|
+
it { typecasts(:sint32_field, '0' => 0) }
|
82
|
+
it { typecasts(:sint64_field, '0' => 0) }
|
83
|
+
it { typecasts(:string_field, string_value => '') }
|
84
|
+
it { typecasts(:uint32_field, '0' => 0) }
|
85
|
+
it { typecasts(:uint64_field, '0' => 0) }
|
86
|
+
end
|
54
87
|
end
|
55
88
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
message Serializer {
|
2
|
+
optional bool bool_field = 1;
|
3
|
+
optional bytes bytes_field = 2;
|
4
|
+
optional double double_field = 3;
|
5
|
+
optional fixed32 fixed32_field = 4;
|
6
|
+
optional fixed64 fixed64_field = 5;
|
7
|
+
optional float float_field = 6;
|
8
|
+
optional int32 int32_field = 7;
|
9
|
+
optional int64 int64_field = 8;
|
10
|
+
optional sfixed32 sfixed32_field = 9;
|
11
|
+
optional sfixed64 sfixed64_field = 10;
|
12
|
+
optional sint32 sint32_field = 11;
|
13
|
+
optional sint64 sint64_field = 12;
|
14
|
+
optional string string_field = 13;
|
15
|
+
optional uint32 uint32_field = 14;
|
16
|
+
optional uint64 uint64_field = 15;
|
17
|
+
}
|
data/spec/support/protobuf.rb
CHANGED
@@ -0,0 +1,33 @@
|
|
1
|
+
##
|
2
|
+
# This file is auto-generated. DO NOT EDIT!
|
3
|
+
#
|
4
|
+
require 'protobuf/message'
|
5
|
+
|
6
|
+
|
7
|
+
##
|
8
|
+
# Message Classes
|
9
|
+
#
|
10
|
+
class Serializer < ::Protobuf::Message; end
|
11
|
+
|
12
|
+
|
13
|
+
##
|
14
|
+
# Message Fields
|
15
|
+
#
|
16
|
+
class Serializer
|
17
|
+
optional :bool, :bool_field, 1
|
18
|
+
optional :bytes, :bytes_field, 2
|
19
|
+
optional :double, :double_field, 3
|
20
|
+
optional :fixed32, :fixed32_field, 4
|
21
|
+
optional :fixed64, :fixed64_field, 5
|
22
|
+
optional :float, :float_field, 6
|
23
|
+
optional :int32, :int32_field, 7
|
24
|
+
optional :int64, :int64_field, 8
|
25
|
+
optional :sfixed32, :sfixed32_field, 9
|
26
|
+
optional :sfixed64, :sfixed64_field, 10
|
27
|
+
optional :sint32, :sint32_field, 11
|
28
|
+
optional :sint64, :sint64_field, 12
|
29
|
+
optional :string, :string_field, 13
|
30
|
+
optional :uint32, :uint32_field, 14
|
31
|
+
optional :uint64, :uint64_field, 15
|
32
|
+
end
|
33
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_remote
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Hutchison
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: active_attr
|
@@ -208,6 +208,7 @@ files:
|
|
208
208
|
- spec/support/definitions/category.proto
|
209
209
|
- spec/support/definitions/error.proto
|
210
210
|
- spec/support/definitions/post.proto
|
211
|
+
- spec/support/definitions/serializer.proto
|
211
212
|
- spec/support/definitions/tag.proto
|
212
213
|
- spec/support/helpers.rb
|
213
214
|
- spec/support/models.rb
|
@@ -221,6 +222,7 @@ files:
|
|
221
222
|
- spec/support/protobuf/category.pb.rb
|
222
223
|
- spec/support/protobuf/error.pb.rb
|
223
224
|
- spec/support/protobuf/post.pb.rb
|
225
|
+
- spec/support/protobuf/serializer.pb.rb
|
224
226
|
- spec/support/protobuf/tag.pb.rb
|
225
227
|
homepage: https://github.com/liveh2o/active_remote
|
226
228
|
licenses: []
|
@@ -265,6 +267,7 @@ test_files:
|
|
265
267
|
- spec/support/definitions/category.proto
|
266
268
|
- spec/support/definitions/error.proto
|
267
269
|
- spec/support/definitions/post.proto
|
270
|
+
- spec/support/definitions/serializer.proto
|
268
271
|
- spec/support/definitions/tag.proto
|
269
272
|
- spec/support/helpers.rb
|
270
273
|
- spec/support/models.rb
|
@@ -278,4 +281,5 @@ test_files:
|
|
278
281
|
- spec/support/protobuf/category.pb.rb
|
279
282
|
- spec/support/protobuf/error.pb.rb
|
280
283
|
- spec/support/protobuf/post.pb.rb
|
284
|
+
- spec/support/protobuf/serializer.pb.rb
|
281
285
|
- spec/support/protobuf/tag.pb.rb
|