Class: Julewire::Core::Serialization::ValueCopy

Inherits:
Object
  • Object
show all
Defined in:
lib/julewire/core/serialization/value_copy.rb

Constant Summary collapse

CIRCULAR_REFERENCE =
Core::CIRCULAR_REFERENCE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(compact_empty:, freeze_values:, max_array_items:, max_depth:, max_hash_keys:, max_string_bytes:, preserve_truncation_metadata:, symbolize_keys:) ⇒ ValueCopy

Returns a new instance of ValueCopy.



258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/julewire/core/serialization/value_copy.rb', line 258

def initialize(compact_empty:, freeze_values:, max_array_items:, max_depth:, max_hash_keys:, max_string_bytes:,
               preserve_truncation_metadata:, symbolize_keys:)
  @compact_empty = compact_empty
  @freeze_values = freeze_values
  @max_array_items = validate_optional_limit(max_array_items, name: :max_array_items)
  @max_depth = max_depth
  @max_hash_keys = validate_optional_limit(max_hash_keys, name: :max_hash_keys)
  @max_string_bytes = validate_optional_limit(max_string_bytes, name: :max_string_bytes)
  @preserve_truncation_metadata = 
  @symbolize_keys = symbolize_keys
  @in_use = false
  @last_truncated = false
  @track_truncation = !!(@max_array_items || @max_hash_keys || @max_string_bytes)
end

Instance Attribute Details

#compact_emptyObject (readonly)

Returns the value of attribute compact_empty.



255
256
257
# File 'lib/julewire/core/serialization/value_copy.rb', line 255

def compact_empty
  @compact_empty
end

#freeze_valuesObject (readonly)

Returns the value of attribute freeze_values.



255
256
257
# File 'lib/julewire/core/serialization/value_copy.rb', line 255

def freeze_values
  @freeze_values
end

#max_array_itemsObject (readonly)

Returns the value of attribute max_array_items.



255
256
257
# File 'lib/julewire/core/serialization/value_copy.rb', line 255

def max_array_items
  @max_array_items
end

#max_depthObject (readonly)

Returns the value of attribute max_depth.



255
256
257
# File 'lib/julewire/core/serialization/value_copy.rb', line 255

def max_depth
  @max_depth
end

#max_hash_keysObject (readonly)

Returns the value of attribute max_hash_keys.



255
256
257
# File 'lib/julewire/core/serialization/value_copy.rb', line 255

def max_hash_keys
  @max_hash_keys
end

#max_string_bytesObject (readonly)

Returns the value of attribute max_string_bytes.



255
256
257
# File 'lib/julewire/core/serialization/value_copy.rb', line 255

def max_string_bytes
  @max_string_bytes
end

#preserve_truncation_metadataObject (readonly)

Returns the value of attribute preserve_truncation_metadata.



255
256
257
# File 'lib/julewire/core/serialization/value_copy.rb', line 255

def 
  @preserve_truncation_metadata
end

#symbolize_keysObject (readonly)

Returns the value of attribute symbolize_keys.



255
256
257
# File 'lib/julewire/core/serialization/value_copy.rb', line 255

def symbolize_keys
  @symbolize_keys
end

Class Method Details

.call(value, compact_empty: false, freeze_values: false, max_array_items: nil, max_depth: Core::NORMALIZATION_MAX_DEPTH, max_hash_keys: nil, max_string_bytes: nil, preserve_truncation_metadata: false, symbolize_keys: false) ⇒ Object

rubocop:disable Metrics/ParameterLists



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/julewire/core/serialization/value_copy.rb', line 183

def call( # rubocop:disable Metrics/ParameterLists
  value,
  compact_empty: false,
  freeze_values: false,
  max_array_items: nil,
  max_depth: Core::NORMALIZATION_MAX_DEPTH,
  max_hash_keys: nil,
  max_string_bytes: nil,
  preserve_truncation_metadata: false,
  symbolize_keys: false
)
  needs_string_limit = value.is_a?(String) && max_string_bytes
  return copy_leaf(value, freeze_values: freeze_values) unless container?(value) || needs_string_limit

  copy_with(
    cached_copier(
      compact_empty: compact_empty,
      freeze_values: freeze_values,
      max_array_items: max_array_items,
      max_depth: max_depth,
      max_hash_keys: max_hash_keys,
      max_string_bytes: max_string_bytes,
      preserve_truncation_metadata: ,
      symbolize_keys: symbolize_keys
    ),
    value
  )
end

.omitted_empty?(value) ⇒ Boolean

Returns:

  • (Boolean)


212
213
214
# File 'lib/julewire/core/serialization/value_copy.rb', line 212

def omitted_empty?(value)
  value.nil? || (value.is_a?(Hash) && value.empty?) || (value.is_a?(Array) && value.empty?)
end

Instance Method Details

#call(value) ⇒ Object



273
274
275
276
277
278
# File 'lib/julewire/core/serialization/value_copy.rb', line 273

def call(value)
  @last_truncated = false
  traverse(value) { |root, depth| copy_value(root, depth) }
ensure
  @last_truncated = false
end

#call_reusable(value) ⇒ Object



280
281
282
283
284
285
# File 'lib/julewire/core/serialization/value_copy.rb', line 280

def call_reusable(value)
  @in_use = true
  call(value)
ensure
  @in_use = false
end

#in_use?Boolean

Returns:

  • (Boolean)


287
# File 'lib/julewire/core/serialization/value_copy.rb', line 287

def in_use? = @in_use