Class: ProtocolBuffers::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol_buffers/runtime/field.rb

Overview

:nodoc: all

Defined Under Namespace

Modules: WireFormats Classes: BoolField, BytesField, DoubleField, EnumField, Fixed32Field, Fixed64Field, FloatField, 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.



116
117
118
119
120
121
# File 'lib/protocol_buffers/runtime/field.rb', line 116

def initialize(otype, name, tag, opts = {})
  @otype = otype
  @name = name
  @tag = tag
  @opts = opts.dup
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



97
98
99
# File 'lib/protocol_buffers/runtime/field.rb', line 97

def name
  @name
end

#otypeObject (readonly)

Returns the value of attribute otype.



97
98
99
# File 'lib/protocol_buffers/runtime/field.rb', line 97

def otype
  @otype
end

#tagObject (readonly)

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/protocol_buffers/runtime/field.rb', line 101

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)
    field = Field::MessageField.new(type, otype, name, tag, opts)
  else
    raise("Type not found: #{type}")
  end
  return field
end

Instance Method Details

#add_methods_to(klass) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/protocol_buffers/runtime/field.rb', line 123

def add_methods_to(klass)
  klass.class_eval <<-EOF, __FILE__, __LINE__+1
    attr_reader :#{name}
  EOF
  if repeated?
    klass.class_eval <<-EOF, __FILE__, __LINE__+1
      def #{name}=(value)
        if value.nil?
          @#{name}.clear
        else
          @#{name}.clear
          value.each { |i| @#{name}.push i }
        end
      end

      def has_#{name}?; true; end
    EOF
  else
    klass.class_eval <<-EOF, __FILE__, __LINE__+1
      def #{name}=(value)
        field = fields[#{tag}]
        if value.nil?
          @set_fields.delete_at(#{tag})
          @#{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

      def has_#{name}?
        value_for_tag?(#{tag})
      end
    EOF
  end
end

#check_valid(value) ⇒ Object

Raises:

  • (TypeError)


177
178
179
180
# File 'lib/protocol_buffers/runtime/field.rb', line 177

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



165
166
167
# File 'lib/protocol_buffers/runtime/field.rb', line 165

def check_value(value)
  # pass
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



196
197
198
# File 'lib/protocol_buffers/runtime/field.rb', line 196

def deserialize(value)
  value
end

#inspect_value(value) ⇒ Object



173
174
175
# File 'lib/protocol_buffers/runtime/field.rb', line 173

def inspect_value(value)
  value.inspect
end

#repeated?Boolean

Returns:

  • (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



187
188
189
# File 'lib/protocol_buffers/runtime/field.rb', line 187

def serialize(value)
  value
end

#valid_type?(value) ⇒ Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/protocol_buffers/runtime/field.rb', line 169

def valid_type?(value)
  true
end