Class: Protobuf::Field::FieldArray

Inherits:
Array
  • Object
show all
Defined in:
lib/protobuf/field/field_array.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field) ⇒ FieldArray

Constructor


15
16
17
# File 'lib/protobuf/field/field_array.rb', line 15

def initialize(field)
  @field = field
end

Instance Attribute Details

#fieldObject (readonly)

Attributes


9
10
11
# File 'lib/protobuf/field/field_array.rb', line 9

def field
  @field
end

Instance Method Details

#<<(val) ⇒ Object


27
28
29
# File 'lib/protobuf/field/field_array.rb', line 27

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

#[]=(nth, val) ⇒ Object

Public Instance Methods


23
24
25
# File 'lib/protobuf/field/field_array.rb', line 23

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

#push(val) ⇒ Object


31
32
33
# File 'lib/protobuf/field/field_array.rb', line 31

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

#replace(val) ⇒ Object


35
36
37
38
39
# 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_valueObject

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


43
44
45
46
47
# 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_valueObject

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.


52
53
54
55
56
57
58
59
60
61
62
# 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_sObject


64
65
66
# File 'lib/protobuf/field/field_array.rb', line 64

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

#unshift(val) ⇒ Object


68
69
70
# File 'lib/protobuf/field/field_array.rb', line 68

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