Module: SafeObjectAsJson

Defined in:
lib/safe_object_as_json.rb

Constant Summary collapse

SUPPORT_FILTERING =

ActiveSupport 7 is adding support for filtering hash values with :only and :except options.

(ActiveSupport.version.canonical_segments.first > 6)
VERSION =
File.read(File.expand_path("../VERSION", __dir__)).chomp

Class Method Summary collapse

Class Method Details

.track_referencesObject

Yield the set of object ids currently being serialized on this thread. The set is created on the outermost call and always removed when that call finishes so that no state leaks between top level calls to Object#as_json.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/safe_object_as_json.rb', line 19

def track_references
  references = Thread.current[:object_as_json_references]
  if references
    yield references
  else
    references = Set.new
    Thread.current[:object_as_json_references] = references
    begin
      yield references
    ensure
      Thread.current[:object_as_json_references] = nil
    end
  end
end