Class: Protobuf::Message

Inherits:
Object
  • Object
show all
Extended by:
Fields
Includes:
Serialization
Defined in:
lib/protobuf/message.rb,
lib/protobuf/message/fields.rb,
lib/protobuf/message/serialization.rb

Defined Under Namespace

Modules: Fields, Serialization

Constant Summary

Constants included from Fields

Fields::ACCESSOR_SUFFIXES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Serialization

#decode, #decode_from, #encode, #encode_to

Constructor Details

#initialize(fields = {}) {|_self| ... } ⇒ Message

Constructor

Yields:

  • (_self)

Yield Parameters:



28
29
30
31
32
33
34
35
# File 'lib/protobuf/message.rb', line 28

def initialize(fields = {})
  @values = {}
  fields.to_hash.each do |name, value|
    set_field(name, value, true)
  end

  yield self if block_given?
end

Class Method Details

.to_jsonObject

Class Methods



20
21
22
# File 'lib/protobuf/message.rb', line 20

def self.to_json
  name
end

Instance Method Details

#==(other) ⇒ Object



169
170
171
172
173
174
175
# File 'lib/protobuf/message.rb', line 169

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) ⇒ Object



177
178
179
180
181
182
183
# File 'lib/protobuf/message.rb', line 177

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) ⇒ Object



185
186
187
# File 'lib/protobuf/message.rb', line 185

def []=(name, value)
  set_field(name, value, true)
end

#clear!Object

Public Instance Methods



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.is_a?(::Protobuf::Field::FieldHash)
      value.clear
      false
    else
      true
    end
  end
  self
end

#cloneObject



53
54
55
# File 'lib/protobuf/message.rb', line 53

def clone
  copy_to(super, :clone)
end

#dupObject



57
58
59
# File 'lib/protobuf/message.rb', line 57

def dup
  copy_to(super, :dup)
end

#each_fieldObject

Iterate over every field, invoking the given block



63
64
65
66
67
68
69
70
# File 'lib/protobuf/message.rb', line 63

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_serializationObject



72
73
74
75
76
77
78
79
80
81
# File 'lib/protobuf/message.rb', line 72

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) ⇒ Boolean Also known as: respond_to_has?

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
# File 'lib/protobuf/message.rb', line 83

def field?(name)
  field = _protobuf_message_field[name]

  if field
    field.field?(@values)
  else
    false
  end
end

#inspectObject



95
96
97
98
99
100
101
# File 'lib/protobuf/message.rb', line 95

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) ⇒ 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?

Returns:

  • (Boolean)


103
104
105
106
107
108
109
110
111
# File 'lib/protobuf/message.rb', line 103

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) ⇒ Object



189
190
191
192
193
194
195
196
197
# File 'lib/protobuf/message.rb', line 189

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_hashObject Also known as: to_hash_value, to_proto_hash

Return a hash-representation of the given fields for this message type.



114
115
116
117
118
119
120
121
122
123
# File 'lib/protobuf/message.rb', line 114

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

#to_hash_with_string_keysObject



125
126
127
128
129
130
131
132
133
134
# File 'lib/protobuf/message.rb', line 125

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 = {}) ⇒ Object



136
137
138
# File 'lib/protobuf/message.rb', line 136

def to_json(options = {})
  to_json_hash.to_json(options)
end

#to_json_hashObject Also known as: to_json_hash_value

Return a hash-representation of the given fields for this message type that is safe to convert to JSON.



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/protobuf/message.rb', line 142

def to_json_hash
  result = {}

  @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)
                     value.to_json_hash_value
                   elsif field.respond_to?(:json_encode)
                     field.json_encode(value)
                   else
                     value
                   end

    result[field.name] = hashed_value
  end

  result
end

#to_protoObject



165
166
167
# File 'lib/protobuf/message.rb', line 165

def to_proto
  self
end