Class: Inttegro::CustomDataPatch

Inherits:
Hash
  • Object
show all
Extended by:
T::Generic, T::Sig
Defined in:
lib/inttegro/custom_data.rb

Overview

Immutable custom-data merge operations. A nil value removes a stored key.

Constant Summary collapse

K =
type_member(:out) { { fixed: String } }
V =
type_member(:out) { { fixed: Object } }
Elem =
type_member(:out) { { fixed: T::Array[T.any(String, Object)] } }

Instance Method Summary collapse

Constructor Details

#initialize(values = {}) ⇒ CustomDataPatch

Returns a new instance of CustomDataPatch.



83
84
85
86
87
88
89
90
91
# File 'lib/inttegro/custom_data.rb', line 83

def initialize(values = {})
  super()
  values.each do |key, value|
    CustomDataValidation.validate_key(key)
    self[key] = CustomDataValidation.copy_json(value)
  end
  CustomDataValidation.validate_size(to_h)
  freeze
end

Instance Method Details

#remove_change(key) ⇒ Object



106
107
108
# File 'lib/inttegro/custom_data.rb', line 106

def remove_change(key)
  CustomDataPatch.new(to_h.reject { |candidate, _| candidate == key })
end

#set(key, value) ⇒ Object

Raises:

  • (ArgumentError)


94
95
96
97
98
# File 'lib/inttegro/custom_data.rb', line 94

def set(key, value)
  raise ArgumentError, "use unset to remove a custom-data value" if value.nil?

  CustomDataPatch.new(to_h.merge(key => value))
end

#unset(key) ⇒ Object



101
102
103
# File 'lib/inttegro/custom_data.rb', line 101

def unset(key)
  CustomDataPatch.new(to_h.merge(key => nil))
end