Module: Sevgi::Graphics::Mixtures::Core::InstanceMethods

Extended by:
Forwardable
Defined in:
lib/sevgi/graphics/mixtures/core.rb

Instance Method Summary collapse

Instance Method Details

#<<(element) ⇒ Object



78
79
80
# File 'lib/sevgi/graphics/mixtures/core.rb', line 78

def <<(element)
  Append(element)
end

#Adopt(new_parent = nil, index: -1)) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sevgi/graphics/mixtures/core.rb', line 14

def Adopt(new_parent = nil, index: -1)
  tap do
    if new_parent
      ArgumentError.("Element type does not match the new parent type: #{self.class}") unless instance_of?(new_parent.class)
    else
      new_parent = parent
    end

    self.Orphan
    (@parent = new_parent).children.insert(index, self)
  end
end

#AdoptFirstObject



27
28
29
# File 'lib/sevgi/graphics/mixtures/core.rb', line 27

def AdoptFirst(*)
  Adopt(*, index: 0)
end

#Append(*elements) ⇒ Object



31
32
33
# File 'lib/sevgi/graphics/mixtures/core.rb', line 31

def Append(*elements)
  tap { elements.each { _1.Adopt(self) } }
end

#Element(tag, *contents, **attributes, &block) ⇒ Object



35
36
37
# File 'lib/sevgi/graphics/mixtures/core.rb', line 35

def Element(tag, *contents, **attributes, &block)
  self.class.send(:new, tag.to_sym, contents: contents.map { Content.encoded(_1) }, attributes:, parent: self, &block)
end

#OrphanObject



39
40
41
# File 'lib/sevgi/graphics/mixtures/core.rb', line 39

def Orphan
  parent.children&.delete(self) unless Root?
end

#Prepend(*elements) ⇒ Object



43
44
45
# File 'lib/sevgi/graphics/mixtures/core.rb', line 43

def Prepend(*elements)
  tap { elements.each { _1.AdoptFirst(self) } }
end

#RootObject



47
48
49
50
51
52
53
54
55
# File 'lib/sevgi/graphics/mixtures/core.rb', line 47

def Root
  element = self
  while element
    break if element.Root?

    element = element.parent
  end
  element
end

#Root?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/sevgi/graphics/mixtures/core.rb', line 57

def Root?
  self.class.root?(self)
end

#Traverse(depth = 0, leave = nil, &block) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/sevgi/graphics/mixtures/core.rb', line 61

def Traverse(depth = 0, leave = nil, &block)
  tap do
    yield(self, depth)

    children.each { |child| child.Traverse(depth + 1, leave, &block) }
    leave&.call(self, depth)
  end
end

#With(element = nil) ⇒ Object



70
71
72
# File 'lib/sevgi/graphics/mixtures/core.rb', line 70

def With(element = nil, ...)
  tap { (element || self).parent.instance_exec(self, ...) }
end

#Within(element = nil) ⇒ Object



74
75
76
# File 'lib/sevgi/graphics/mixtures/core.rb', line 74

def Within(element = nil, ...)
  tap { (element || self).instance_exec(...) }
end