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, opts = {}) ⇒ BaseField

Returns a new instance of BaseField.



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

def initialize(message_class, rule, type, name, tag, opts={})
  @message_class, @rule, @type, @name, @tag, @default, @extension =
    message_class, rule, type, name, tag, opts[:default], opts[:extension]
  @error_message = 'Type invalid'
  define_accessor
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



29
30
31
# File 'lib/protobuf/message/field.rb', line 29

def default
  @default
end

#message_classObject

Returns the value of attribute message_class.



29
30
31
# File 'lib/protobuf/message/field.rb', line 29

def message_class
  @message_class
end

#nameObject

Returns the value of attribute name.



29
30
31
# File 'lib/protobuf/message/field.rb', line 29

def name
  @name
end

#ruleObject

Returns the value of attribute rule.



29
30
31
# File 'lib/protobuf/message/field.rb', line 29

def rule
  @rule
end

#tagObject

Returns the value of attribute tag.



29
30
31
# File 'lib/protobuf/message/field.rb', line 29

def tag
  @tag
end

#typeObject

Returns the value of attribute type.



29
30
31
# File 'lib/protobuf/message/field.rb', line 29

def type
  @type
end

Class Method Details

.defaultObject



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

def default; nil end

.descriptorObject



22
23
24
# File 'lib/protobuf/message/field.rb', line 22

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

Instance Method Details

#acceptable?(val) ⇒ Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/protobuf/message/field.rb', line 182

def acceptable?(val)
  true
end

#clear(message) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/protobuf/message/field.rb', line 59

def clear(message)
  if repeated?
    message[name].clear
  else
    message.instance_variable_get(:@values).delete(name)
  end
end

#default_valueObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/protobuf/message/field.rb', line 67

def default_value
  case rule
  when :repeated
    FieldArray.new self
  when :required
    nil
  when :optional
    typed_default_value default
  else
    raise InvalidRuleError
  end
end

#descriptorObject



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

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

#error_messageObject



186
187
188
# File 'lib/protobuf/message/field.rb', line 186

def error_message
  @error_message
end

#get(value) ⇒ Object



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

def get(value)
  encode(value)
end

#initialized?(message) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/protobuf/message/field.rb', line 44

def initialized?(message)
  case rule
  when :required
    return false if message[name].nil?
    return false if is_a?(Protobuf::Field::MessageField) and not message[name].initialized?
  when :repeated
    return message[name].all? {|msg|
      (not is_a?(Protobuf::Field::MessageField)) or msg.initialized?
    }
  when :optional
    return false if message[name] and is_a?(Protobuf::Field::MessageField) and not message[name].initialized?
  end
  true
end

#maxObject



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

def max; self.class.max end

#merge(message_instance, value) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/protobuf/message/field.rb', line 157

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

#merge_array(message_instance, value) ⇒ Object



165
166
167
# File 'lib/protobuf/message/field.rb', line 165

def merge_array(message_instance, value)
  message_instance[tag].concat value
end

#merge_value(message_instance, value) ⇒ Object



169
170
171
# File 'lib/protobuf/message/field.rb', line 169

def merge_value(message_instance, value)
  message_instance[tag] = value
end

#minObject



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

def min; self.class.min end

#optional?Boolean

Returns:

  • (Boolean)


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

def optional?; rule == :optional end

#ready?Boolean

Returns:

  • (Boolean)


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

def ready?; true end

#repeated?Boolean

utility methods

Returns:

  • (Boolean)


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

def repeated?; rule == :repeated end

#required?Boolean

Returns:

  • (Boolean)


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

def required?; rule == :required end

#set(message_instance, bytes) ⇒ Object

encoder/decoder related methods



134
135
136
137
138
139
140
141
# File 'lib/protobuf/message/field.rb', line 134

def set(message_instance, bytes)
  value = decode(bytes)
  if repeated?
    message_instance.send(name) << value
  else
    message_instance.send("#{name}=", value)
  end
end

#to_sObject



190
191
192
# File 'lib/protobuf/message/field.rb', line 190

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

#typed_default_value(default = nil) ⇒ Object



80
81
82
# File 'lib/protobuf/message/field.rb', line 80

def typed_default_value(default=nil)
  default or self.class.default
end