Module: Sequel::Plugins::PgLtree::InstanceMethods

Defined in:
lib/sequel/plugins/pg_ltree/pg_ltree.rb

Instance Method Summary collapse

Instance Method Details

#after_destroyboolean

After destroy hook Works only with destroy, not with delete

Returns:

  • (boolean)


169
170
171
# 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_updateboolean

After update hook

Returns:

  • (boolean)


154
155
156
157
158
159
160
161
162
163
# 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

#ancestorsarray

Fetch ancestors without self

Returns:

  • (array)


147
148
149
# 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

#childrenarray

Fetch children

Returns:

  • (array)


101
102
103
104
# 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

#descendantsarray

Fetch descendants without self

Returns:

  • (array)


116
117
118
# 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_columnstring

Ltree column name

Returns:

  • (string)


59
60
61
# File 'lib/sequel/plugins/pg_ltree/pg_ltree.rb', line 59

def ltree_column
  scope.column
end

#ltree_pathstring

Fetch ltree path value

Returns:

  • (string)


73
74
75
# File 'lib/sequel/plugins/pg_ltree/pg_ltree.rb', line 73

def ltree_path
  public_send scope.column
end

#nlevelinteger

Fetch node level

Returns:

  • (integer)


80
81
82
# 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

#parentObject

Fetch parent of the node

return [object] parent



94
95
96
# 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

#rootobject

Fetch node root

Returns:

  • (object)

    root



87
88
89
# 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

#scopeObject

Plugin configuration

Returns:

  • class



52
53
54
# File 'lib/sequel/plugins/pg_ltree/pg_ltree.rb', line 52

def scope
  self.class
end

#self_and_ancestorsarray

Fetch self and ancestors

Returns:

  • (array)


140
141
142
# 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_descendantsarray

Fetch self and descendants

Returns:

  • (array)


109
110
111
# 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_siblingsarray

Fetch self and siblings

Returns:

  • (array)


123
124
125
126
127
128
# 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

#siblingsarray

Fetch siblings without self

Returns:

  • (array)


133
134
135
# 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_namestring

Model table name

Returns:

  • (string)


66
67
68
# File 'lib/sequel/plugins/pg_ltree/pg_ltree.rb', line 66

def table_name
  scope.table_name
end