Class: ProtocolBuffers::Field
- Inherits:
-
Object
- Object
- ProtocolBuffers::Field
show all
- Defined in:
- lib/protocol_buffers/runtime/field.rb
Overview
Defined Under Namespace
Modules: WireFormats
Classes: AggregateField, BoolField, BytesField, DoubleField, EnumField, Fixed32Field, Fixed64Field, FloatField, GroupField, Int32Field, Int64Field, MessageField, NumericField, Sfixed32Field, Sfixed64Field, SignedVarintField, Sint32Field, Sint64Field, StringField, Uint32Field, Uint64Field, VarintField
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(otype, name, tag, opts = {}) ⇒ Field
Returns a new instance of Field.
121
122
123
124
125
126
|
# File 'lib/protocol_buffers/runtime/field.rb', line 121
def initialize(otype, name, tag, opts = {})
@otype = otype
@name = name
@tag = tag
@opts = opts.dup
end
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
97
98
99
|
# File 'lib/protocol_buffers/runtime/field.rb', line 97
def name
@name
end
|
#otype ⇒ Object
Returns the value of attribute otype.
97
98
99
|
# File 'lib/protocol_buffers/runtime/field.rb', line 97
def otype
@otype
end
|
#tag ⇒ Object
Returns the value of attribute tag.
97
98
99
|
# File 'lib/protocol_buffers/runtime/field.rb', line 97
def tag
@tag
end
|
Class Method Details
.create(sender, otype, type, name, tag, opts = {}) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/protocol_buffers/runtime/field.rb', line 102
def self.create(sender, otype, type, name, tag, opts = {})
if type.is_a?(Symbol)
klass = Field.const_get("#{type.to_s.capitalize}Field") rescue nil
raise("Type not found: #{type}") if klass.nil?
field = klass.new(otype, name, tag, opts)
elsif type.ancestors.include?(ProtocolBuffers::Enum)
field = Field::EnumField.new(type, otype, name, tag, opts)
elsif type.ancestors.include?(ProtocolBuffers::Message)
if opts[:group]
field = Field::GroupField.new(type, otype, name, tag, opts)
else
field = Field::MessageField.new(type, otype, name, tag, opts)
end
else
raise("Type not found: #{type}")
end
return field
end
|
Instance Method Details
#add_methods_to(klass) ⇒ Object
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
# File 'lib/protocol_buffers/runtime/field.rb', line 190
def add_methods_to(klass)
add_reader_to(klass)
add_writer_to(klass)
if repeated?
klass.initial_set_fields[tag] = true
klass.class_eval <<-EOF, __FILE__, __LINE__+1
def has_#{name}?; true; end
EOF
else
klass.class_eval <<-EOF, __FILE__, __LINE__+1
def has_#{name}?
value_for_tag?(#{tag})
end
EOF
end
end
|
#add_reader_to(klass) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/protocol_buffers/runtime/field.rb', line 128
def add_reader_to(klass)
if repeated?
klass.class_eval <<-EOF, __FILE__, __LINE__+1
def #{name}
unless @#{name}
@#{name} = RepeatedField.new(fields[#{tag}])
end
@#{name}
end
EOF
else
klass.class_eval <<-EOF, __FILE__, __LINE__+1
def #{name}
if @set_fields[#{tag}] == nil
# first access of this field, generate it
initialize_field(#{tag})
end
@#{name}
end
EOF
end
end
|
#add_writer_to(klass) ⇒ Object
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/protocol_buffers/runtime/field.rb', line 151
def add_writer_to(klass)
if repeated?
klass.class_eval <<-EOF, __FILE__, __LINE__+1
def #{name}=(__value)
if __value.nil?
#{name}.clear
else
unless __value.equal?(#{name})
#{name}.clear
__value.each { |i| @#{name}.push i }
end
if @parent_for_notify
@parent_for_notify.default_changed(@tag_for_notify)
@parent_for_notify = @tag_for_notify = nil
end
end
end
EOF
else
klass.class_eval <<-EOF, __FILE__, __LINE__+1
def #{name}=(__value)
field = fields[#{tag}]
if __value.nil?
@set_fields[#{tag}] = false
@#{name} = field.default_value
else
field.check_valid(__value)
@set_fields[#{tag}] = true
@#{name} = __value
if @parent_for_notify
@parent_for_notify.default_changed(@tag_for_notify)
@parent_for_notify = @tag_for_notify = nil
end
end
end
EOF
end
end
|
#check_valid(value) ⇒ Object
222
223
224
225
|
# File 'lib/protocol_buffers/runtime/field.rb', line 222
def check_valid(value)
raise(TypeError, "can't assign #{value.class.name} to #{self.class.name}") unless valid_type?(value)
check_value(value)
end
|
#check_value(value) ⇒ Object
210
211
212
|
# File 'lib/protocol_buffers/runtime/field.rb', line 210
def check_value(value)
end
|
#deserialize(value) ⇒ Object
the type of value passed in depends on the wire_type of the field: VARINT => Integer (Fixnum or Bignum) FIXED64 => 8-byte string LENGTH_DELIMITED => IO class, make sure to consume all data available FIXED32 => 4-byte string
241
242
243
|
# File 'lib/protocol_buffers/runtime/field.rb', line 241
def deserialize(value)
value
end
|
#inspect_value(value) ⇒ Object
218
219
220
|
# File 'lib/protocol_buffers/runtime/field.rb', line 218
def inspect_value(value)
value.inspect
end
|
#packed? ⇒ Boolean
100
|
# File 'lib/protocol_buffers/runtime/field.rb', line 100
def packed?; repeated? && @opts[:packed] end
|
#repeated? ⇒ Boolean
99
|
# File 'lib/protocol_buffers/runtime/field.rb', line 99
def repeated?; otype == :repeated end
|
#serialize(value) ⇒ Object
the type of value to return depends on the wire_type of the field: VARINT => Integer FIXED64 => 8-byte string LENGTH_DELIMITED => string FIXED32 => 4-byte string
232
233
234
|
# File 'lib/protocol_buffers/runtime/field.rb', line 232
def serialize(value)
value
end
|
#text_format(io, value, options = nil) ⇒ Object
245
246
247
|
# File 'lib/protocol_buffers/runtime/field.rb', line 245
def text_format(io, value, options = nil)
io.write value.to_s
end
|
#valid_type?(value) ⇒ Boolean
214
215
216
|
# File 'lib/protocol_buffers/runtime/field.rb', line 214
def valid_type?(value)
true
end
|