parentry 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f3f1eabb0151c4d46831ffbe3d442403509888d0
4
+ data.tar.gz: 9c049edc49491eb47f152c175cadf24e118d7b11
5
+ SHA512:
6
+ metadata.gz: 448cc42620b9053eaec75d101bd2eaf3a4595429e013b39cc4e712bba955b656a6eb8be92f2255c9ec1a10b5b46f10adb3071a12fa629ec315ae082e450c6a50
7
+ data.tar.gz: 525a7bc48fd91ab28519d78add1fbb94f70baf782d071678c16648e22b7abda76117513bda2599b633976f50d4312b3efc6ccea68a241efe9bf6d5a3fff4e146
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea
11
+ .ruby-gemset
12
+ .ruby-version
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format progress
2
+ --color
3
+ --order rand
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in parentry.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Hamed Asghari
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Parentry
2
+
3
+ Parentry in its current incarnation is an ActiveRecord extension for the [PostgreSQL ltree](http://www.postgresql.org/docs/9.4/static/ltree.html) module.
4
+
5
+ The API is inspired by the popular [ancestry](https://github.com/stefankroes/ancestry) gem and mirrors (with a few exceptions) the methods available in that gem.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'parentry'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install parentry
22
+
23
+ ## Usage
24
+
25
+ This gem requires 2 columns for each table: `parentry` and `parent_id`. A sample migration would look like:
26
+
27
+ ```ruby
28
+ add_column :my_table, :parentry, :ltree
29
+ add_column :my_table, :parent_id, :integer
30
+ ```
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hasghari/parentry. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
41
+
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
46
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'parentry'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ module Parentry
2
+ module ClassMethods
3
+ def parentry(options = {})
4
+ self.parentry_column = options.fetch(:parentry_column, 'parentry')
5
+ self.depth_offset = options.fetch(:depth_offset, 0)
6
+ self.cache_depth = options.fetch(:cache_depth, false)
7
+ end
8
+
9
+ def arrange(options = {})
10
+ scope =
11
+ if (order = options.delete(:order))
12
+ self.base_class.order_by_parentry.order(order)
13
+ else
14
+ self.base_class.order_by_parentry
15
+ end
16
+
17
+ scope.where(options).each_with_object(Hash.new { |h, k| h[k] = {} }) do |node, memo|
18
+ insert_node = node.ancestor_ids.reduce(memo) do |subtree, ancestor_id|
19
+ match = subtree.find { |parent, _children| parent.id == ancestor_id }
20
+ match ? match[1] : subtree
21
+ end
22
+ insert_node[node] = {}
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,44 @@
1
+ module Parentry
2
+ module InstanceMethods
3
+ def parentry_scope
4
+ self.class.base_class
5
+ end
6
+
7
+ def prevent_circular_parentry
8
+ computed = compute_parentry
9
+ errors.add(:parentry, 'contains a circular reference') unless computed.split('.').uniq == computed.split('.')
10
+ end
11
+
12
+ def commit_parentry
13
+ update_column(parentry_column, compute_parentry)
14
+ end
15
+
16
+ def assign_parentry
17
+ write_attribute(parentry_column, compute_parentry)
18
+ end
19
+
20
+ def cache_parentry_depth
21
+ update_column(:parentry_depth, depth)
22
+ end
23
+
24
+ def cascade_parentry
25
+ old_path, new_path = changes[parentry_column]
26
+ parentry_scope.where(
27
+ ["#{parentry_column} <@ :ltree AND id != :id", ltree: old_path, id: id]
28
+ ).update_all(
29
+ [
30
+ "#{parentry_column} = :new_path || subpath(#{parentry_column}, nlevel(:old_path))",
31
+ new_path: new_path, old_path: old_path
32
+ ]
33
+ )
34
+ end
35
+
36
+ def compute_parentry
37
+ parent.present? ? "#{parent.parentry}.#{id}" : "#{id}"
38
+ end
39
+
40
+ def parentry
41
+ read_attribute(parentry_column)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,96 @@
1
+ module Parentry
2
+ module Navigation
3
+ def root_id
4
+ path_ids[0]
5
+ end
6
+
7
+ def root
8
+ unscoped_find(root_id)
9
+ end
10
+
11
+ def root?
12
+ root_id == id
13
+ end
14
+
15
+ def path_ids
16
+ parentry.split('.').map(&:to_i)
17
+ end
18
+
19
+ def path(scopes = {})
20
+ with_depth_scopes(scopes) do
21
+ parentry_scope.where(id: path_ids).order_by_parentry
22
+ end
23
+ end
24
+
25
+ def ancestor_ids
26
+ path_ids[0..-2]
27
+ end
28
+
29
+ def ancestors(scopes = {})
30
+ with_depth_scopes(scopes) do
31
+ parentry_scope.where(id: ancestor_ids).order_by_parentry
32
+ end
33
+ end
34
+
35
+ def leaf?
36
+ children.size == 0
37
+ end
38
+ alias_method :childless?, :leaf?
39
+
40
+ def children?
41
+ !leaf?
42
+ end
43
+
44
+ def sibling_ids
45
+ siblings.pluck(:id)
46
+ end
47
+
48
+ def siblings
49
+ parentry_scope.where.not(id: id).where.not(parent_id: nil).where(parent_id: parent_id)
50
+ end
51
+
52
+ def siblings?
53
+ siblings.size > 0
54
+ end
55
+
56
+ def only_child?
57
+ !siblings?
58
+ end
59
+
60
+ def subtree_ids(scopes = {})
61
+ subtree(scopes).pluck(:id)
62
+ end
63
+
64
+ def subtree(scopes = {})
65
+ with_depth_scopes(scopes) do
66
+ parentry_scope.where("#{parentry_column} <@ ?", parentry)
67
+ end
68
+ end
69
+
70
+ def descendant_ids(scopes = {})
71
+ descendants(scopes).pluck(:id)
72
+ end
73
+
74
+ def descendants(scopes = {})
75
+ with_depth_scopes(scopes) do
76
+ subtree.where.not(id: id)
77
+ end
78
+ end
79
+
80
+ def depth
81
+ path_ids.size - 1 + depth_offset
82
+ end
83
+
84
+ private
85
+
86
+ def unscoped_find(id)
87
+ parentry_scope.unscoped { parentry_scope.find(id) }
88
+ end
89
+
90
+ def with_depth_scopes(scopes)
91
+ scopes.reduce(yield) do |memo, (scope, depth)|
92
+ memo.send(scope, depth)
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,3 @@
1
+ module Parentry
2
+ VERSION = '0.2.0'
3
+ end
data/lib/parentry.rb ADDED
@@ -0,0 +1,48 @@
1
+ require 'parentry/version'
2
+
3
+ module Parentry
4
+ autoload :InstanceMethods, 'parentry/instance_methods'
5
+ autoload :Navigation, 'parentry/navigation'
6
+ autoload :ClassMethods, 'parentry/class_methods'
7
+
8
+ def self.included(base)
9
+ base.class_eval do
10
+ mattr_accessor :parentry_column, :depth_offset, :cache_depth
11
+
12
+ belongs_to :parent, class_name: base_class.name
13
+ has_many :children, class_name: base_class.name, foreign_key: :parent_id, dependent: :destroy
14
+
15
+ validate do
16
+ unless parent.blank? || parent.persisted?
17
+ errors.add(:parent, 'must be persisted')
18
+ end
19
+ end
20
+
21
+ validate :prevent_circular_parentry
22
+
23
+ after_create :commit_parentry
24
+ before_update :assign_parentry, if: proc { changes[:parent_id].present? }
25
+ after_update :cascade_parentry, if: proc { changes[parentry_column].present? }
26
+ after_save :cache_parentry_depth, if: proc { cache_depth && depth != parentry_depth }
27
+
28
+ scope :order_by_parentry, -> { order("nlevel(#{parentry_column})") }
29
+
30
+ scope :before_depth, ->(depth) { where("nlevel(#{parentry_column}) - 1 < ?", depth + depth_offset) }
31
+ scope :to_depth, ->(depth) { where("nlevel(#{parentry_column}) - 1 <= ?", depth + depth_offset) }
32
+ scope :at_depth, ->(depth) { where("nlevel(#{parentry_column}) - 1 = ?", depth + depth_offset) }
33
+ scope :from_depth, ->(depth) { where("nlevel(#{parentry_column}) - 1 >= ?", depth + depth_offset) }
34
+ scope :after_depth, ->(depth) { where("nlevel(#{parentry_column}) - 1 > ?", depth + depth_offset) }
35
+
36
+ scope :roots, -> { where("nlevel(#{parentry_column}) = 1") }
37
+ scope :ancestors_of, ->(node) { where("#{parentry_column} @> ?", node.parentry).where.not(id: node.id) }
38
+ scope :children_of, ->(node) { where(parent_id: node.id) }
39
+ scope :descendants_of, ->(node) { subtree_of(node).where.not(id: node.id) }
40
+ scope :subtree_of, ->(node) { where("#{parentry_column} <@ ?", node.parentry) }
41
+ scope :siblings_of, ->(node) { where(parent_id: node.parent_id).where.not(id: node.id) }
42
+ end
43
+
44
+ base.send :include, Navigation
45
+ base.send :include, InstanceMethods
46
+ base.extend ClassMethods
47
+ end
48
+ end
data/parentry.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'parentry/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'parentry'
8
+ spec.version = Parentry::VERSION
9
+ spec.authors = ['Hamed Asghari']
10
+ spec.email = ['hasghari@gmail.com']
11
+
12
+ spec.summary = %q{ActiveRecord adapter for the Postgres ltree module}
13
+ spec.homepage = 'https://github.com/hasghari/parentry'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_dependency 'activerecord', '>= 3.1.0'
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.10'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec-rails'
24
+ spec.add_development_dependency 'pg'
25
+ spec.add_development_dependency 'combustion', '>= 0.5.2'
26
+ spec.add_development_dependency 'database_cleaner'
27
+ spec.add_development_dependency 'pry'
28
+ end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parentry
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Hamed Asghari
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pg
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: combustion
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 0.5.2
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 0.5.2
97
+ - !ruby/object:Gem::Dependency
98
+ name: database_cleaner
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description:
126
+ email:
127
+ - hasghari@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".travis.yml"
135
+ - CODE_OF_CONDUCT.md
136
+ - Gemfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - bin/console
141
+ - bin/setup
142
+ - lib/parentry.rb
143
+ - lib/parentry/class_methods.rb
144
+ - lib/parentry/instance_methods.rb
145
+ - lib/parentry/navigation.rb
146
+ - lib/parentry/version.rb
147
+ - parentry.gemspec
148
+ homepage: https://github.com/hasghari/parentry
149
+ licenses:
150
+ - MIT
151
+ metadata: {}
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ requirements: []
167
+ rubyforge_project:
168
+ rubygems_version: 2.4.8
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: ActiveRecord adapter for the Postgres ltree module
172
+ test_files: []