Module: PgLtree::Ltree::InstanceMethods

Defined in:
lib/pg_ltree/ltree.rb

Overview

Define instance methods

Instance Method Summary collapse

Instance Method Details

#ancestorsActiveRecord::Relation

Get ancestors

Returns:

  • (ActiveRecord::Relation)

158
159
160
# File 'lib/pg_ltree/ltree.rb', line 158

def ancestors
  self_and_ancestors.where.not ltree_path_column => ltree_path
end

#cascade_destroyActiveRecord::Relation

Delete all children for current path

Returns:

  • (ActiveRecord::Relation)

228
229
230
# File 'lib/pg_ltree/ltree.rb', line 228

def cascade_destroy
  ltree_scope.where("#{ltree_path_column} <@ ?", ltree_path_was).delete_all
end

#cascade_updateActiveRecord::Relation

Update all childen for current path

Returns:

  • (ActiveRecord::Relation)

220
221
222
223
# File 'lib/pg_ltree/ltree.rb', line 220

def cascade_update
  ltree_scope.where(["#{ltree_path_column} <@ ?", ltree_path_was]).where(["#{ltree_path_column} != ?", ltree_path])
             .update_all ["#{ltree_path_column} = ? || subpath(#{ltree_path_column}, nlevel(?))", ltree_path, ltree_path_was]
end

#childrenActiveRecord::Relation

Get children

Returns:

  • (ActiveRecord::Relation)

212
213
214
215
# File 'lib/pg_ltree/ltree.rb', line 212

def children
  ltree_scope.where "? @> #{ltree_path_column} AND nlevel(#{ltree_path_column}) = NLEVEL(?) + 1",
                    ltree_path, ltree_path
end

#depthInteger

Get node depth

Returns:

  • (Integer)

    node depth


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

def depth
  ActiveRecord::Base.connection.select_all("SELECT NLEVEL('#{ltree_path}')").rows.flatten.first.to_i
end

#descendantsActiveRecord::Relation

Get descendants

Returns:

  • (ActiveRecord::Relation)

180
181
182
# File 'lib/pg_ltree/ltree.rb', line 180

def descendants
  self_and_descendants.where.not ltree_path_column => ltree_path
end

#descendentsActiveRecord::Relation

Deprecated.

Please use #descendants instead

Get descendants

Returns:

  • (ActiveRecord::Relation)

187
188
189
190
# File 'lib/pg_ltree/ltree.rb', line 187

def descendents
  warn '[DEPRECATION] `descendents` is deprecated. Please use `descendants` instead.'
  descendants
end

#heightNumber

Get node height

The height of a node is the number of edges on the longest downward path between that node and a leaf. The leaf nodes have height zero, and a tree with only a single node (hence both a root and leaf) has height zero. Conventionally, an empty tree (tree with no nodes, if such are allowed) has depth and height −1

Returns:

  • (Number)

    height of the given node. Height of the tree for root node.


109
110
111
# File 'lib/pg_ltree/ltree.rb', line 109

def height
  self_and_descendants.maximum("NLEVEL(#{ltree_path_column})") - depth.to_i
end

#leaf?Boolean

Check what current node have leaves

Returns:

  • (Boolean)

    True - if node have leaves, False - if node doesn't have leaves


144
145
146
# File 'lib/pg_ltree/ltree.rb', line 144

def leaf?
  leaves.count == 0
end

#leavesActiveRecord::Relation

Get leaves of the node

Returns:

  • (ActiveRecord::Relation)

137
138
139
# File 'lib/pg_ltree/ltree.rb', line 137

def leaves
  ltree_scope.leaves.where("#{ltree_path_column} <@ ?", ltree_path).where.not ltree_path_column => ltree_path
end

#ltree_pathString

Get lTree value

Returns:

  • (String)

    ltree current value


83
84
85
# File 'lib/pg_ltree/ltree.rb', line 83

def ltree_path
  public_send ltree_path_column
end

#ltree_path_columnString

Get lTree column

Returns:

  • (String)

    ltree column name


76
77
78
# File 'lib/pg_ltree/ltree.rb', line 76

def ltree_path_column
  ltree_scope.ltree_path_column
end

#ltree_path_wasString

Get lTree previous value

Returns:

  • (String)

    ltree previous value


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

def ltree_path_was
  public_send :"#{ltree_path_column}_was"
end

#ltree_scopeObject

Get default scope of ltree

Returns:

  • current class


69
70
71
# File 'lib/pg_ltree/ltree.rb', line 69

def ltree_scope
  self.class
end

#parentObject

Get parent of the node

return [Object] root node


130
131
132
# File 'lib/pg_ltree/ltree.rb', line 130

def parent
  ltree_scope.find_by "#{ltree_path_column} = SUBPATH(?, 0, NLEVEL(?) - 1)", ltree_path, ltree_path
end

#rootObject

Get root of the node

return [Object] root node


123
124
125
# File 'lib/pg_ltree/ltree.rb', line 123

def root
  ltree_scope.where("#{ltree_path_column} = SUBPATH(?, 0, 1)", ltree_path).first
end

#root?Boolean

Check what current node is root

Returns:

  • (Boolean)

    True - for root node, False - for childen node


97
98
99
# File 'lib/pg_ltree/ltree.rb', line 97

def root?
  depth == 1
end

#self_and_ancestorsActiveRecord::Relation

Get self and ancestors

Returns:

  • (ActiveRecord::Relation)

151
152
153
# File 'lib/pg_ltree/ltree.rb', line 151

def self_and_ancestors
  ltree_scope.where("#{ltree_path_column} @> ?", ltree_path)
end

#self_and_descendantsActiveRecord::Relation

Get self and descendants

Returns:

  • (ActiveRecord::Relation)

165
166
167
# File 'lib/pg_ltree/ltree.rb', line 165

def self_and_descendants
  ltree_scope.where("#{ltree_path_column} <@ ?", ltree_path)
end

#self_and_descendentsActiveRecord::Relation

Deprecated.

Please use #self_and_descendants instead

Get self and descendants

Returns:

  • (ActiveRecord::Relation)

172
173
174
175
# File 'lib/pg_ltree/ltree.rb', line 172

def self_and_descendents
  warn '[DEPRECATION] `self_and_descendents` is deprecated. Please use `self_and_descendants` instead.'
  self_and_descendants
end

#self_and_siblingsActiveRecord::Relation

Get self and siblings

Returns:

  • (ActiveRecord::Relation)

195
196
197
198
199
200
# File 'lib/pg_ltree/ltree.rb', line 195

def self_and_siblings
  ltree_scope.where(
    "SUBPATH(?, 0, NLEVEL(?) - 1) @> #{ltree_path_column} AND nlevel(#{ltree_path_column}) = NLEVEL(?)",
    ltree_path, ltree_path, ltree_path
  )
end

#siblingsActiveRecord::Relation

Get siblings

Returns:

  • (ActiveRecord::Relation)

205
206
207
# File 'lib/pg_ltree/ltree.rb', line 205

def siblings
  self_and_siblings.where.not ltree_path_column => ltree_path
end