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

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

Defined Under Namespace

Classes: Traversal

Instance Method Summary collapse

Instance Method Details

#<<(element) ⇒ Object



134
135
136
# File 'lib/sevgi/graphics/mixtures/core.rb', line 134

def <<(element)
  Append(element)
end

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



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

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

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

#AdoptFirstObject



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

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

#Append(*elements) ⇒ Object



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

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

#Classify(*classes) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sevgi/graphics/mixtures/core.rb', line 36

def Classify(*classes)
  tap do
    unless self[:class]
      self[:class] = classes
      next
    end

    case self[:class]
    when ::Array
      self[:class]
    when ::String
      self[:class].split
    end => klasses

    classes.each { klasses << it unless klasses.include?(it) }
  end
end

#Defaults(**attributes) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/sevgi/graphics/mixtures/core.rb', line 54

def Defaults(**attributes)
  tap do
    attributes.each do |key, value|
      next if self[key]

      self[key] = value
    end
  end
end

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



64
65
66
# File 'lib/sevgi/graphics/mixtures/core.rb', line 64

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

#Forward(receiver, method) ⇒ Object



68
69
70
# File 'lib/sevgi/graphics/mixtures/core.rb', line 68

def Forward(receiver, method, ...)
  receiver.public_send(method, self, ...)
end

#Is?(name) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/sevgi/graphics/mixtures/core.rb', line 72

def Is?(name)
  self.name() == name.to_sym
end

#OrphanObject



76
77
78
# File 'lib/sevgi/graphics/mixtures/core.rb', line 76

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

#Prepend(*elements) ⇒ Object



80
81
82
# File 'lib/sevgi/graphics/mixtures/core.rb', line 80

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

#RootObject



84
85
86
87
88
89
# File 'lib/sevgi/graphics/mixtures/core.rb', line 84

def Root
  element = self
  element = element.parent while element.Root?()

  element
end

#Root?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/sevgi/graphics/mixtures/core.rb', line 91

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

#StayObject



97
# File 'lib/sevgi/graphics/mixtures/core.rb', line 97

def Stay(...) = Traversal.new(...)

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



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sevgi/graphics/mixtures/core.rb', line 99

def Traverse(depth = 0, leave = nil, &block)
  ArgumentError.("Block required") unless block

  tap do
    yield(self, depth).tap { return it.value if it.is_a?(Traversal) }

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

    leave&.call(self, depth).tap { return it.value if it.is_a?(Traversal) }
  end
end

#TraverseUp(height = 0, &block) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/sevgi/graphics/mixtures/core.rb', line 111

def TraverseUp(height = 0, &block)
  ArgumentError.("Block required") unless block

  element = self

  loop do
    yield(element, height).tap { return it.value if it.is_a?(Traversal) }

    break if element.Root?()

    element = element.parent
    height += 1
  end
end

#With(*args, **kwargs, &block) ⇒ Object



126
127
128
# File 'lib/sevgi/graphics/mixtures/core.rb', line 126

def With(*args, **kwargs, &block)
  tap { (args.shift || self).parent.instance_exec(*args, **kwargs, &block) }
end

#Within(*args, **kwargs, &block) ⇒ Object



130
131
132
# File 'lib/sevgi/graphics/mixtures/core.rb', line 130

def Within(*args, **kwargs, &block)
  tap { (args.shift || self).instance_exec(*args, **kwargs, &block) }
end