lib/protocol_buffers/runtime/message.rb in ruby-protocol-buffers-1.4.1 vs lib/protocol_buffers/runtime/message.rb in ruby-protocol-buffers-1.5.0.beta1
- old
+ new
@@ -255,10 +255,26 @@
serialize(sio)
return sio.string
end
alias_method :to_s, :serialize_to_string
+ def to_hash
+ self.class.to_hash(self)
+ end
+
+ def self.to_hash(message)
+ return nil if message == nil
+ return message.is_a?(String) ? message.dup : message unless message.is_a?(::ProtocolBuffers::Message)
+ message.fields.select do |tag, field|
+ message.value_for_tag?(tag)
+ end.inject(Hash.new) do |hash, (tag, field)|
+ value = message.value_for_tag(tag)
+ hash[field.name] = value.is_a?(::ProtocolBuffers::RepeatedField) ? value.map { |elem| to_hash(elem) } : to_hash(value)
+ hash
+ end
+ end
+
# Parse a Message of this class from the given IO/String. Since Protocol
# Buffers are not length delimited, this will read until the end of the
# stream.
#
# This does not call clear! beforehand, so this is logically equivalent to
@@ -443,9 +459,21 @@
@set_fields[tag] = true
if @parent_for_notify
@parent_for_notify.default_changed(@tag_for_notify)
@parent_for_notify = @tag_for_notify = nil
end
+ end
+
+ def self.set_fully_qualified_name(name)
+ @fully_qualified_name = name.dup.freeze
+ end
+
+ def self.fully_qualified_name
+ @fully_qualified_name
+ end
+
+ def fully_qualified_name
+ self.class.fully_qualified_name
end
def valid?
self.class.valid?(self)
end