class Protobuf::Message
Public Class Methods
from_json(json)
click to toggle source
# File lib/protobuf/message.rb, line 24 def self.from_json(json) fields = normalize_json(JSON.parse(json)) new(fields) end
new(fields = {}) { |self| ... }
click to toggle source
Constructor
# File lib/protobuf/message.rb, line 44 def initialize(fields = {}) @values = {} fields.to_hash.each do |name, value| set_field(name, value, true) end yield self if block_given? end
normalize_json(ob)
click to toggle source
# File lib/protobuf/message.rb, line 29 def self.normalize_json(ob) case ob when Array ob.map { |value| normalize_json(value) } when Hash Hash[*ob.flat_map { |key, value| [key.underscore, normalize_json(value)] }] else ob end end
to_json()
click to toggle source
Class Methods
# File lib/protobuf/message.rb, line 20 def self.to_json name end
Public Instance Methods
==(other)
click to toggle source
# File lib/protobuf/message.rb, line 192 def ==(other) return false unless other.is_a?(self.class) each_field do |field, value| return false unless value == other[field.name] end true end
[](name)
click to toggle source
# File lib/protobuf/message.rb, line 200 def [](name) field = _protobuf_message_field[name] field.value_from_values(@values) rescue # not having a field should be the exceptional state raise if field fail ArgumentError, "invalid field name=#{name.inspect}" end
[]=(name, value)
click to toggle source
# File lib/protobuf/message.rb, line 208 def []=(name, value) set_field(name, value, true) end
clear!()
click to toggle source
Public Instance Methods
# File lib/protobuf/message.rb, line 57 def clear! @values.delete_if do |_, value| if value.is_a?(::Protobuf::Field::FieldArray) || value.is_a?(::Protobuf::Field::FieldHash) value.clear false else true end end self end
clone()
click to toggle source
Calls superclass method
# File lib/protobuf/message.rb, line 69 def clone copy_to(super, :clone) end
dup()
click to toggle source
Calls superclass method
# File lib/protobuf/message.rb, line 73 def dup copy_to(super, :dup) end
each_field() { |field, value| ... }
click to toggle source
Iterate over every field, invoking the given block
# File lib/protobuf/message.rb, line 79 def each_field return to_enum(:each_field) unless block_given? self.class.all_fields.each do |field| value = self[field.name] yield(field, value) end end
each_field_for_serialization() { |field, value_from_values_for_serialization| ... }
click to toggle source
# File lib/protobuf/message.rb, line 88 def each_field_for_serialization _protobuf_message_unset_required_field_tags.each do |tag| fail ::Protobuf::SerializationError, "Required field #{self.class.name}##{_protobuf_message_field[tag].name} does not have a value." end @values.each_key do |fully_qualified_name| field = _protobuf_message_field[fully_qualified_name] yield(field, field.value_from_values_for_serialization(@values)) end end
field?(name)
click to toggle source
# File lib/protobuf/message.rb, line 99 def field?(name) field = _protobuf_message_field[name] if field field.field?(@values) else false end end
Also aliased as: respond_to_has?
inspect()
click to toggle source
# File lib/protobuf/message.rb, line 111 def inspect attrs = self.class.fields.map do |field| [field.name, self[field.name].inspect].join('=') end.join(' ') "#<#{self.class} #{attrs}>" end
respond_to_has_and_present?(key)
click to toggle source
# File lib/protobuf/message.rb, line 119 def respond_to_has_and_present?(key) field = _protobuf_message_field[key] if field field.field_and_present?(@values) else false end end
set_field(name, value, ignore_nil_for_repeated, field = nil)
click to toggle source
# File lib/protobuf/message.rb, line 212 def set_field(name, value, ignore_nil_for_repeated, field = nil) field ||= _protobuf_message_field[name] if field field.set_field(@values, value, ignore_nil_for_repeated, self) else fail(::Protobuf::FieldNotDefinedError, name) unless ::Protobuf.ignore_unknown_fields? end end
to_hash()
click to toggle source
Return a hash-representation of the given fields for this message type.
# File lib/protobuf/message.rb, line 130 def to_hash result = {} @values.each_key do |field_name| field = _protobuf_message_field[field_name] field.to_message_hash(@values, result) end result end
Also aliased as: to_hash_value, to_proto_hash
to_hash_with_string_keys()
click to toggle source
# File lib/protobuf/message.rb, line 141 def to_hash_with_string_keys result = {} @values.each_key do |field_name| field = _protobuf_message_field[field_name] field.to_message_hash_with_string_key(@values, result) end result end
to_json(options = {})
click to toggle source
# File lib/protobuf/message.rb, line 152 def to_json(options = {}) to_json_hash(options).to_json(options) end
to_json_hash(options = {})
click to toggle source
Return a hash-representation of the given fields for this message type that is safe to convert to JSON.
# File lib/protobuf/message.rb, line 158 def to_json_hash(options = {}) result = {} proto3 = options[:proto3] || options[:lower_camel_case] @values.each_key do |field_name| value = self[field_name] field = self.class.get_field(field_name, true) # NB: to_json_hash_value should come before json_encode so as to handle # repeated fields without extra logic. hashed_value = if value.respond_to?(:to_json_hash_value) && !field.is_a?(::Protobuf::Field::EnumField) value.to_json_hash_value(options) elsif field.respond_to?(:json_encode) field.json_encode(value, options) else value end if proto3 && (hashed_value.nil? || value == field.class.default) result.delete(field.name) else key = proto3 ? field.name.to_s.camelize(:lower).to_sym : field.name result[key] = hashed_value end end result end
Also aliased as: to_json_hash_value
to_proto()
click to toggle source
# File lib/protobuf/message.rb, line 188 def to_proto self end
unknown()
click to toggle source
Includes & Extends
# File lib/protobuf/message.rb, line 12 extend ::Protobuf::Message::Fields
Private Instance Methods
copy_to(object, method)
click to toggle source
Private Instance Methods
# File lib/protobuf/message.rb, line 244 def copy_to(object, method) duplicate = proc do |obj| case obj when Message, String then obj.__send__(method) else obj end end object.__send__(:initialize) @values.each do |name, value| if value.is_a?(::Protobuf::Field::FieldArray) object[name].replace(value.map { |v| duplicate.call(v) }) else object[name] = duplicate.call(value) end end object end