Module: BrowserOfBabel::Holotheca::Holarchy::ClassMethods Private

Included in:
BrowserOfBabel::Holotheca
Defined in:
lib/browser_of_babel/holotheca/holarchy.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Class methods for manipulating hierarchy.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#child_classClass?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Class, nil)


13
14
15
# File 'lib/browser_of_babel/holotheca/holarchy.rb', line 13

def child_class
  @child_class
end

#parent_classClass?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Class, nil)


11
12
13
# File 'lib/browser_of_babel/holotheca/holarchy.rb', line 11

def parent_class
  @parent_class
end

Instance Method Details

#>>(other) ⇒ Class

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Define holarchy relationship where other is a part of self.

Examples:

Library >> Section >> Bookcase >> Book

Parameters:

  • other (Class)

    a subclass of Holarchy

Returns:

  • (Class)

    other

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
# File 'lib/browser_of_babel/holotheca/holarchy.rb', line 52

def >>(other)
  raise ArgumentError, "#{other} is not a Holarchy" unless other < Holarchy

  self.child_class = other
  other.parent_class = self

  other
end

#depthInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Depth of this holotheca class in the holarchy, starting from 0.

Returns:

  • (Integer)


63
64
65
66
# File 'lib/browser_of_babel/holotheca/holarchy.rb', line 63

def depth
  parent_class = @parent_class
  parent_class ? parent_class.depth + 1 : 0
end

#holarchy(holotheca) ⇒ Class

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

It is not possible to reliably redefine the holarchy after the first call.

Convenience method to establish holarchy with current class as the root. Also redefines BrowserOfBabel::Holotheca::Holarchy#initialize to have only one optional parameter for identifier,

Examples:

# Holarchy with a single unnamed root.
class SolarSystem < Holotheca
  holarchy Planet >> Continent >> Region
end
SolarSystem.new
# Holarchy with multiple named roots.
class Planet < Holotheca
  holarchy Continent >> Region
  identifier_format(/\A[α-ω]\z/)
end
Planet.new("χ")
# Root can be included too!
holarchy SolarSystem >> Planet >> Continent >> Region

Parameters:

  • holotheca (Class)

Returns:

  • (Class)

    self



36
37
38
39
40
41
42
43
44
45
# File 'lib/browser_of_babel/holotheca/holarchy.rb', line 36

def holarchy(holotheca)
  class_eval { def initialize(identifier = nil) = super(nil, identifier) }

  # Due to how `.>>` works, parent and child classes can be messed up
  # on redefinition of a holarchy, do not even try.
  top = holotheca.root
  self >> top unless top == self

  self
end

#rootClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get root holotheca class for the holarchy.

Returns:

  • (Class)


70
71
72
# File 'lib/browser_of_babel/holotheca/holarchy.rb', line 70

def root
  Enumerator.produce(self) { _1.parent_class }.find { _1.parent_class.nil? }
end