module ObjectFramework::Values
Copies storage state and detaches values crossing an invocation boundary.
Public Class Methods
Source
# File lib/object_framework/execution.rb, line 19 def self.detach(value, visiting = {}) composite = value.is_a?(Array) || value.is_a?(Hash) || value.is_a?(Data) if composite raise UnsafeResult, "Public values cannot contain cycles" if visiting.key?(value.object_id) visiting[value.object_id] = true end case value when nil, true, false, Symbol, Integer, Float then value when String then value.dup.freeze when Array then value.map { |item| detach(item, visiting) }.freeze when Hash then value.to_h { |key, item| [detach(key, visiting), detach(item, visiting)] }.freeze when Data then value.class.new(**detach(value.to_h, visiting)) else raise UnsafeResult, "Public results must contain independent values (#{value.class})" end ensure visiting.delete(value.object_id) if composite end
Returns an independent, immutable graph of supported scalar, Array, Hash, and Data values. Raises UnsafeResult for cycles and unsupported objects.
Source
# File lib/object_framework/execution.rb, line 7 def self.mutable_copy(value) case value when String then value.dup when Array then value.map { |item| mutable_copy(item) } when Hash then value.to_h { |key, item| [mutable_copy(key), mutable_copy(item)] } when Data then value.class.new(**mutable_copy(value.to_h)) else value end end
Storage values are immutable; domain state receives its own editable copy.