Class: Hierarchy::IndexPath
- Inherits:
-
Object
- Object
- Hierarchy::IndexPath
- Includes:
- Enumerable
- Defined in:
- lib/hierarchy/index_path.rb
Overview
An array of integers representing an ordered list of IDs. Duck-types an @Array@ in many ways.
Class Method Summary collapse
-
.from_ltree(string) ⇒ IndexPath
Creates an index path from a PostgreSQL @LTREE@ column.
Instance Method Summary collapse
-
#<=>(other) ⇒ -1, ...
Defines a natural ordering of index paths.
-
#initialize(id, ...) ⇒ IndexPath
constructor
Creates an index path from a list of integer IDs.
- #inspect ⇒ Object
Constructor Details
#initialize(id, ...) ⇒ IndexPath
Returns A new instance.
23 24 25 26 |
# File 'lib/hierarchy/index_path.rb', line 23 def initialize(*indexes) raise ArgumentError, "IndexPath indexes must be integers" unless indexes.all? { |index| index.kind_of?(Fixnum) } @indexes = indexes end |
Class Method Details
.from_ltree(string) ⇒ IndexPath
Creates an index path from a PostgreSQL @LTREE@ column.
33 34 35 |
# File 'lib/hierarchy/index_path.rb', line 33 def self.from_ltree(string) new(*(string.split('.').map(&:to_i))) end |
Instance Method Details
#<=>(other) ⇒ -1, ...
Defines a natural ordering of index paths. Paths with lower IDs at the same index level will come before those with higher IDs at that index level. Lower IDs at shallower index levels come before lower IDs at deeper index levels.
47 48 49 50 |
# File 'lib/hierarchy/index_path.rb', line 47 def <=>(other) raise ArgumentError, "Can't compare IndexPath and #{other.class.to_s}" unless other.kind_of?(IndexPath) indexes <=> other.send(:indexes) end |
#inspect ⇒ Object
53 |
# File 'lib/hierarchy/index_path.rb', line 53 def inspect() "#<#{self.class.to_s} #{@indexes.inspect}>" end |