Module: Protobug::Message::InstanceMethods
- Defined in:
- lib/protobug/message.rb
Instance Attribute Summary collapse
-
#unknown_fields ⇒ Object
readonly
Returns the value of attribute unknown_fields.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #as_json(print_unknown_fields: false) ⇒ Object
- #hash ⇒ Object
- #initialize ⇒ Object
- #pretty_print(pp) ⇒ Object
- #to_json(print_unknown_fields: false) ⇒ Object
- #to_proto ⇒ Object
- #to_text ⇒ Object
Instance Attribute Details
#unknown_fields ⇒ Object (readonly)
Returns the value of attribute unknown_fields.
262 263 264 |
# File 'lib/protobug/message.rb', line 262 def unknown_fields @unknown_fields end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
253 254 255 256 257 258 259 |
# File 'lib/protobug/message.rb', line 253 def ==(other) return false unless other.instance_of?(self.class) self.class.fields_by_name.all? do |name, _| send(name) == other.send(name) end end |
#as_json(print_unknown_fields: false) ⇒ Object
314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/protobug/message.rb', line 314 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 |
#hash ⇒ Object
291 292 293 294 295 296 297 |
# File 'lib/protobug/message.rb', line 291 def hash # Build a single array (map result is mutated in place via unshift) rather # than allocating both the mapped array and a second spread literal. values = self.class.fields_by_name.map { |name, _| send(name) } values.unshift(self.class) values.hash end |
#initialize ⇒ Object
264 265 266 267 268 269 270 |
# File 'lib/protobug/message.rb', line 264 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
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/protobug/message.rb', line 272 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 == 0 pp.text "," pp.breakable " " end pp.text "#{name}: " pp.pp send(field.name) end end end end |
#to_json(print_unknown_fields: false) ⇒ Object
326 327 328 329 330 331 |
# File 'lib/protobug/message.rb', line 326 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_proto ⇒ Object
310 311 312 |
# File 'lib/protobug/message.rb', line 310 def to_proto self.class.encode(self) end |
#to_text ⇒ Object
299 300 301 302 303 304 305 306 307 308 |
# File 'lib/protobug/message.rb', line 299 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 |