Sha256: 331a3fa357d2ed688e1e8c1e7e4308e7f72a833c0ab571eeb8f9cc494bb59396

Contents?: true

Size: 1.81 KB

Versions: 65

Compression:

Stored size: 1.81 KB

Contents

module Protobuf
  module Field
    class FieldArray < Array
      ##
      # Constructor
      #
      def initialize(field)
        @field = field
      end

      ##
      # Public Instance Methods
      #
      def []=(nth, val)
        super(nth, normalize(val)) unless val.nil?
      end

      def <<(val)
        super(normalize(val)) unless val.nil?
      end

      def push(val)
        super(normalize(val)) unless val.nil?
      end

      def unshift(val)
        super(normalize(val)) unless val.nil?
      end

      def replace(val)
        raise_type_error(val) unless val.is_a?(Array)
        val = val.map! { |v| normalize(v) }
        super(val)
      end

      # Return a hash-representation of the given values for this field type.
      # The value in this case would be an array.
      def to_hash_value
        self.map do |value|
          value.respond_to?(:to_hash_value) ? value.to_hash_value : value
        end
      end

      def to_s
        "[#{@field.name}]"
      end

      private

      ##
      # Private Instance Methods
      #
      def normalize(value)
        value = value.to_proto if value.respond_to?(:to_proto)
        raise TypeError, "Unacceptable value #{value} for field #{@field.name} of type #{@field.type}" unless @field.acceptable?(value)

        if @field.is_a?(::Protobuf::Field::EnumField)
          @field.type.fetch(value)
        elsif @field.is_a?(::Protobuf::Field::MessageField) && value.respond_to?(:to_hash)
          @field.type.new(value.to_hash)
        else
          value
        end
      end

      def raise_type_error(val)
        error_text = <<-TYPE_ERROR
          Expected repeated value of type '#{@field.type}'
          Got '#{val.class}' for repeated protobuf field #{@field.name}
        TYPE_ERROR

        raise TypeError, error_text
      end
    end
  end
end

Version data entries

65 entries across 65 versions & 1 rubygems

Version Path
protobuf-2.8.13 lib/protobuf/field/field_array.rb
protobuf-2.8.12 lib/protobuf/field/field_array.rb
protobuf-2.8.11 lib/protobuf/field/field_array.rb
protobuf-2.8.10 lib/protobuf/field/field_array.rb
protobuf-2.8.9 lib/protobuf/field/field_array.rb
protobuf-2.8.8 lib/protobuf/field/field_array.rb
protobuf-2.8.7 lib/protobuf/field/field_array.rb
protobuf-2.8.6 lib/protobuf/field/field_array.rb
protobuf-2.7.12 lib/protobuf/field/field_array.rb
protobuf-2.8.5 lib/protobuf/field/field_array.rb
protobuf-2.8.4 lib/protobuf/field/field_array.rb
protobuf-2.8.3 lib/protobuf/field/field_array.rb
protobuf-2.8.2 lib/protobuf/field/field_array.rb
protobuf-2.8.1 lib/protobuf/field/field_array.rb
protobuf-2.8.0 lib/protobuf/field/field_array.rb
protobuf-2.8.0.beta9-java lib/protobuf/field/field_array.rb
protobuf-2.8.0.beta9 lib/protobuf/field/field_array.rb
protobuf-2.8.0.beta8-java lib/protobuf/field/field_array.rb
protobuf-2.8.0.beta8 lib/protobuf/field/field_array.rb
protobuf-2.8.0.beta6-java lib/protobuf/field/field_array.rb