Module: Magick::RVG::Duplicatable

Included in:
Magick::RVG, ClipPath, Group, Image, Pattern, TextBase, Use
Defined in:
lib/rvg/misc.rb

Overview

This is a standard deep_copy method that is used in most classes. Thanks to Robert Klemme.

Instance Method Summary collapse

Instance Method Details

#deep_copy(h = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rvg/misc.rb', line 8

def deep_copy(h = {})
  # Prevent recursion. If we reach the
  # object we started with, stop copying.
  copy = h[__id__]
  unless copy
    h[__id__] = copy = self.class.allocate
    ivars = instance_variables
    ivars.each do |ivar|
      ivalue = instance_variable_get(ivar)
      cvalue = if ivalue.is_a?(NilClass) || ivalue.is_a?(Symbol) || ivalue.is_a?(Float) || ivalue.is_a?(Integer) || ivalue.is_a?(FalseClass) || ivalue.is_a?(TrueClass)
                 ivalue
               elsif ivalue.respond_to?(:deep_copy)
                 ivalue.deep_copy(h)
               elsif ivalue.respond_to?(:dup)
                 ivalue.dup
               else
                 ivalue
               end
      copy.instance_variable_set(ivar, cvalue)
    end
    copy.freeze if frozen?
  end
  copy
end