Class: Protobuf::Field::MessageField

Inherits:
BaseField
  • Object
show all
Defined in:
lib/protobuf/message/field.rb

Instance Attribute Summary

Attributes inherited from BaseField

#default, #message_class, #name, #rule, #tag, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseField

#clear, #default_value, #descriptor, descriptor, #error_message, #get, #initialize, #initialized?, #max, #merge, #merge_array, #min, #optional?, #ready?, #repeated?, #required?, #set, #to_s

Constructor Details

This class inherits a constructor from Protobuf::Field::BaseField

Class Method Details

.defaultObject



551
# File 'lib/protobuf/message/field.rb', line 551

def default; nil end

Instance Method Details

#acceptable?(val) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (TypeError)


584
585
586
587
# File 'lib/protobuf/message/field.rb', line 584

def acceptable?(val)
  raise TypeError unless val.instance_of?(type) or val.instance_of?(Hash)
  true
end

#decode(bytes) ⇒ Object



589
590
591
592
593
# File 'lib/protobuf/message/field.rb', line 589

def decode(bytes)
  message = type.new
  message.parse_from_string bytes.pack('C*') # TODO
  message
end

#define_setterObject



554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/protobuf/message/field.rb', line 554

def define_setter
  field = self
  @message_class.class_eval do
    define_method("#{field.name}=") do |val|
      case val
      when nil
        @values.delete(field.name)
      when Hash
        @values[field.name] = field.type.new(val)
      when field.type
        @values[field.name] = val
      else
        raise TypeError
      end
    end
  end
end

#encode(value) ⇒ Object



595
596
597
598
599
# File 'lib/protobuf/message/field.rb', line 595

def encode(value)
  bytes = value.serialize_to_string
  string_size = VarintField.encode bytes.size
  string_size << bytes
end

#merge_value(message_instance, value) ⇒ Object



601
602
603
# File 'lib/protobuf/message/field.rb', line 601

def merge_value(message_instance, value)
  message_instance[tag].merge_from value
end

#typed_default_value(default = nil) ⇒ Object



576
577
578
579
580
581
582
# File 'lib/protobuf/message/field.rb', line 576

def typed_default_value(default=nil)
  if default.is_a? Symbol
    type.module_eval default.to_s
  else
    default
  end
end

#wire_typeObject



572
573
574
# File 'lib/protobuf/message/field.rb', line 572

def wire_type
  Protobuf::WireType::LENGTH_DELIMITED
end