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

- (BaseField) initialize(message_class, rule, type, name, tag, options)



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

- (Object) default (readonly)

Returns the value of attribute default



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

def default
  @default
end

- (Object) default_value (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

- (Object) extension (readonly)

Returns the value of attribute extension



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

def extension
  @extension
end

- (Object) message_class (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

- (Object) name (readonly)

Returns the value of attribute name



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

def name
  @name
end

- (Object) rule (readonly)

Returns the value of attribute rule



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

def rule
  @rule
end

- (Object) tag (readonly)

Returns the value of attribute tag



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

def tag
  @tag
end

- (Object) type (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

+ (Object) default



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

def self.default
  nil
end

+ (Object) descriptor



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

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

Instance Method Details

- (Boolean) acceptable?(value)

Is a value acceptable for this field?



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

def acceptable?(value)
  true
end

- (Object) decode(bytes)

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

- (Object) descriptor



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

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

- (Object) encode(value)

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

- (Boolean) initialized?(message_instance)



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

- (Object) max

Upper limit for this field.



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

def max
  self.class.max
end

- (Object) merge(message_instance, value)

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

- (Object) min

Lower limit for this field.



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

def min
  self.class.min
end

- (Boolean) optional?

Is this a optional field?



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

def optional?
  @rule == :optional
end

- (Boolean) packed?

Is this a packed repeated field?



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

def packed?
  @packed
end

- (Boolean) ready?

:nodoc:



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

def ready? # :nodoc:
  true
end

- (Boolean) repeated?

Is this a repeated field?



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

def repeated?
  @rule == :repeated
end

- (Boolean) required?

Is this a required field?



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

def required?
  @rule == :required
end

- (Object) to_s



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

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