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. Public id attributes are moved to an internal -id attribute before the optional block runs, allowing the block to derive replacement ids without rendering duplicate public ids.

Parameters:

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

    x translation

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

    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:

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when the target parent has a different element class



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sevgi/graphics/mixtures/duplicate.rb', line 19

def Duplicate(dx: nil, dy: nil, parent: nil, &block)
  duplicated = Subtree.copy(self)

  duplicated.Traverse() do |element|
    id = element.attributes.delete(:id)
    element[:"#{ATTRIBUTE_INTERNAL_PREFIX}id"] = id if id
    block&.call(element)
  end

  duplicated.Translate(dx, dy) if dx || dy

  duplicated.Adopt(parent)

  duplicated
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:

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when the target parent has a different element class



42
# File 'lib/sevgi/graphics/mixtures/duplicate.rb', line 42

def DuplicateX(dx, parent: nil, &block) = Duplicate(dx:, dy: 0, parent:, &block)

#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:

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when the target parent has a different element class



51
# File 'lib/sevgi/graphics/mixtures/duplicate.rb', line 51

def DuplicateY(dy, parent: nil, &block) = Duplicate(dx: 0, dy:, parent:, &block)