Module: Protobug::Message::InstanceMethods

Defined in:
lib/protobug/message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#unknown_fieldsObject (readonly)

Returns the value of attribute unknown_fields.



276
277
278
# File 'lib/protobug/message.rb', line 276

def unknown_fields
  @unknown_fields
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



268
269
270
271
272
273
# File 'lib/protobug/message.rb', line 268

def ==(other)
  self.class.full_name == other.class.full_name &&
    self.class.fields_by_name.all? do |name, _|
      send(name) == other.send(name)
    end
end

#as_json(print_unknown_fields: false) ⇒ Object



324
325
326
327
328
329
330
331
332
333
334
# File 'lib/protobug/message.rb', line 324

def as_json(print_unknown_fields: false)
  fields_with_values = self.class.fields_by_name.select do |_name, field|
    send(field.haser)
  end

  fields_with_values.to_h do |_name, field|
    value = send(field.name)

    [field.json_name, field.json_encode(value, print_unknown_fields: print_unknown_fields)]
  end
end

#hashObject



305
306
307
# File 'lib/protobug/message.rb', line 305

def hash
  self.class.fields_by_name.map { |name, _| send(name) }.hash
end

#initializeObject



278
279
280
281
282
283
284
# File 'lib/protobug/message.rb', line 278

def initialize
  super
  self.class.fields_by_name.each_value do |field|
    instance_variable_set(field.ivar, UNSET)
  end
  @unknown_fields = []
end

#pretty_print(pp) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/protobug/message.rb', line 286

def pretty_print(pp)
  fields_with_values = self.class.fields_by_name.select do |_name, field|
    send(field.haser)
  end
  pp.group 2, "#{self.class}.new(", ")" do
    pp.breakable
    fields_with_values.each_with_index do |(name, field), idx|
      pp.nest 2 do
        unless idx.zero?
          pp.text ","
          pp.breakable " "
        end
        pp.text "#{name}: "
        pp.pp send(field.name)
      end
    end
  end
end

#to_json(print_unknown_fields: false) ⇒ Object



336
337
338
339
340
341
# File 'lib/protobug/message.rb', line 336

def to_json(print_unknown_fields: false)
  require "json"
  JSON.generate(as_json(print_unknown_fields: print_unknown_fields), allow_infinity: true)
rescue JSON::GeneratorError => e
  raise EncodeError, "failed to generate JSON: #{e}"
end

#to_protoObject



320
321
322
# File 'lib/protobug/message.rb', line 320

def to_proto
  self.class.encode(self)
end

#to_textObject



309
310
311
312
313
314
315
316
317
318
# File 'lib/protobug/message.rb', line 309

def to_text
  fields_with_values = self.class.fields_by_name.select do |_name, field|
    send(field.haser)
  end

  fields_with_values.map do |_name, field|
    value = send(field.name)
    field.to_text(value)
  end.join("\n")
end