Class: Protobuf::Field::BaseField

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

Direct Known Subclasses

BytesField, FloatField, MessageField, VarintField

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message_class, rule, type, name, tag, options) ⇒ BaseField

Returns a new instance of BaseField



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/protobuf/message/field.rb', line 46

def initialize(message_class, rule, type, name, tag, options)
  @message_class, @rule, @type, @name, @tag = \
    message_class, rule, type, name, tag

  @default   = options.delete(:default)
  @extension = options.delete(:extension)
  @packed    = repeated? && options.delete(:packed)
  unless options.empty?
    warn "WARNING: Invalid options: #{options.inspect} (in #{@message_class.name.split('::').last}.#{@name})"
  end
  if packed? && ! WireType::PACKABLE_TYPES.include?(wire_type)
    raise "Can't use packed encoding for `#{@type}' type"
  end

  @default_value = \
    case @rule
    when :repeated
      FieldArray.new(self).freeze
    when :required
      nil
    when :optional
      typed_default_value
    end

  define_accessor
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default



39
40
41
# File 'lib/protobuf/message/field.rb', line 39

def default
  @default
end

#default_valueObject (readonly)

Returns the value of attribute default_value



40
41
42
# File 'lib/protobuf/message/field.rb', line 40

def default_value
  @default_value
end

#extensionObject (readonly)

Returns the value of attribute extension



39
40
41
# File 'lib/protobuf/message/field.rb', line 39

def extension
  @extension
end

#message_classObject (readonly)

Returns the value of attribute message_class



39
40
41
# File 'lib/protobuf/message/field.rb', line 39

def message_class
  @message_class
end

#nameObject (readonly)

Returns the value of attribute name



39
40
41
# File 'lib/protobuf/message/field.rb', line 39

def name
  @name
end

#ruleObject (readonly)

Returns the value of attribute rule



39
40
41
# File 'lib/protobuf/message/field.rb', line 39

def rule
  @rule
end

#tagObject (readonly)

Returns the value of attribute tag



39
40
41
# File 'lib/protobuf/message/field.rb', line 39

def tag
  @tag
end

#typeObject (readonly)

Returns the value of attribute type



39
40
41
# File 'lib/protobuf/message/field.rb', line 39

def type
  @type
end

Class Method Details

.defaultObject



35
36
37
# File 'lib/protobuf/message/field.rb', line 35

def self.default
  nil
end

.descriptorObject



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

def self.descriptor
  @descriptor ||= Descriptor::FieldDescriptor.new
end

Instance Method Details

#acceptable?(value) ⇒ Boolean

Is a value acceptable for this field?

Returns:

  • (Boolean)


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

def acceptable?(value)
  true
end

#decode(bytes) ⇒ Object

Decode bytes and return a field value.

Raises:

  • (NotImplementedError)


90
91
92
# File 'lib/protobuf/message/field.rb', line 90

def decode(bytes)
  raise NotImplementedError, "#{self.class.name}\#decode"
end

#descriptorObject



42
43
44
# File 'lib/protobuf/message/field.rb', line 42

def descriptor
  @descriptor ||= Descriptor::FieldDescriptor.new(self)
end

#encode(value) ⇒ Object

Encode value and return a byte string.

Raises:

  • (NotImplementedError)


95
96
97
# File 'lib/protobuf/message/field.rb', line 95

def encode(value)
  raise NotImplementedError, "#{self.class.name}\#encode"
end

#initialized?(message_instance) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
87
# File 'lib/protobuf/message/field.rb', line 77

def initialized?(message_instance)
  value = message_instance.__send__(@name)
  case @rule
  when :required
    ! value.nil? && (! kind_of?(MessageField) || value.initialized?)
  when :repeated
    value.all? {|msg| ! kind_of?(MessageField) || msg.initialized? }
  when :optional
    value.nil? || ! kind_of?(MessageField) || value.initialized?
  end
end

#maxObject

Upper limit for this field.



129
130
131
# File 'lib/protobuf/message/field.rb', line 129

def max
  self.class.max
end

#merge(message_instance, value) ⇒ Object

Merge value with message_instance.



100
101
102
103
104
105
106
# File 'lib/protobuf/message/field.rb', line 100

def merge(message_instance, value)
  if repeated?
    merge_array(message_instance, value)
  else
    merge_value(message_instance, value)
  end
end

#minObject

Lower limit for this field.



134
135
136
# File 'lib/protobuf/message/field.rb', line 134

def min
  self.class.min
end

#optional?Boolean

Is this a optional field?

Returns:

  • (Boolean)


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

def optional?
  @rule == :optional
end

#packed?Boolean

Is this a packed repeated field?

Returns:

  • (Boolean)


124
125
126
# File 'lib/protobuf/message/field.rb', line 124

def packed?
  @packed
end

#ready?Boolean

:nodoc:

Returns:

  • (Boolean)


73
74
75
# File 'lib/protobuf/message/field.rb', line 73

def ready? # :nodoc:
  true
end

#repeated?Boolean

Is this a repeated field?

Returns:

  • (Boolean)


109
110
111
# File 'lib/protobuf/message/field.rb', line 109

def repeated?
  @rule == :repeated
end

#required?Boolean

Is this a required field?

Returns:

  • (Boolean)


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

def required?
  @rule == :required
end

#to_sObject



143
144
145
# File 'lib/protobuf/message/field.rb', line 143

def to_s
  "#{@rule} #{@type} #{@name} = #{@tag} #{@default ? "[default=#{@default.inspect}]" : ''}"
end