Class: Protobuf::Message
- Inherits:
-
Object
show all
- Extended by:
- Fields
- Includes:
- Serialization
- Defined in:
- lib/protobuf/message.rb,
lib/protobuf/message/fields.rb,
lib/protobuf/message/serialization.rb
Direct Known Subclasses
Google::Protobuf::Compiler::CodeGeneratorRequest, Google::Protobuf::Compiler::CodeGeneratorResponse, Google::Protobuf::Compiler::CodeGeneratorResponse::File, Google::Protobuf::DescriptorProto, Google::Protobuf::DescriptorProto::ExtensionRange, Google::Protobuf::EnumDescriptorProto, Google::Protobuf::EnumOptions, Google::Protobuf::EnumValueDescriptorProto, Google::Protobuf::EnumValueOptions, Google::Protobuf::FieldDescriptorProto, Google::Protobuf::FieldOptions, Google::Protobuf::FileDescriptorProto, Google::Protobuf::FileDescriptorSet, Google::Protobuf::FileOptions, Google::Protobuf::MessageOptions, Google::Protobuf::MethodDescriptorProto, Google::Protobuf::MethodOptions, Google::Protobuf::ServiceDescriptorProto, Google::Protobuf::ServiceOptions, Google::Protobuf::SourceCodeInfo, Google::Protobuf::SourceCodeInfo::Location, Google::Protobuf::UninterpretedOption, Google::Protobuf::UninterpretedOption::NamePart, Rpc::DynamicDiscovery::Beacon, Rpc::DynamicDiscovery::Server, Socketrpc::Request, Socketrpc::Response
Defined Under Namespace
Modules: Fields, Serialization
Class Method Summary
collapse
Instance Method Summary
collapse
-
#==(obj) ⇒ Object
-
#[](name) ⇒ Object
-
#[]=(name, value) ⇒ Object
-
#clear! ⇒ Object
-
#clone ⇒ Object
-
#dup ⇒ Object
-
#each_field ⇒ Object
Iterate over every field, invoking the given block.
-
#each_field_for_serialization ⇒ Object
-
#has_field?(name) ⇒ Boolean
-
#initialize(fields = {}) ⇒ Message
constructor
-
#inspect ⇒ Object
-
#respond_to_has?(key) ⇒ Boolean
(also: #responds_to_has?, #respond_to_and_has?, #responds_to_and_has?)
-
#respond_to_has_and_present?(key) ⇒ Boolean
(also: #respond_to_has_present?, #respond_to_and_has_present?, #respond_to_and_has_and_present?, #responds_to_has_present?, #responds_to_and_has_present?, #responds_to_and_has_and_present?)
-
#to_hash ⇒ Object
(also: #to_hash_value, #to_proto_hash)
Return a hash-representation of the given fields for this message type.
-
#to_json(options = {}) ⇒ Object
-
#to_proto ⇒ Object
Methods included from Fields
all_fields, define_field, extension_fields, extension_ranges, extension_tag?, extensions, field_store, field_tag?, fields, get_extension_field, get_field, optional, raise_if_name_collision, raise_if_tag_collision, repeated, required
#decode, #decode_from, #encode, #encode_to
Constructor Details
#initialize(fields = {}) ⇒ Message
29
30
31
32
33
34
35
|
# File 'lib/protobuf/message.rb', line 29
def initialize(fields = {})
@values = {}
fields.to_hash.each_pair do |name, value|
self[name] = value
end
end
|
Class Method Details
.to_json ⇒ Object
21
22
23
|
# File 'lib/protobuf/message.rb', line 21
def self.to_json
name
end
|
Instance Method Details
#==(obj) ⇒ Object
122
123
124
125
126
127
128
|
# File 'lib/protobuf/message.rb', line 122
def ==(obj)
return false unless obj.is_a?(self.class)
each_field do |field, value|
return false unless value == obj.__send__(field.name)
end
true
end
|
#[](name) ⇒ Object
130
131
132
133
134
|
# File 'lib/protobuf/message.rb', line 130
def [](name)
if field = self.class.get_field(name, true)
__send__(field.getter)
end
end
|
#[]=(name, value) ⇒ Object
136
137
138
139
140
141
142
143
144
|
# File 'lib/protobuf/message.rb', line 136
def []=(name, value)
if field = self.class.get_field(name, true)
__send__(field.setter, value) unless value.nil?
else
unless ::Protobuf.ignore_unknown_fields?
raise ::Protobuf::FieldNotDefinedError, name
end
end
end
|
#clear! ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/protobuf/message.rb', line 41
def clear!
@values.delete_if do |_, value|
if value.is_a?(::Protobuf::Field::FieldArray)
value.clear
false
else
true
end
end
self
end
|
#clone ⇒ Object
53
54
55
|
# File 'lib/protobuf/message.rb', line 53
def clone
copy_to(super, :clone)
end
|
#dup ⇒ Object
57
58
59
|
# File 'lib/protobuf/message.rb', line 57
def dup
copy_to(super, :dup)
end
|
#each_field ⇒ Object
Iterate over every field, invoking the given block
63
64
65
66
67
68
|
# File 'lib/protobuf/message.rb', line 63
def each_field
self.class.all_fields.each do |field|
value = __send__(field.getter)
yield(field, value)
end
end
|
#each_field_for_serialization ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/protobuf/message.rb', line 70
def each_field_for_serialization
self.class.all_fields.each do |field|
next unless field_must_be_serialized?(field)
value = @values[field.getter]
if value.nil?
raise ::Protobuf::SerializationError, "Required field #{self.class.name}##{field.name} does not have a value."
else
yield(field, value)
end
end
end
|
#has_field?(name) ⇒ Boolean
84
85
86
|
# File 'lib/protobuf/message.rb', line 84
def has_field?(name)
@values.has_key?(name)
end
|
#inspect ⇒ Object
88
89
90
|
# File 'lib/protobuf/message.rb', line 88
def inspect
to_hash.inspect
end
|
#respond_to_has?(key) ⇒ Boolean
Also known as:
responds_to_has?, respond_to_and_has?, responds_to_and_has?
92
93
94
|
# File 'lib/protobuf/message.rb', line 92
def respond_to_has?(key)
respond_to?(key) && has_field?(key)
end
|
#respond_to_has_and_present?(key) ⇒ Boolean
Also known as:
respond_to_has_present?, respond_to_and_has_present?, respond_to_and_has_and_present?, responds_to_has_present?, responds_to_and_has_present?, responds_to_and_has_and_present?
96
97
98
99
|
# File 'lib/protobuf/message.rb', line 96
def respond_to_has_and_present?(key)
respond_to_has?(key) &&
(__send__(key).present? || [true, false].include?(__send__(key)))
end
|
#to_hash ⇒ Object
Also known as:
to_hash_value, to_proto_hash
Return a hash-representation of the given fields for this message type.
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/protobuf/message.rb', line 102
def to_hash
result = Hash.new
@values.keys.each do |field_name|
value = __send__(field_name)
hashed_value = value.respond_to?(:to_hash_value) ? value.to_hash_value : value
result.merge!(field_name => hashed_value)
end
return result
end
|
#to_json(options = {}) ⇒ Object
114
115
116
|
# File 'lib/protobuf/message.rb', line 114
def to_json(options = {})
to_hash.to_json(options)
end
|
#to_proto ⇒ Object
118
119
120
|
# File 'lib/protobuf/message.rb', line 118
def to_proto
self
end
|