Module: LittleGhost::Support

Defined in:
lib/little_ghost/support.rb,
lib/little_ghost/support/loader.rb,
lib/little_ghost/support/executor.rb,
lib/little_ghost/support/redactor.rb,
lib/little_ghost/support/callbacks.rb,
lib/little_ghost/support/sse_parser.rb,
lib/little_ghost/support/http_client.rb,
lib/little_ghost/support/content_capture.rb,
lib/little_ghost/support/class_attributes.rb,
lib/little_ghost/support/output_truncation.rb,
lib/little_ghost/support/cancellation_token.rb,
lib/little_ghost/support/interruptible_stream.rb

Overview

Support collects small building blocks for LittleGhost extensions. They are available to applications that need the same cancellation, loading, callback, execution, and diagnostic behavior as the framework.

Defined Under Namespace

Modules: ClassAttributes, OutputTruncation Classes: Callbacks, CancellationToken, ContentCapture, Executor, HTTPClient, InterruptibleStream, Loader, Redactor, SSEParser

Class Method Summary collapse

Class Method Details

.deep_dup(value, ancestors = {}) ⇒ Object

Recursively duplicates hashes, arrays, and strings while preserving other values. Cyclic containers are not supported.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/little_ghost/support.rb', line 12

def deep_dup(value, ancestors = {})
  if value.is_a?(Hash) || value.is_a?(Array) || value.is_a?(Data)
    identity = value.object_id
    raise ArgumentError, "cyclic values cannot be duplicated" if ancestors.key?(identity)

    ancestors[identity] = true
  end
  case value
  when Hash
    value.each_with_object({}) do |(key, child), copy|
      copy[deep_dup(key, ancestors)] = deep_dup(child, ancestors)
    end
  when Array
    value.map { |child| deep_dup(child, ancestors) }
  when String
    value.dup
  when Data
    value.class.new(**value.to_h.transform_values { |child| deep_dup(child, ancestors) })
  else
    value
  end
ensure
  ancestors.delete(identity) if identity
end