Project

arel-ltree

0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Arel extension for PostgreSQL ltree type
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0
>= 0

Runtime

>= 3.0
 Project Readme

Gem Version Build Status Dependency Status

arel-ltree

Arel extension for PostgreSQL ltree type.

Installation

Add this line to your application's Gemfile:

gem 'arel-ltree'

And then execute:

$ bundle

Or install it yourself as:

$ gem install arel-ltree

Usage

Select all parent nodes:

Node.where(Node.arel_table[:path].ancestor_of('root.subtree.node')).to_sql
# => SELECT * FROM nodes WHERE "nodes"."path" @> 'root.subtree.node'::ltree;

Select all children nodes:

Node.where(Node.arel_table[:path].descendant_of('root.subtree')).to_sql
# => SELECT * FROM nodes WHERE "nodes"."path" <@ 'root.subtree'::ltree;

Match against ltree:

Node.where(Node.arel_table[:path].matches.ltree('root.subtree')).to_sql
# => SELECT * FROM nodes WHERE "nodes"."path" ~ 'root.subtree'::ltree;

ltree = Arel::Attributes::Ltree.new('root.subtree')
Node.where(Node.arel_table[:path].matches(ltree).to_sql
# => SELECT * FROM nodes WHERE "nodes"."path" ~ 'root.subtree'::ltree;

Match against lquery (simple regex for ltree):

Node.where(Node.arel_table[:path].matches.lquery('root.*{1}.node')).to_sql
# => SELECT * FROM nodes WHERE "nodes"."path" ~ 'root.*{1}.node'::lquery;

lquery = Arel::Attributes::Lquery.new('root.*{1}.node')
Node.where(Node.arel_table[:path].matches(lquery).to_sql
# => SELECT * FROM nodes WHERE "nodes"."path" ~ 'root.*{1}.node'::lquery;

Full-text match:

Node.where(Node.arel_table[:path].matches.ltxtquery('1 2')).to_sql
# => SELECT * FROM nodes WHERE "nodes"."path" @ '1 2';

lquery = Arel::Attributes::Ltxtquery.new('1 2')
Node.where(Node.arel_table[:path].matches(lquery).to_sql
# => SELECT * FROM nodes WHERE "nodes"."path" @ '1 2';

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request