class Protobuf::Field::FieldArray

Attributes

field[R]

Attributes

Public Class Methods

new(field) click to toggle source

Constructor

# File lib/protobuf/field/field_array.rb, line 15
def initialize(field)
  @field = field
end

Public Instance Methods

<<(val) click to toggle source
Calls superclass method
# File lib/protobuf/field/field_array.rb, line 27
def <<(val)
  super(normalize(val)) unless val.nil?
end
[]=(nth, val) click to toggle source

Public Instance Methods

Calls superclass method
# File lib/protobuf/field/field_array.rb, line 23
def []=(nth, val)
  super(nth, normalize(val)) unless val.nil?
end
push(val) click to toggle source
Calls superclass method
# File lib/protobuf/field/field_array.rb, line 31
def push(val)
  super(normalize(val)) unless val.nil?
end
replace(val) click to toggle source
Calls superclass method
# File lib/protobuf/field/field_array.rb, line 35
def replace(val)
  raise_type_error(val) unless val.is_a?(Array)
  val.map! { |v| normalize(v) }
  super(val)
end
to_hash_value() click to toggle source

Return a hash-representation of the given values for this field type. The value in this case would be an array.

# File lib/protobuf/field/field_array.rb, line 43
def to_hash_value
  map do |value|
    value.respond_to?(:to_hash_value) ? value.to_hash_value : value
  end
end
to_json_hash_value() click to toggle source

Return a hash-representation of the given values for this field type that is safe to convert to JSON. The value in this case would be an array.

# File lib/protobuf/field/field_array.rb, line 52
def to_json_hash_value
  if field.respond_to?(:json_encode)
    map do |value|
      field.json_encode(value)
    end
  else
    map do |value|
      value.respond_to?(:to_json_hash_value) ? value.to_json_hash_value : value
    end
  end
end
to_s() click to toggle source
# File lib/protobuf/field/field_array.rb, line 64
def to_s
  "[#{field.name}]"
end
unshift(val) click to toggle source
Calls superclass method
# File lib/protobuf/field/field_array.rb, line 68
def unshift(val)
  super(normalize(val)) unless val.nil?
end

Private Instance Methods

normalize(value) click to toggle source

Private Instance Methods

# File lib/protobuf/field/field_array.rb, line 78
def normalize(value)
  value = value.to_proto if value.respond_to?(:to_proto)
  fail TypeError, "Unacceptable value #{value} for field #{field.name} of type #{field.type_class}" unless field.acceptable?(value)

  if field.is_a?(::Protobuf::Field::EnumField)
    field.type_class.fetch(value)
  elsif field.is_a?(::Protobuf::Field::MessageField) && value.is_a?(field.type_class)
    value
  elsif field.is_a?(::Protobuf::Field::MessageField) && value.respond_to?(:to_hash)
    field.type_class.new(value.to_hash)
  else
    value
  end
end
raise_type_error(val) click to toggle source
# File lib/protobuf/field/field_array.rb, line 93
      def raise_type_error(val)
        fail TypeError, <<-TYPE_ERROR
          Expected repeated value of type '#{field.type_class}'
          Got '#{val.class}' for repeated protobuf field #{field.name}
        TYPE_ERROR
      end