Class: BrowserOfBabel::Holotheca

Inherits:
Object
  • Object
show all
Extended by:
Holarchy::ClassMethods
Includes:
Holarchy
Defined in:
lib/browser_of_babel/holotheca.rb,
lib/browser_of_babel/holotheca/holarchy.rb

Overview

Base holotheca class. From holon (Greek holos (ὅλος) meaning ‘whole’, with the suffix -on which denotes a part) and theca (Greek theke (θήκη) meaning ‘case, sheath, sleeve’ — a container).

Direct Known Subclasses

Hex, Library, Page, Shelf, Volume, Wall

Defined Under Namespace

Modules: Holarchy

Constant Summary collapse

DEFAULT_URL_FORMATTER =

Trivial formatter that just calls #to_s on identifier. You probably want something more.

-> { _1.to_s }

Instance Attribute Summary

Attributes included from Holarchy::ClassMethods

#child_class, #parent_class

Attributes included from Holarchy

#identifier, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Holarchy::ClassMethods

>>, depth, holarchy, root

Methods included from Holarchy

#depth, #dig, #down, #path, #path_identifiers, #root, #up

Constructor Details

#initialize(parent, identifier) ⇒ Holotheca

Returns a new instance of Holotheca.

Parameters:

  • parent (Holotheca)

    must be an instance of parent_class

  • identifier (Any)

    must correspond to identifier_format; if identifier_class is String, Symbol, or Integer, identifier is converted to a frozen instance with #to_s, #to_sym, or #to_i respectively

Raises:



92
93
94
95
# File 'lib/browser_of_babel/holotheca.rb', line 92

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

Class Method Details

.holotheca_nameString?

Get the name of the holotheca, the class name without the namespace.

Returns:

  • (String, nil)


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

def holotheca_name
  return unless name

  @holotheca_name ||= name.split("::").last # rubocop:disable Style/IpAddresses
end

.identifier_classClass?

Get expected class for the holotheca’s identifier.

Depends on identifier_format:

  • a Class — that class

  • a RegexpString

  • a Range — class of the first element

  • a Set — class of first element if all elements are of that class (subsequent elements can be of a subclass)

  • anything else — nil (class unknown)

Returns:

  • (Class, nil)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/browser_of_babel/holotheca.rb', line 42

def identifier_class
  case (validator = @identifier_format)
  when Class
    validator
  when Regexp
    String
  when Range
    validator.begin.class
  when Set
    klass = validator.first.class
    (validator.all? { klass === _1 }) ? klass : nil
  else
    nil
  end
end

.identifier_format#===? .identifier_format(format) ⇒ #===?

Get or set format validator for the holotheca’s identifier.

Overloads:

  • .identifier_format(format) ⇒ #===?

    Parameters:

    • format (#===, nil)

Returns:

  • (#===, nil)

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
# File 'lib/browser_of_babel/holotheca.rb', line 23

def identifier_format(format = (no_argument = true; nil)) # rubocop:disable Style/Semicolon
  return @identifier_format if no_argument

  return @identifier_format = format if nil == format || format.respond_to?(:===) # rubocop:disable Style/YodaCondition

  raise ArgumentError, "invalid format validator #{format}"
end

.url_format#call .url_format(holotheca) ⇒ #call

Get or set string formatter for URLs.

Overloads:

  • .url_format(holotheca) ⇒ #call

    Parameters:

    • format (#call, nil)

      formatter or nil to reset to default

Returns:



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/browser_of_babel/holotheca.rb', line 63

def url_format(format = (no_argument = true; nil)) # rubocop:disable Style/Semicolon
  return (@url_format ||= DEFAULT_URL_FORMATTER) if no_argument

  if nil == format # rubocop:disable Style/YodaCondition
    @url_format = DEFAULT_URL_FORMATTER
  elsif format.respond_to?(:call)
    # @type var format : _Formatter
    @url_format = format
  else
    raise ArgumentError, "invalid formatter #{format}"
  end
end

Instance Method Details

#to_sString

Get string representation of the holotheca path.

Returns:

  • (String)


126
127
128
# File 'lib/browser_of_babel/holotheca.rb', line 126

def to_s
  path.filter_map(&:to_s_part).join(", ")
end

#to_s_partString

Get string representation of the holotheca: holotheca_name + BrowserOfBabel::Holotheca::Holarchy#identifier. May be empty.

Returns:

  • (String)


112
113
114
115
116
117
118
119
120
121
122
# File 'lib/browser_of_babel/holotheca.rb', line 112

def to_s_part
  if self.class.holotheca_name
    if identifier
      "#{self.class.holotheca_name} #{identifier}"
    else
      self.class.holotheca_name.to_s
    end
  else
    identifier.to_s
  end
end

#to_urlString

Get string representation of the complete URI (down to this holotheca).

Returns:

  • (String)


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

def to_url
  path.map(&:to_url_part).join
end

#to_url_partString

Get string representation for use in URIs.

Returns:

  • (String)


99
100
101
# File 'lib/browser_of_babel/holotheca.rb', line 99

def to_url_part
  self.class.url_format.call(identifier)
end