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

Class Method Summary collapse

Instance Method Summary collapse

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

Methods included from Serialization

#decode, #decode_from, #encode, #encode_to

Constructor Details

#initialize(fields = {}) ⇒ Message

Constructor



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_jsonObject

Class Methods



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

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.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
# 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_serializationObject



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

Returns:

  • (Boolean)


84
85
86
# File 'lib/protobuf/message.rb', line 84

def has_field?(name)
  @values.has_key?(name)
end

#inspectObject



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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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_hashObject 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_protoObject



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

def to_proto
  self
end