Module: BrowserOfBabel::Holotheca::Holarchy 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.

Methods related to manipulating hierarachy of holathecas.

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#identifierAny (readonly)

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:

  • (Any)


82
83
84
# File 'lib/browser_of_babel/holotheca/holarchy.rb', line 82

def identifier
  @identifier
end

#parentHolotheca? (readonly)

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:



80
81
82
# File 'lib/browser_of_babel/holotheca/holarchy.rb', line 80

def parent
  @parent
end

Instance Method Details

#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.

Get depth of this holotheca in the holarchy, starting from #root.

Returns:

  • (Integer)


105
106
107
108
# File 'lib/browser_of_babel/holotheca/holarchy.rb', line 105

def depth
  @depth ||=
    enumerate_parents.reduce(0) { |count, e| e.parent ? count + 1 : (break count) }
end

#dig(*identifiers) ⇒ Object

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.

Go down several levels, following a string of identifiers.

Parameters:

  • identifiers (Array<String, Symbol, Integer, Any>)

Raises:

See Also:



157
158
159
160
161
# File 'lib/browser_of_babel/holotheca/holarchy.rb', line 157

def dig(*identifiers)
  return self if !identifiers || identifiers.empty?

  down(identifiers.first).dig(*identifiers[1..])
end

#down(identifier) ⇒ Holotheca

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.

Go down a level to holotheca called identifier.

It is an error to go below the lowest level.

Parameters:

  • identifier (String, Symbol, Integer, Any)

Returns:

Raises:



145
146
147
148
149
150
# File 'lib/browser_of_babel/holotheca/holarchy.rb', line 145

def down(identifier)
  child_class = self.class.child_class
  raise InvalidHolothecaError, "nowhere to go down" unless child_class

  child_class.new(self, identifier)
end

#initialize(parent, identifier) ⇒ Object

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.



84
85
86
87
# File 'lib/browser_of_babel/holotheca/holarchy.rb', line 84

def initialize(parent, identifier)
  @parent = parent
  @identifier = identifier
end

#pathArray<Holotheca>

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 holothecas from the top level to this one.

Returns:



91
92
93
# File 'lib/browser_of_babel/holotheca/holarchy.rb', line 91

def path
  @path ||= enumerate_parents.take_while(&:itself).reverse!
end

#path_identifiersArray<Any> Also known as: deconstruct

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 identifiers of all holothecas in the #path.

Returns:

  • (Array<Any>)


97
98
99
# File 'lib/browser_of_babel/holotheca/holarchy.rb', line 97

def path_identifiers
  path.map(&:identifier)
end

#rootHolotheca

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 the root holotheca.

Returns:



112
113
114
# File 'lib/browser_of_babel/holotheca/holarchy.rb', line 112

def root
  @root ||= enumerate_parents.find { _1.parent.nil? }
end

#up(levels = 1) ⇒ Holotheca

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.

Go up levels number of times.

There is no penalty for trying to escape from the top level.

Parameters:

  • levels (#to_int) (defaults to: 1)

Returns:

Raises:

  • (ArgumentError)

    if levels is not negative or can’t be converted to integer



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/browser_of_babel/holotheca/holarchy.rb', line 123

def up(levels = 1)
  levels = levels.to_int
  raise ArgumentError, "levels must be non-negative" if levels.negative?

  return self unless parent
  return self if levels.zero?

  parent.up(levels - 1)
rescue NoMethodError => e
  raise unless e.name == :to_int

  raise ArgumentError, "no implicit conversion of #{levels.class} to Integer"
end