Class: Torque::PostgreSQL::Attributes::LTree

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil, sanitize: true) ⇒ LTree

Returns a new instance of LTree.

Parameters:

  • value (Object) (defaults to: nil)
  • sanitize: (Boolean) (defaults to: true)


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.

Returns:

  • (Array[untyped])


12
13
14
# File 'lib/torque/postgresql/attributes/ltree.rb', line 12

def items
  @items
end

Class Method Details

.[](*items) ⇒ instance

Parameters:

  • items (Object)

Returns:

  • (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

Parameters:

  • value (Object)

Returns:

  • (Object)


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

Parameters:

  • value (Object)

Returns:

  • (Boolean)


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: +

Parameters:

  • other (Object)

Returns:

  • (instance)


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?

Parameters:

  • other (Object)

Returns:

  • (Boolean)


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

Parameters:

  • (Object)

Returns:

  • (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

Parameters:

  • other (Object)

Returns:

  • (Boolean)


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

Parameters:

  • other (Object)

Returns:

  • (Boolean)


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

Parameters:

  • (Object)

Yields:

Yield Parameters:

  • arg0 (Object)

Yield Returns:

  • (Object)

Returns:

  • (Object)


10
# File 'sig/torque/postgresql/attributes/ltree.rbs', line 10

def each: (*untyped) ?{ (untyped) -> untyped } -> untyped

#empty? ⇒ Boolean

Returns:

  • (Boolean)


12
# File 'sig/torque/postgresql/attributes/ltree.rbs', line 12

def empty?: () -> bool

#hash ⇒ Integer

Returns:

  • (Integer)


51
52
53
# File 'lib/torque/postgresql/attributes/ltree.rb', line 51

def hash
  items.hash
end

#index_of(subpath, offset = 0) ⇒ Integer

Parameters:

  • subpath (Object)
  • offset (Integer) (defaults to: 0)

Returns:

  • (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

Returns:

  • (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

Parameters:

  • others (Object)

Returns:

  • (instance)


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?

Returns:

  • (instance, nil)


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

Returns:

  • (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

Returns:

  • (Boolean)


55
56
57
# File 'lib/torque/postgresql/attributes/ltree.rb', line 55

def root?
  size <= 1
end

#size ⇒ Integer Also known as: depth

Returns:

  • (Integer)


11
# File 'sig/torque/postgresql/attributes/ltree.rbs', line 11

def size: () -> Integer

#to_s ⇒ String

Returns:

  • (String)


41
42
43
# File 'lib/torque/postgresql/attributes/ltree.rb', line 41

def to_s
  type.serialize(self)
end