ltree_hierarchy 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -13
- data/Gemfile.lock +23 -26
- data/README.md +63 -32
- data/lib/ltree_hierarchy/hierarchy.rb +5 -1
- data/lib/ltree_hierarchy/version.rb +1 -1
- metadata +29 -15
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
MzQwZGU1ZWIwZGY5MzNjM2I5OGQ1MTRkZDE4NmFjZWMxYmJiYjEzYw==
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ddf71fca7fa2fce2c558501cbb85eea76a98b2a8
|
4
|
+
data.tar.gz: 2b94b34ba4601dabaff8b0499615a9e9b9250118
|
7
5
|
SHA512:
|
8
|
-
metadata.gz:
|
9
|
-
|
10
|
-
MDMyZGM4ZWMwYWMyYmY4NDRkNjY4Yjc3N2RlMTMzOGU2Yjk0N2IyNDhlZDA2
|
11
|
-
ZDE3NTBiMGNjZTU5YWM4MTQ5ZjM5YjY3YWVlYWIyMjZjYWNiMTY=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
ZWY1YTg3MWY4NTM4NzJhNzQzZmExNWQ5MmVlNGNmMmQ4MzlhNDY1ZWZiOGY4
|
14
|
-
OGRhYWJhMjU5MDg5N2JjYWViYWFkNjI3NjM5NDYxZjhlZmY1NzI2ZmFiM2I2
|
15
|
-
NGFlOTMwODRiOTYxOGVlZTFjYjNkYzJlOGQ1Y2JjZDMzOWFjODQ=
|
6
|
+
metadata.gz: 14442c8f23722c9eb7019e93f15d64307114323b2eb57ba0dcb4193b62db142ff251a98b8c38c97994f9b3a9a5a8128770ef3a0a53d9b16b5786189044700c25
|
7
|
+
data.tar.gz: b9efa3414d82f84d940fb889c675819b7812a51cdffcff4da91945c2724f739fbb1af181cb456495a9f22bf5394bdc747750c67700dab437ec40a4865d46e9a1
|
data/Gemfile.lock
CHANGED
@@ -1,39 +1,36 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ltree_hierarchy (0.0.
|
4
|
+
ltree_hierarchy (0.0.5)
|
5
5
|
activerecord (>= 3.1.0)
|
6
6
|
pg
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
-
activemodel (4.
|
12
|
-
activesupport (= 4.
|
13
|
-
builder (~> 3.1
|
14
|
-
activerecord (4.
|
15
|
-
activemodel (= 4.
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
11
|
+
activemodel (4.1.6)
|
12
|
+
activesupport (= 4.1.6)
|
13
|
+
builder (~> 3.1)
|
14
|
+
activerecord (4.1.6)
|
15
|
+
activemodel (= 4.1.6)
|
16
|
+
activesupport (= 4.1.6)
|
17
|
+
arel (~> 5.0.0)
|
18
|
+
activesupport (4.1.6)
|
19
|
+
i18n (~> 0.6, >= 0.6.9)
|
20
|
+
json (~> 1.7, >= 1.7.7)
|
21
|
+
minitest (~> 5.1)
|
22
|
+
thread_safe (~> 0.1)
|
23
|
+
tzinfo (~> 1.1)
|
24
|
+
arel (5.0.1.20140414130214)
|
25
|
+
builder (3.2.2)
|
26
|
+
i18n (0.7.0)
|
27
|
+
json (1.8.2)
|
28
|
+
minitest (5.5.1)
|
29
|
+
pg (0.17.1)
|
30
|
+
rake (10.4.2)
|
31
|
+
thread_safe (0.3.4)
|
32
|
+
tzinfo (1.2.2)
|
24
33
|
thread_safe (~> 0.1)
|
25
|
-
tzinfo (~> 0.3.37)
|
26
|
-
arel (4.0.0)
|
27
|
-
atomic (1.1.14)
|
28
|
-
builder (3.1.4)
|
29
|
-
i18n (0.6.5)
|
30
|
-
minitest (4.7.5)
|
31
|
-
multi_json (1.8.0)
|
32
|
-
pg (0.17.0)
|
33
|
-
rake (10.1.0)
|
34
|
-
thread_safe (0.1.3)
|
35
|
-
atomic
|
36
|
-
tzinfo (0.3.37)
|
37
34
|
|
38
35
|
PLATFORMS
|
39
36
|
ruby
|
data/README.md
CHANGED
@@ -1,54 +1,85 @@
|
|
1
|
-
#
|
1
|
+
# Ltree Hierarchy
|
2
2
|
|
3
3
|
A simplistic gem that allows ActiveRecord models to be organized in a tree or hierarchy. It uses a materialized path implementation based around PostgreSQL's [ltree](http://www.postgresql.org/docs/current/static/ltree.html) data type, associated functions and operators.
|
4
4
|
|
5
|
-
[](https://travis-ci.org/robworley/ltree_hierarchy)
|
6
|
-
|
7
5
|
## Why might you want to use it?
|
8
6
|
|
9
7
|
- You want to be able to construct optimized hierarchical queries with ease, both from Ruby AND raw SQL.
|
10
8
|
- You want to be able to compose complex arel expressions from pre-defined building blocks.
|
11
9
|
- You prefer PostgreSQL over other relational DBs.
|
12
10
|
|
13
|
-
##
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'ltree_hierarchy'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Add ltree extension to PostgreSQL:
|
22
|
+
|
23
|
+
$ psql -U postgres -d my_database
|
24
|
+
-> CREATE EXTENSION IF NOT EXISTS ltree;
|
25
|
+
|
26
|
+
Update your table:
|
14
27
|
|
15
|
-
|
28
|
+
``` ruby
|
29
|
+
class AddLtreeToLocations < ActiveRecord::Migration
|
30
|
+
def self.up
|
31
|
+
add_column :locations, :parent_id, :integer
|
32
|
+
add_column :locations, :path, :ltree
|
16
33
|
|
17
|
-
|
18
|
-
|
19
|
-
- Install required gems: **bundle install**
|
20
|
-
2. Add parent_id (integer) and path (ltree) columns to your table.
|
21
|
-
3. Add ltree hierarchy to your model
|
22
|
-
- Add to app/models/[model].rb: has_ltree_hierarchy
|
34
|
+
add_index :locations, :parent_id
|
35
|
+
end
|
23
36
|
|
24
|
-
|
37
|
+
def self.down
|
38
|
+
remove_index :locations, :parent_id
|
39
|
+
remove_column :locations, :parent_id
|
40
|
+
remove_column :locations, :path
|
41
|
+
end
|
42
|
+
end
|
43
|
+
```
|
25
44
|
|
26
|
-
|
45
|
+
Run migrations:
|
27
46
|
|
28
|
-
|
47
|
+
$ bundle exec rake db:migrate
|
29
48
|
|
30
|
-
##
|
49
|
+
## Usage
|
31
50
|
|
32
|
-
|
51
|
+
``` ruby
|
52
|
+
class Location < ActiveRecord::Base
|
53
|
+
has_ltree_hierarchy
|
54
|
+
end
|
33
55
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
- siblings
|
38
|
-
- self_and_siblings
|
39
|
-
- children
|
40
|
-
- self_and_children
|
41
|
-
- descendents
|
42
|
-
- self_and_descendents
|
43
|
-
- leaves
|
56
|
+
root = Location.create!(name: 'UK')
|
57
|
+
child = Location.create!(name: 'London', parent: root)
|
58
|
+
subchild = Location.create!(name: 'Hackney', parent: child)
|
44
59
|
|
45
|
-
|
60
|
+
root.parent # => nil
|
61
|
+
child.parent # => root
|
62
|
+
root.children # => [child]
|
63
|
+
root.children.first.children.first # => subchild
|
64
|
+
subchild.root # => root
|
65
|
+
```
|
46
66
|
|
47
|
-
- roots
|
48
|
-
- leaves
|
49
|
-
- at_depth(n)
|
50
|
-
- lowest_common_ancestors(scope)
|
67
|
+
- `.roots`
|
68
|
+
- `.leaves`
|
69
|
+
- `.at_depth(n)`
|
70
|
+
- `.lowest_common_ancestors(scope)`
|
71
|
+
- `#parent`
|
72
|
+
- `#ancestors`
|
73
|
+
- `#self_and_ancestors`
|
74
|
+
- `#siblings`
|
75
|
+
- `#self_and_siblings`
|
76
|
+
- `#children`
|
77
|
+
- `#self_and_children`
|
78
|
+
- `#descendents`
|
79
|
+
- `#self_and_descendents`
|
80
|
+
- `#leaves`
|
51
81
|
|
52
82
|
## TODO
|
53
83
|
|
54
|
-
- Better error message for circular references.
|
84
|
+
- [ ] Better error message for circular references.
|
85
|
+
- [ ] Don't neglect i18n.
|
@@ -133,7 +133,7 @@ module Ltree
|
|
133
133
|
end
|
134
134
|
|
135
135
|
def leaf?
|
136
|
-
!children.
|
136
|
+
!children.exists?
|
137
137
|
end
|
138
138
|
|
139
139
|
def depth # 1-based, for compatibility with ltree's nlevel().
|
@@ -146,6 +146,10 @@ module Ltree
|
|
146
146
|
end
|
147
147
|
end
|
148
148
|
|
149
|
+
def root
|
150
|
+
ltree_scope.where("#{ltree_path_column} = subpath(?, 0, 1)", ltree_path).first
|
151
|
+
end
|
152
|
+
|
149
153
|
def ancestors
|
150
154
|
ltree_scope.where("#{ltree_path_column} @> ? AND #{ltree_fragment_column} != ?", ltree_path, ltree_fragment)
|
151
155
|
end
|
metadata
CHANGED
@@ -1,55 +1,69 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ltree_hierarchy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rob Worley
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-05-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pg
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activerecord
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: 3.1.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 3.1.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: rake
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
44
58
|
requirements:
|
45
|
-
- -
|
59
|
+
- - ">="
|
46
60
|
- !ruby/object:Gem::Version
|
47
61
|
version: '0'
|
48
62
|
type: :development
|
49
63
|
prerelease: false
|
50
64
|
version_requirements: !ruby/object:Gem::Requirement
|
51
65
|
requirements:
|
52
|
-
- -
|
66
|
+
- - ">="
|
53
67
|
- !ruby/object:Gem::Version
|
54
68
|
version: '0'
|
55
69
|
description: Organizes ActiveRecord models into a tree/hierarchy using a materialized
|
@@ -61,14 +75,14 @@ executables: []
|
|
61
75
|
extensions: []
|
62
76
|
extra_rdoc_files: []
|
63
77
|
files:
|
64
|
-
- lib/ltree_hierarchy/hierarchy.rb
|
65
|
-
- lib/ltree_hierarchy/version.rb
|
66
|
-
- lib/ltree_hierarchy.rb
|
67
78
|
- Gemfile
|
68
79
|
- Gemfile.lock
|
69
80
|
- MIT-LICENSE
|
70
|
-
- Rakefile
|
71
81
|
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- lib/ltree_hierarchy.rb
|
84
|
+
- lib/ltree_hierarchy/hierarchy.rb
|
85
|
+
- lib/ltree_hierarchy/version.rb
|
72
86
|
homepage: https://github.com/robworley/ltree_hierarchy
|
73
87
|
licenses:
|
74
88
|
- MIT
|
@@ -79,17 +93,17 @@ require_paths:
|
|
79
93
|
- lib
|
80
94
|
required_ruby_version: !ruby/object:Gem::Requirement
|
81
95
|
requirements:
|
82
|
-
- -
|
96
|
+
- - ">="
|
83
97
|
- !ruby/object:Gem::Version
|
84
98
|
version: '0'
|
85
99
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
100
|
requirements:
|
87
|
-
- -
|
101
|
+
- - ">="
|
88
102
|
- !ruby/object:Gem::Version
|
89
103
|
version: '0'
|
90
104
|
requirements: []
|
91
105
|
rubyforge_project: ltree_hierarchy
|
92
|
-
rubygems_version: 2.
|
106
|
+
rubygems_version: 2.4.7
|
93
107
|
signing_key:
|
94
108
|
specification_version: 4
|
95
109
|
summary: Organize ActiveRecord models into a tree using PostgreSQL's ltree datatype
|