class Protobuf::Field::FieldHash
Attributes
field[R]
Attributes
key_field[R]
Attributes
value_field[R]
Attributes
Public Class Methods
new(field)
click to toggle source
Constructor
# File lib/protobuf/field/field_hash.rb, line 15 def initialize(field) @field = field @key_field = field.type_class.get_field(:key) @value_field = field.type_class.get_field(:value) end
Public Instance Methods
[]=(key, val)
click to toggle source
Public Instance Methods
Calls superclass method
# File lib/protobuf/field/field_hash.rb, line 25 def []=(key, val) super(normalize_key(key), normalize_val(val)) end
Also aliased as: store
merge!(other)
click to toggle source
# File lib/protobuf/field/field_hash.rb, line 37 def merge!(other) raise_type_error(other) unless other.is_a?(Hash) # keys and values will be normalized by []= above other.each { |k, v| self[k] = v } end
Also aliased as: update
replace(val)
click to toggle source
# File lib/protobuf/field/field_hash.rb, line 31 def replace(val) raise_type_error(val) unless val.is_a?(Hash) clear update(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 the hash itself, right? Unfortunately not because the values of the map could be messages themselves that we need to transform.
# File lib/protobuf/field/field_hash.rb, line 49 def to_hash_value each_with_object({}) do |(key, value), hash| hash[key] = value.respond_to?(:to_hash_value) ? value.to_hash_value : value end end
to_json_hash_value(options = {})
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 the hash itself, right? Unfortunately not because the values of the map could be messages themselves that we need to transform.
# File lib/protobuf/field/field_hash.rb, line 61 def to_json_hash_value(options = {}) if field.respond_to?(:json_encode) each_with_object({}) do |(key, value), hash| hash[key] = field.json_encode(value) end else each_with_object({}) do |(key, value), hash| hash[key] = value.respond_to?(:to_json_hash_value) ? value.to_json_hash_value(options) : value end end end
to_s()
click to toggle source
# File lib/protobuf/field/field_hash.rb, line 73 def to_s "{#{field.name}}" end
Private Instance Methods
fetch_enum(type, val)
click to toggle source
# File lib/protobuf/field/field_hash.rb, line 107 def fetch_enum(type, val) en = type.fetch(val) raise_type_error(val) if en.nil? en end
normalize(what, value, normalize_field)
click to toggle source
# File lib/protobuf/field/field_hash.rb, line 91 def normalize(what, value, normalize_field) raise_type_error(value) if value.nil? value = value.to_proto if value.respond_to?(:to_proto) fail TypeError, "Unacceptable #{what} #{value} for field #{field.name} of type #{normalize_field.type_class}" unless normalize_field.acceptable?(value) if normalize_field.is_a?(::Protobuf::Field::EnumField) fetch_enum(normalize_field.type_class, value) elsif normalize_field.is_a?(::Protobuf::Field::MessageField) && value.is_a?(normalize_field.type_class) value elsif normalize_field.is_a?(::Protobuf::Field::MessageField) && value.respond_to?(:to_hash) normalize_field.type_class.new(value.to_hash) else value end end
normalize_key(key)
click to toggle source
Private Instance Methods
# File lib/protobuf/field/field_hash.rb, line 83 def normalize_key(key) normalize(:key, key, key_field) end
normalize_val(value)
click to toggle source
# File lib/protobuf/field/field_hash.rb, line 87 def normalize_val(value) normalize(:value, value, value_field) end
raise_type_error(val)
click to toggle source
# File lib/protobuf/field/field_hash.rb, line 113 def raise_type_error(val) fail TypeError, <<-TYPE_ERROR Expected map value of type '#{key_field.type_class} -> #{value_field.type_class}' Got '#{val.class}' for map protobuf field #{field.name} TYPE_ERROR end