Module: Sequel::Plugins::PgLtree::InstanceMethods
- Defined in:
- lib/sequel/plugins/pg_ltree/pg_ltree.rb
Instance Method Summary collapse
-
#after_destroy ⇒ boolean
After destroy hook Works only with destroy, not with delete.
-
#after_update ⇒ boolean
After update hook.
-
#ancestors ⇒ array
Fetch ancestors without self.
-
#children ⇒ array
Fetch children.
-
#descendants ⇒ array
Fetch descendants without self.
-
#ltree_column ⇒ string
Ltree column name.
-
#ltree_path ⇒ string
Fetch ltree path value.
-
#nlevel ⇒ integer
Fetch node level.
-
#parent ⇒ Object
Fetch parent of the node.
-
#root ⇒ object
Fetch node root.
-
#scope ⇒ Object
Plugin configuration.
-
#self_and_ancestors ⇒ array
Fetch self and ancestors.
-
#self_and_descendants ⇒ array
Fetch self and descendants.
-
#self_and_siblings ⇒ array
Fetch self and siblings.
-
#siblings ⇒ array
Fetch siblings without self.
-
#table_name ⇒ string
Model table name.
Instance Method Details
#after_destroy ⇒ boolean
After destroy hook Works only with destroy, not with delete
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_update ⇒ boolean
After update hook
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 |
#ancestors ⇒ array
Fetch ancestors without self
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 |
#children ⇒ array
Fetch children
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 |
#descendants ⇒ array
Fetch descendants without self
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_column ⇒ string
Ltree column name
59 60 61 |
# File 'lib/sequel/plugins/pg_ltree/pg_ltree.rb', line 59 def ltree_column scope.column end |
#ltree_path ⇒ string
Fetch ltree path value
73 74 75 |
# File 'lib/sequel/plugins/pg_ltree/pg_ltree.rb', line 73 def ltree_path public_send scope.column end |
#nlevel ⇒ integer
Fetch node level
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 |
#parent ⇒ Object
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 |
#root ⇒ object
Fetch node 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 |
#scope ⇒ Object
Plugin configuration
52 53 54 |
# File 'lib/sequel/plugins/pg_ltree/pg_ltree.rb', line 52 def scope self.class end |
#self_and_ancestors ⇒ array
Fetch self and ancestors
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_descendants ⇒ array
Fetch self and descendants
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_siblings ⇒ array
Fetch self and siblings
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 |
#siblings ⇒ array
Fetch siblings without self
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_name ⇒ string
Model table name
66 67 68 |
# File 'lib/sequel/plugins/pg_ltree/pg_ltree.rb', line 66 def table_name scope.table_name end |