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:



38
39
40
41
42
43
44
45
# File 'lib/protobuf/message.rb', line 38

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



30
31
32
# File 'lib/protobuf/message.rb', line 30

def self.to_json
  name
end

Instance Method Details

#==(other) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/protobuf/message.rb', line 144

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



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/protobuf/message.rb', line 152

def [](name)
  field = self.class.get_field(name, true)

  fail ArgumentError, "invalid field name=#{name.inspect}" unless field

  if field.repeated?
    @values[field.fully_qualified_name] ||= ::Protobuf::Field::FieldArray.new(field)
  elsif @values.key?(field.fully_qualified_name)
    @values[field.fully_qualified_name]
  else
    field.default_value
  end
end

#[]=(name, value) ⇒ Object



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

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

#clear!Object

Public Instance Methods



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/protobuf/message.rb', line 51

def clear!
  @values.delete_if do |_, value|
    if value.is_a?(::Protobuf::Field::FieldArray)
      value.clear
      false
    else
      true
    end
  end
  self
end

#cloneObject



63
64
65
# File 'lib/protobuf/message.rb', line 63

def clone
  copy_to(super, :clone)
end

#dupObject



67
68
69
# File 'lib/protobuf/message.rb', line 67

def dup
  copy_to(super, :dup)
end

#each_fieldObject

Iterate over every field, invoking the given block



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

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



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

def each_field_for_serialization
  self.class.all_fields.each do |field|
    value = @values[field.fully_qualified_name]
    if value.nil?
      fail ::Protobuf::SerializationError, "Required field #{self.class.name}##{field.name} does not have a value." if field.required?
      next
    end

    yield(field, value)
  end
end

#field?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def field?(name)
  field = self.class.get_field(name, true)
  return false if field.nil?
  if field.repeated?
    @values.key?(field.fully_qualified_name) && @values[field.fully_qualified_name].present?
  else
    @values.key?(field.fully_qualified_name)
  end
end

#inspectObject



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

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?(key) ⇒ Boolean Also known as: responds_to_has?, respond_to_and_has?, responds_to_and_has?

Returns:

  • (Boolean)


113
114
115
# File 'lib/protobuf/message.rb', line 113

def respond_to_has?(key)
  respond_to?(key) && 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?

Returns:

  • (Boolean)


117
118
119
120
# File 'lib/protobuf/message.rb', line 117

def respond_to_has_and_present?(key)
  respond_to_has?(key) &&
    (self[key].present? || [true, false].include?(self[key]))
end

#to_hashObject Also known as: to_hash_value, to_proto_hash

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



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

def to_hash
  result = {}

  @values.each_key do |field_name|
    value = self[field_name]
    field = self.class.get_field(field_name, true)
    hashed_value = value.respond_to?(:to_hash_value) ? value.to_hash_value : value
    result[field.name] = hashed_value
  end

  result
end

#to_json(options = {}) ⇒ Object



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

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

#to_protoObject



140
141
142
# File 'lib/protobuf/message.rb', line 140

def to_proto
  self
end