module Sequel::Plugins::PgLtree::InstanceMethods

Public Instance Methods

after_destroy() click to toggle source

After destroy hook Works only with destroy, not with delete

@return [boolean]

# File lib/sequel/plugins/pg_ltree/pg_ltree.rb, line 169
def after_destroy
  scope.where(Sequel.lit("#{table_name}.#{ltree_column} <@ ?", ltree_path)).delete
end
after_update() click to toggle source

After update hook

@return [boolean]

Calls superclass method
# File lib/sequel/plugins/pg_ltree/pg_ltree.rb, line 154
def after_update
  super

  if column_changed?(ltree_column.to_sym)
    old_value = column_change(:path)[0]
    scope
        .where(Sequel.lit("tree.path <@ ? AND tree.path != ?", old_value, ltree_path))
        .update("#{ltree_column}" => Sequel.lit("'#{ltree_path}' || SUBPATH(path, NLEVEL('#{old_value}'))"))
  end
end
ancestors() click to toggle source

Fetch ancestors without self

@return [array]

# File lib/sequel/plugins/pg_ltree/pg_ltree.rb, line 147
def ancestors
  self_and_ancestors.where(Sequel.lit("#{table_name}.#{ltree_column} != '#{ltree_path}'"))
end
children() click to toggle source

Fetch children

@return [array]

# File lib/sequel/plugins/pg_ltree/pg_ltree.rb, line 101
def children
  scope.where(Sequel.lit("? @> #{table_name}.#{ltree_column} AND nlevel(#{table_name}.#{ltree_column}) = NLEVEL(?) + 1",
                         ltree_path, ltree_path))
end
descendants() click to toggle source

Fetch descendants without self

@return [array]

# File lib/sequel/plugins/pg_ltree/pg_ltree.rb, line 116
def descendants
  self_and_descendants.where(Sequel.lit("#{table_name}.#{ltree_column} != '#{ltree_path}'"))
end
ltree_column() click to toggle source

Ltree column name

@return [string]

# File lib/sequel/plugins/pg_ltree/pg_ltree.rb, line 59
def ltree_column
  scope.column
end
ltree_path() click to toggle source

Fetch ltree path value

@return [string]

# File lib/sequel/plugins/pg_ltree/pg_ltree.rb, line 73
def ltree_path
  public_send scope.column
end
nlevel() click to toggle source

Fetch node level

@return [integer]

# File lib/sequel/plugins/pg_ltree/pg_ltree.rb, line 80
def nlevel
  scope.select(Sequel.lit("NLEVEL(?)", ltree_path).as(:count)).where(id: 2).first[:count]
end
parent() click to toggle source

Fetch parent of the node

return [object] parent

# File lib/sequel/plugins/pg_ltree/pg_ltree.rb, line 94
def parent
  scope.where(Sequel.lit("#{table_name}.#{ltree_column} = SUBPATH(?, 0, NLEVEL(?) - 1)", ltree_path, ltree_path)).first
end
root() click to toggle source

Fetch node root

@return [object] root

# File lib/sequel/plugins/pg_ltree/pg_ltree.rb, line 87
def root
  scope.where(Sequel.lit("#{table_name}.#{ltree_column} = SUBPATH(?, 0, 1)", ltree_path)).first
end
scope() click to toggle source

Plugin configuration

@return class

# File lib/sequel/plugins/pg_ltree/pg_ltree.rb, line 52
def scope
  self.class
end
self_and_ancestors() click to toggle source

Fetch self and ancestors

@return [array]

# File lib/sequel/plugins/pg_ltree/pg_ltree.rb, line 140
def self_and_ancestors
  scope.where(Sequel.lit("#{table_name}.#{ltree_column} @> ?", ltree_path))
end
self_and_descendants() click to toggle source

Fetch self and descendants

@return [array]

# File lib/sequel/plugins/pg_ltree/pg_ltree.rb, line 109
def self_and_descendants
  scope.where(Sequel.lit("#{table_name}.#{ltree_column} <@ ?", ltree_path))
end
self_and_siblings() click to toggle source

Fetch self and siblings

@return [array]

# File lib/sequel/plugins/pg_ltree/pg_ltree.rb, line 123
def self_and_siblings
  scope.where(
      Sequel.lit("SUBPATH(?, 0, NLEVEL(?) - 1) @> #{table_name}.#{ltree_column} AND nlevel(#{table_name}.#{ltree_column}) = NLEVEL(?)",
                 ltree_path, ltree_path, ltree_path)
  )
end
siblings() click to toggle source

Fetch siblings without self

@return [array]

# File lib/sequel/plugins/pg_ltree/pg_ltree.rb, line 133
def siblings
  self_and_siblings.where(Sequel.lit("#{table_name}.#{ltree_column} != '#{ltree_path}'"))
end
table_name() click to toggle source

Model table name

@return [string]

# File lib/sequel/plugins/pg_ltree/pg_ltree.rb, line 66
def table_name
  scope.table_name
end