lib/active_remote/serializers/protobuf.rb in active_remote-3.3.3 vs lib/active_remote/serializers/protobuf.rb in active_remote-5.0.0.pre
- old
+ new
@@ -1,7 +1,5 @@
-require "active_remote/type"
-
module ActiveRemote
module Serializers
module Protobuf
extend ActiveSupport::Concern
@@ -97,18 +95,26 @@
when field.repeated_message? then
repeated_message_value
when field.message? then
message_value
when field.repeated? then
- repeated_value.map { |value| typecast(value) }
+ repeated_value.map { |value| cast(value) }
else
- typecasted_value
+ cast_value
end
end
private
+ def cast(value)
+ type.cast(value)
+ end
+
+ def cast_value
+ cast(value)
+ end
+
def message_value(attributes = nil)
attributes ||= value
attributes.is_a?(Hash) ? Fields.from_attributes(field.type_class, attributes) : attributes
end
@@ -117,34 +123,18 @@
message_value(attributes)
end
end
def repeated_value
- value.is_a?(Array) ? value : [ value ]
+ value.is_a?(Array) ? value : [value]
end
- def type_name
- Serializers::Protobuf.type_name_for_field(field)
+ def type
+ @type ||= ::ActiveModel::Type.lookup(type_name)
end
- def typecast(value)
- return value if value.nil?
-
- typecaster? ? typecaster.call(value) : value
- end
-
- def typecasted_value
- typecast(value)
- end
-
- def typecaster
- return nil if type_name.nil?
-
- @typecaster ||= Type.lookup(type_name)
- end
-
- def typecaster?
- typecaster.present?
+ def type_name
+ Serializers::Protobuf.type_name_for_field(field) || :value
end
end
end
end
end