Module: Sumi::Graphics::Mixtures::Duplicate

Defined in:
graphics/lib/sumi/graphics/mixtures/duplicate.rb

Overview

DSL helpers for duplicating independent element subtrees.

Instance Method Summary collapse

Instance Method Details

#Duplicate(dx: nil, dy: nil, parent: nil) {|element| ... } ⇒ Sumi::Graphics::Element

Duplicates an element subtree as an independent tree and optionally translates it. Copied elements receive new child arrays, attribute stores, and content arrays. Visible id attributes are moved to non-rendering -id metadata before the optional block runs, allowing the block to derive replacement ids without rendering duplicates. A pre-existing -id takes precedence over the visible id. Translation and parent channels are validated before the subtree is copied or the customization block runs. The copied subtree has no parent during customization and is attached to the target parent afterward.

Examples:

Remap source ids on a duplicate

source = Sumi::Graphics.SVG { rect id: "shape" }.children.first
copy = source.Duplicate do |node|
  node[:id] = "#{node[:"-id"]}-copy" if node[:"-id"]
end

Parameters:

  • dx (Numeric, nil) (defaults to: nil) —

    finite x translation. Nil omits the axis

  • dy (Numeric, nil) (defaults to: nil) —

    finite y translation. Nil omits the axis

  • parent (Sumi::Graphics::Element, nil) (defaults to: nil) —

    explicit parent, or the source parent when nil

Yields:

  • (element) —

    optional customization hook for each copied element

Yield Parameters:

Yield Returns:

  • (Object) —

    ignored customization result

Returns:

Raises:



29
30
31
32
33
34
35
# File 'graphics/lib/sumi/graphics/mixtures/duplicate.rb', line 29

def Duplicate(dx: nil, dy: nil, parent: nil, &block)
  dx, dy, target = Subtree.channels(self, dx, dy, parent)
  duplicated = dup
  Subtree.prepare(duplicated, &block)
  Subtree.translate(duplicated, dx, dy)
  Subtree.attach(duplicated, target)
end

#DuplicateX(dx, parent: nil) {|element| ... } ⇒ Sumi::Graphics::Element

Duplicates an element subtree along the x-axis.

Parameters:

  • dx (Numeric) —

    x translation

  • parent (Sumi::Graphics::Element, nil) (defaults to: nil) —

    parent for the duplicated subtree

Yields:

  • (element) —

    optional customization hook for each copied element

Yield Parameters:

Yield Returns:

  • (Object) —

    ignored customization result

Returns:

Raises:



47
48
49
50
# File 'graphics/lib/sumi/graphics/mixtures/duplicate.rb', line 47

def DuplicateX(dx, parent: nil, &block)
  ArgumentError.("Duplicate x translation cannot be nil") if dx.nil?
  Duplicate(dx:, dy: 0, parent:, &block)
end

#DuplicateY(dy, parent: nil) {|element| ... } ⇒ Sumi::Graphics::Element

Duplicates an element subtree along the y-axis.

Parameters:

  • dy (Numeric) —

    y translation

  • parent (Sumi::Graphics::Element, nil) (defaults to: nil) —

    parent for the duplicated subtree

Yields:

  • (element) —

    optional customization hook for each copied element

Yield Parameters:

Yield Returns:

  • (Object) —

    ignored customization result

Returns:

Raises:



62
63
64
65
# File 'graphics/lib/sumi/graphics/mixtures/duplicate.rb', line 62

def DuplicateY(dy, parent: nil, &block)
  ArgumentError.("Duplicate y translation cannot be nil") if dy.nil?
  Duplicate(dx: 0, dy:, parent:, &block)
end