Class: Protobuf::Field::FieldHash
- Inherits:
-
Hash
- Object
- Hash
- Protobuf::Field::FieldHash
- Defined in:
- lib/protobuf/field/field_hash.rb
Instance Attribute Summary collapse
-
#field ⇒ Object
readonly
Attributes.
-
#key_field ⇒ Object
readonly
Attributes.
-
#value_field ⇒ Object
readonly
Attributes.
Instance Method Summary collapse
-
#[]=(key, val) ⇒ Object
(also: #store)
Public Instance Methods.
-
#initialize(field) ⇒ FieldHash
constructor
Constructor.
- #merge!(other) ⇒ Object (also: #update)
- #replace(val) ⇒ Object
-
#to_hash_value ⇒ Object
Return a hash-representation of the given values for this field type.
-
#to_json_hash_value ⇒ Object
Return a hash-representation of the given values for this field type that is safe to convert to JSON.
- #to_s ⇒ Object
Constructor Details
#initialize(field) ⇒ FieldHash
Constructor
15 16 17 18 19 |
# 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 |
Instance Attribute Details
#field ⇒ Object (readonly)
Attributes
9 10 11 |
# File 'lib/protobuf/field/field_hash.rb', line 9 def field @field end |
#key_field ⇒ Object (readonly)
Attributes
9 10 11 |
# File 'lib/protobuf/field/field_hash.rb', line 9 def key_field @key_field end |
#value_field ⇒ Object (readonly)
Attributes
9 10 11 |
# File 'lib/protobuf/field/field_hash.rb', line 9 def value_field @value_field end |
Instance Method Details
#[]=(key, val) ⇒ Object Also known as: store
Public Instance Methods
25 26 27 |
# File 'lib/protobuf/field/field_hash.rb', line 25 def []=(key, val) super(normalize_key(key), normalize_val(val)) end |
#merge!(other) ⇒ Object Also known as: update
37 38 39 40 41 |
# 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 |
#replace(val) ⇒ Object
31 32 33 34 35 |
# 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 ⇒ Object
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.
49 50 51 52 53 |
# 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 ⇒ Object
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.
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/protobuf/field/field_hash.rb', line 61 def to_json_hash_value 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 : value end end end |
#to_s ⇒ Object
73 74 75 |
# File 'lib/protobuf/field/field_hash.rb', line 73 def to_s "{#{field.name}}" end |