Module: Yzz

Defined in:
lib/yzz.rb,
lib/yzz/version.rb

Overview

Module Yzz is a mixin that provides qualities of a ZZ structure cell to its includers.

A ZZ structure consists of ZZ objects, which exist in multiple dimensions. ZZ objects may be connected by directed edges – connections. Connected objects are called neighbors. Each connection belongs to exactly one dimension. A ZZ object is considered as having two sides in each dimension: posward side and negward side. A connection always points away from the posward side, and towards the negward side of the neighbor. In each dimension, a ZZ object can have at most one posward and one negward neighbor. The relation is bijective: If B is the posward neighbor of A along dimension X, then A is a negward neighbor of B along X, and vice-versa. There is no limitation as to what objects can be connected. Circles are allowed. A ZZ object can even be connected to itself, forming a loop.

To this basic definition, Ted Nelson adds a bunch of additional terminology. A rank is a series of ZZ objects connected along the same dimension. A rank viewed horizontally is referred to as row. A rank viewed vertically is referred to as column. Ted Nelson’s terms related not to the ZZ structure itself, but rather to the proposed user interface (such as cursor, view…) are not implemented in yzz, but rather in y_nelson gem.

Defined Under Namespace

Modules: Side Classes: NegwardSide, PoswardSide, SidePair

Constant Summary collapse

Dimensions =

A hash whose #[] method expects a dimension as an argument and returns a dimension-specific mixin.

Hash.new { |, missing_dimension|
  [ missing_dimension ] = Module.new do
    define_singleton_method :dimension do missing_dimension end
    define_method :dimension do missing_dimension end
  end
}
VERSION =
"2.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.Dimension(dimension) ⇒ Object

An accessor method for a dimension-specific mixin from Yzz::Dimensions hash.



48
49
50
# File 'lib/yzz.rb', line 48

def self.Dimension dimension
  Dimensions[ dimension ]
end

Instance Method Details

#along(dimension) ⇒ Object

Returns a SidePair instance along the requested dimension.



77
78
79
# File 'lib/yzz.rb', line 77

def along dimension
  zz_dimensions[ dimension ]
end

#connectionsObject

Returns all sides actually connected to a zz object.



83
84
85
86
# File 'lib/yzz.rb', line 83

def connections
  zz_dimensions.map { |_, pair| [ pair.negward, pair.posward ] }
    .reduce( [], :+ ).select { |side| side.neighbor.is_a_zz? }
end

#inspectObject

Inspect string of the object.



116
117
118
# File 'lib/yzz.rb', line 116

def inspect
  to_s
end

#neighborsObject

Returns all neighbors of a zz object.



90
91
92
# File 'lib/yzz.rb', line 90

def neighbors
  connections.map &:neighbor
end

#SidePairObject

A reader method that returns a parametrized subclass of Yzz::SidePair. This reader subsequently redefines itself (shadows itself with a newly defined singleton method) so as to always simply return the same parametrized subclass.



70
71
72
73
# File 'lib/yzz.rb', line 70

def SidePair
  SidePair.parametrize( zz: self )
    .tap { |ç| define_singleton_method :SidePair do ç end }
end

#to_sObject

A string describing the object with respect to its zz qualities.



110
111
112
# File 'lib/yzz.rb', line 110

def to_s
  "#<Yzz, #{connections.size} conn.>"
end

#towards(other) ⇒ Object

Returns all sides facing another zz object supplied as argument. (Note that this can be more than 1 side: object A can be connected to B along more than 1 dimension.



98
99
100
# File 'lib/yzz.rb', line 98

def towards other
  connections.select { |side| side.neighbor == other }
end

#tw(other) ⇒ Object

Prints the labels of the sides facing towards a given zz object.



104
105
106
# File 'lib/yzz.rb', line 104

def tw other
  puts towards( other ).map &:label
end

#zz_dimensionsObject

A reader method that returns a hash whose #[] method returns an appropriate side pair for a supplied dimension. The hash is constructed upon first call of the reader. This reader subsequently redefines itself (shadows itself with a newly defined singleton method) so as to always simply return the same hash.



57
58
59
60
61
62
63
# File 'lib/yzz.rb', line 57

def zz_dimensions
  Hash.new { |, missing_dimension|
    [ missing_dimension ] = Class.new SidePair() do
      include ::Yzz.Dimension missing_dimension # ::Yzz just in case
    end.new
  }.tap { || define_singleton_method :zz_dimensions do  end }
end