Class: Sevgi::Graphics::Element

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

Direct Known Subclasses

Document::Proto

Defined Under Namespace

Modules: Ident

Constant Summary collapse

RootParent =
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) ⇒ Element

Returns a new instance of Element.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sevgi/graphics/element.rb', line 34

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) ⇒ Object



46
47
48
# File 'lib/sevgi/graphics/element.rb', line 46

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

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



32
33
34
# File 'lib/sevgi/graphics/element.rb', line 32

def attributes
  @attributes
end

#childrenObject

Returns the value of attribute children.



32
33
34
# File 'lib/sevgi/graphics/element.rb', line 32

def children
  @children
end

#contentsObject (readonly)

Returns the value of attribute contents.



32
33
34
# File 'lib/sevgi/graphics/element.rb', line 32

def contents
  @contents
end

#nameObject (readonly)

Returns the value of attribute name.



32
33
34
# File 'lib/sevgi/graphics/element.rb', line 32

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



32
33
34
# File 'lib/sevgi/graphics/element.rb', line 32

def parent
  @parent
end

Class Method Details

.element(name, parent:, &block) ⇒ Object



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

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

.root(&block) ⇒ Object



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

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

.root?(element) ⇒ Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

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

Returns:

  • (Boolean)


50
51
52
# File 'lib/sevgi/graphics/element.rb', line 50

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