Module: Demiurge::Util
Overview
Utilities for deep-copying simple JSON-serializable data structures.
Instance Method Summary collapse
-
#copyfreeze(items) ⇒ Object
This operation duplicates standard data that can be reconstituted from JSON, to make a frozen copy.
-
#deepcopy(items) ⇒ Object
This operation duplicates standard data that can be reconstituted from JSON, to make a non-frozen copy.
Instance Method Details
#copyfreeze(items) ⇒ Object
This operation duplicates standard data that can be reconstituted from JSON, to make a frozen copy.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/demiurge/util.rb', line 8 def copyfreeze(items) case items when Hash result = {} items.each do |k, v| result[k] = copyfreeze(v) end result.freeze when Array items.map { |i| copyfreeze(i) } when Numeric items when NilClass items when TrueClass items when FalseClass items when String if items.frozen? items else items.dup.freeze end else STDERR.puts "Unrecognized item type #{items.class.inspect} in copyfreeze!" items.dup.freeze end end |
#deepcopy(items) ⇒ Object
This operation duplicates standard data that can be reconstituted from JSON, to make a non-frozen copy.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/demiurge/util.rb', line 40 def deepcopy(items) case items when Hash result = {} items.each do |k, v| result[k] = deepcopy(v) end result when Array items.map { |i| deepcopy(i) } when Numeric items when NilClass items when TrueClass items when FalseClass items when String items.dup else STDERR.puts "Unrecognized item type #{items.class.inspect} in copyfreeze!" items.dup end end |