Module: Sevgi::Graphics::Mixtures::Duplicate

Defined in:
lib/sevgi/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| ... } ⇒ Sevgi::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.

Examples:

Remap source ids on a duplicate

source = Sevgi::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 (Sevgi::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:

  • (Sevgi::ArgumentError)

    when the target parent has a different element class

  • (Sevgi::ArgumentError)

    when a translation is not a finite real number

  • (Sevgi::ArgumentError)

    when copied attributes or contents contain cyclic payloads



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

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

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

Duplicates an element subtree along the x-axis.

Parameters:

  • dx (Numeric)

    x translation

  • parent (Sevgi::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:

  • (Sevgi::ArgumentError)

    when the target parent has a different element class

  • (Sevgi::ArgumentError)

    when dx is not a finite real number

  • (Sevgi::ArgumentError)

    when copied attributes or contents contain cyclic payloads



46
47
48
49
# File 'lib/sevgi/graphics/mixtures/duplicate.rb', line 46

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| ... } ⇒ Sevgi::Graphics::Element

Duplicates an element subtree along the y-axis.

Parameters:

  • dy (Numeric)

    y translation

  • parent (Sevgi::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:

  • (Sevgi::ArgumentError)

    when the target parent has a different element class

  • (Sevgi::ArgumentError)

    when dy is not a finite real number

  • (Sevgi::ArgumentError)

    when copied attributes or contents contain cyclic payloads



61
62
63
64
# File 'lib/sevgi/graphics/mixtures/duplicate.rb', line 61

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