lib/protocol_buffers/runtime/message.rb in ruby-protocol-buffers-1.2.2 vs lib/protocol_buffers/runtime/message.rb in ruby-protocol-buffers-1.2.3.beta1
- old
+ new
@@ -230,26 +230,11 @@
# message = MyMessageClass.new(attributes)
# # is equivalent to
# message = MyMessageClass.new
# message.attributes = attributes
def initialize(attributes = {})
- @set_fields = []
-
- fields.each do |tag, field|
- if field.repeated?
- self.instance_variable_set("@#{field.name}", RepeatedField.new(field))
- @set_fields[tag] = true # repeated fields are always "set"
- else
- value = field.default_value
- self.__send__("#{field.name}=", value)
- @set_fields[tag] = false
- if field.class == Field::MessageField
- value.notify_on_change(self, tag)
- end
- end
- end
-
+ @set_fields = self.class.initial_set_fields.dup
self.attributes = attributes
end
# Serialize this Message to the given IO stream using the Protocol Buffer
# wire format.
@@ -352,10 +337,14 @@
# Returns a hash of { tag => ProtocolBuffers::Field }
def self.fields
@fields || @fields = {}
end
+ def self.initial_set_fields
+ @set_fields ||= []
+ end
+
# Returns a hash of { tag => ProtocolBuffers::Field }
def fields
self.class.fields
end
@@ -416,11 +405,10 @@
@set_fields[tag] = true
end
end
def self.define_field(otype, type, name, tag, opts = {}) # :NODOC:
- raise("gen_methods! already called, cannot add more fields") if @methods_generated
type = type.is_a?(Module) ? type : type.to_sym
name = name.to_sym
tag = tag.to_i
raise("Field already exists for tag: #{tag}") if fields[tag]
field = Field.create(self, otype, type, name, tag, opts)
@@ -460,15 +448,17 @@
def self.valid?(message, raise_exception=false)
return true unless @has_required_field
fields.each do |tag, field|
- if field.otype == :required && !message.value_for_tag?(tag)
- return false unless raise_exception
- raise(ProtocolBuffers::EncodeError.new(field), "Required field '#{field.name}' is invalid")
- end
+ next if field.otype != :required
+ next if message.value_for_tag?(tag) && (field.class != Field::MessageField || message.value_for_tag(tag).valid?)
+ return false unless raise_exception
+ raise(ProtocolBuffers::EncodeError.new(field), "Required field '#{field.name}' is invalid")
end
+
+ true
end
def validate!
self.class.validate!(self)
end
@@ -490,17 +480,24 @@
def unknown_field_count
(@unknown_fields || []).size
end
- # Generate the initialize method using reflection, to improve speed. This is
- # called by the generated .pb.rb code, it's not necessary to call this
- # method directly.
+ # left in for compatibility with previously created .pb.rb files -- no longer used
def self.gen_methods! # :NODOC:
@methods_generated = true
- # these generated methods have gone away for now -- the infrastructure has
- # been left in place, since they'll probably make their way back in at
- # some point.
+ end
+
+ protected
+
+ def initialize_field(tag)
+ field = fields[tag]
+ new_value = field.default_value
+ self.instance_variable_set("@#{field.name}", new_value)
+ if field.class == Field::MessageField
+ new_value.notify_on_change(self, tag)
+ end
+ @set_fields[tag] = false
end
end
end