Sha256: 43d84c8c7c7c80012fab1a1a4c9c6c2e6938f77a46bf4db7d187dd8e7089451e

Contents?: true

Size: 1.41 KB

Versions: 7

Compression:

Stored size: 1.41 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 TypeError 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)
        raise TypeError 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
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
protobuf-2.4.3.rc1 lib/protobuf/field/field_array.rb
protobuf-2.4.2-java lib/protobuf/field/field_array.rb
protobuf-2.4.2 lib/protobuf/field/field_array.rb
protobuf-2.4.1-java lib/protobuf/field/field_array.rb
protobuf-2.4.1 lib/protobuf/field/field_array.rb
protobuf-2.4.0-java lib/protobuf/field/field_array.rb
protobuf-2.4.0 lib/protobuf/field/field_array.rb