Class: Torque::PostgreSQL::Attributes::LTree
- Inherits:
-
Object
- Object
- Torque::PostgreSQL::Attributes::LTree
- Includes:
- Enumerable
- Defined in:
- lib/torque/postgresql/attributes/ltree.rb,
sig/torque/postgresql/attributes/ltree.rbs
Overview
A label path, as stored by the ltree data type. It holds the plain
list of labels, so it enumerates like any other list, and an ltree[]
column simply becomes an Array of these
Instance Attribute Summary collapse
-
#items ⇒ Array[untyped]
readonly
Returns the value of attribute items.
Class Method Summary collapse
- .[](*items) ⇒ instance
-
.compatible(value) ⇒ Object
The path that the object describes for itself.
-
.compatible?(value) ⇒ Boolean
Whether the object knows how to describe itself as a path, which is what allows any class to be used where a path is expected.
Instance Method Summary collapse
- #/(other) ⇒ instance (also: #+)
- #==(other) ⇒ Boolean (also: #eql?)
- #[] ⇒ Object
-
#ancestor_of?(other) ⇒ Boolean
(also: #covers?)
Same as the
@>operator, which includes the path itself. -
#descendant_of?(other) ⇒ Boolean
(also: #covered_by?)
Same as the
<@operator, which includes the path itself. - #each {|arg0| ... } ⇒ Object
- #empty? ⇒ Boolean
- #hash ⇒ Integer
- #index_of(subpath, offset = 0) ⇒ Integer
-
#initialize(value = nil, sanitize: true) ⇒ LTree
constructor
A new instance of LTree.
- #inspect ⇒ String
-
#lca(*others) ⇒ instance
The longest common ancestor, which never includes the last label of any of the paths, exactly like PostgreSQL's own
lca. - #parent ⇒ instance?
- #root ⇒ instance
- #root? ⇒ Boolean
- #size ⇒ Integer (also: #depth)
- #to_s ⇒ String
Constructor Details
#initialize(value = nil, sanitize: true) ⇒ LTree
Returns a new instance of LTree.
37 38 39 |
# File 'lib/torque/postgresql/attributes/ltree.rb', line 37 def initialize(value = nil, sanitize: true) @items = type.items_for(value, sanitize: sanitize).freeze end |
Instance Attribute Details
#items ⇒ Array[untyped] (readonly)
Returns the value of attribute items.
12 13 14 |
# File 'lib/torque/postgresql/attributes/ltree.rb', line 12 def items @items end |
Class Method Details
.[](*items) ⇒ instance
19 20 21 |
# File 'lib/torque/postgresql/attributes/ltree.rb', line 19 def [](*items) new(items) end |
.compatible(value) ⇒ Object
The path that the object describes for itself
31 32 33 34 |
# File 'lib/torque/postgresql/attributes/ltree.rb', line 31 def compatible(value) method = PostgreSQL.config.ltree.compatible_method value.public_send(method) if compatible?(value) end |
.compatible?(value) ⇒ Boolean
Whether the object knows how to describe itself as a path, which is what allows any class to be used where a path is expected
25 26 27 28 |
# File 'lib/torque/postgresql/attributes/ltree.rb', line 25 def compatible?(value) method = PostgreSQL.config.ltree.compatible_method method.present? && value.respond_to?(method) end |
Instance Method Details
#/(other) ⇒ instance Also known as: +
67 68 69 |
# File 'lib/torque/postgresql/attributes/ltree.rb', line 67 def /(other) self.class.new(items + self.class.new(other).items, sanitize: false) end |
#==(other) ⇒ Boolean Also known as: eql?
45 46 47 48 |
# File 'lib/torque/postgresql/attributes/ltree.rb', line 45 def ==(other) other = other.items if other.is_a?(self.class) other.is_a?(::Array) && items == other end |
#[] ⇒ Object
13 |
# File 'sig/torque/postgresql/attributes/ltree.rbs', line 13
def []: (*untyped) -> untyped
|
#ancestor_of?(other) ⇒ Boolean Also known as: covers?
Same as the @> operator, which includes the path itself
74 75 76 77 |
# File 'lib/torque/postgresql/attributes/ltree.rb', line 74 def ancestor_of?(other) other = self.class.new(other).items size <= other.size && other.first(size) == items end |
#descendant_of?(other) ⇒ Boolean Also known as: covered_by?
Same as the <@ operator, which includes the path itself
81 82 83 |
# File 'lib/torque/postgresql/attributes/ltree.rb', line 81 def descendant_of?(other) self.class.new(other).ancestor_of?(self) end |
#each {|arg0| ... } ⇒ Object
10 |
# File 'sig/torque/postgresql/attributes/ltree.rbs', line 10
def each: (*untyped) ?{ (untyped) -> untyped } -> untyped
|
#empty? ⇒ Boolean
12 |
# File 'sig/torque/postgresql/attributes/ltree.rbs', line 12
def empty?: () -> bool
|
#hash ⇒ Integer
51 52 53 |
# File 'lib/torque/postgresql/attributes/ltree.rb', line 51 def hash items.hash end |
#index_of(subpath, offset = 0) ⇒ Integer
95 96 97 98 99 100 101 102 103 |
# File 'lib/torque/postgresql/attributes/ltree.rb', line 95 def index_of(subpath, offset = 0) subpath = self.class.new(subpath).items offset += size if offset.negative? return -1 if subpath.empty? || offset.negative? range = offset..(size - subpath.size) position = range.find { |i| items[i, subpath.size] == subpath } position || -1 end |
#inspect ⇒ String
9 |
# File 'sig/torque/postgresql/attributes/ltree.rbs', line 9
def inspect: () -> String
|
#lca(*others) ⇒ instance
The longest common ancestor, which never includes the last label of
any of the paths, exactly like PostgreSQL's own lca
88 89 90 91 92 93 |
# File 'lib/torque/postgresql/attributes/ltree.rb', line 88 def lca(*others) paths = [self, *others].map { |path| self.class.new(path).items[0..-2] } result = paths.shift paths.each { |path| result = common_prefix(result, path) } self.class.new(result, sanitize: false) end |
#parent ⇒ instance?
63 64 65 |
# File 'lib/torque/postgresql/attributes/ltree.rb', line 63 def parent self.class.new(items[0..-2], sanitize: false) unless root? end |
#root ⇒ instance
59 60 61 |
# File 'lib/torque/postgresql/attributes/ltree.rb', line 59 def root self.class.new(items.first(1), sanitize: false) end |
#root? ⇒ Boolean
55 56 57 |
# File 'lib/torque/postgresql/attributes/ltree.rb', line 55 def root? size <= 1 end |
#size ⇒ Integer Also known as: depth
11 |
# File 'sig/torque/postgresql/attributes/ltree.rbs', line 11
def size: () -> Integer
|
#to_s ⇒ String
41 42 43 |
# File 'lib/torque/postgresql/attributes/ltree.rb', line 41 def to_s type.serialize(self) end |