Class: Sevgi::Graphics::Element

Inherits:
Object
  • Object
show all
Extended by:
Ident
Defined in:
lib/sevgi/graphics/element.rb

Overview

SVG element node used by the graphics DSL.

Direct Known Subclasses

Document::Proto

Defined Under Namespace

Modules: Ident

Constant Summary collapse

RootParent =

Sentinel parent used by root SVG elements.

Object.new.tap { def it.inspect = "RootParent" }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Ident

id

Constructor Details

#initialize(name, parent:, attributes: {}, contents: [], &block) ⇒ void

Creates an element.

Parameters:

  • name (Symbol)

    SVG element name

  • parent (Sevgi::Graphics::Element, Object)

    parent element or root sentinel

  • attributes (Hash) (defaults to: {})

    SVG attributes

  • contents (Array<Sevgi::Graphics::Content>) (defaults to: [])

    content objects



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/sevgi/graphics/element.rb', line 73

def initialize(name, parent:, attributes: {}, contents: [], &block)
  @name = name
  @attributes = Attributes.new(attributes)
  @children = []
  @contents = contents
  @parent = parent

  parent.children << self unless self.class.root?(self)

  instance_exec(&block) if block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, &block) ⇒ Sevgi::Graphics::Element

Dispatches SVG element DSL calls and caches valid element methods.

Parameters:

  • name (Symbol)

    missing method name

Returns:

Raises:

  • (NameError)

    when the name is not a valid SVG element

  • (Sevgi::ArgumentError)

    when an argument cannot be parsed as attributes or content



90
91
92
# File 'lib/sevgi/graphics/element.rb', line 90

def method_missing(name, *, &block)
  Element.valid?(tag = Element.id(name)) ? Dispatch.(self, name, tag, *, &block) : super
end

Instance Attribute Details

#attributesSevgi::Graphics::Attributes

Returns attribute store.

Returns:



65
# File 'lib/sevgi/graphics/element.rb', line 65

attr_reader :name, :attributes, :children, :contents, :parent

#childrenArray<Sevgi::Graphics::Element>

Returns child elements.

Returns:



65
# File 'lib/sevgi/graphics/element.rb', line 65

attr_reader :name, :attributes, :children, :contents, :parent

#contentsArray<Sevgi::Graphics::Content> (readonly)

Returns element content objects.

Returns:



65
# File 'lib/sevgi/graphics/element.rb', line 65

attr_reader :name, :attributes, :children, :contents, :parent

#nameSymbol (readonly)

Returns SVG element name.

Returns:

  • (Symbol)

    SVG element name



65
66
67
# File 'lib/sevgi/graphics/element.rb', line 65

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



65
# File 'lib/sevgi/graphics/element.rb', line 65

attr_reader :name, :attributes, :children, :contents, :parent

Class Method Details

.element(name, parent:, &block) ⇒ Sevgi::Graphics::Element

Builds an element node.

Parameters:

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when an argument cannot be parsed as attributes or content



12
# File 'lib/sevgi/graphics/element.rb', line 12

def self.element(name, *, parent:, &block) = new(name, **Dispatch.parse(name, *), parent:, &block)

.root(&block) ⇒ Sevgi::Graphics::Element

Builds an SVG root element.

Parameters:

  • block (Proc, nil)

    DSL block evaluated in the root element

Returns:



17
# File 'lib/sevgi/graphics/element.rb', line 17

def self.root(*, &block) = element(:svg, *, parent: RootParent, &block)

.root?(element) ⇒ Boolean

Reports whether an element is the root element.

Parameters:

Returns:

  • (Boolean)


22
# File 'lib/sevgi/graphics/element.rb', line 22

def self.root?(element) = element.parent == RootParent

Instance Method Details

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Reports whether a missing method can dispatch to an SVG element.

Parameters:

  • name (Symbol)

    queried method name

  • include_private (Boolean) (defaults to: false)

    standard Ruby respond_to? flag

Returns:

  • (Boolean)


98
99
100
# File 'lib/sevgi/graphics/element.rb', line 98

def respond_to_missing?(name, include_private = false)
  Element.valid?(Element.id(name)) || super
end