Module: Magick::RVG::Duplicatable

Included in:
Magick::RVG, ClipPath, Group, Image, Pattern, Shape, 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
32
33
# 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 = case
                when NilClass === ivalue, Symbol === ivalue, Float === ivalue,
                     Fixnum === ivalue, FalseClass === ivalue, TrueClass === ivalue
                    ivalue
                when ivalue.respond_to?(:deep_copy)
                    ivalue.deep_copy(h)
                when ivalue.respond_to?(:dup)
                    ivalue.dup
                else
                    ivalue
                end
            copy.instance_variable_set(ivar, cvalue)
        end
        copy.freeze if frozen?
    end
    copy
end