Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jackc/edge
Edge provides graph functionality to ActiveRecord.
https://github.com/jackc/edge
Last synced: 11 days ago
JSON representation
Edge provides graph functionality to ActiveRecord.
- Host: GitHub
- URL: https://github.com/jackc/edge
- Owner: jackc
- License: mit
- Created: 2012-03-11T02:38:44.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2021-02-20T15:39:50.000Z (over 3 years ago)
- Last Synced: 2024-10-14T21:52:50.043Z (23 days ago)
- Language: Ruby
- Homepage:
- Size: 57.6 KB
- Stars: 90
- Watchers: 4
- Forks: 10
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Edge
[![Build Status](https://travis-ci.org/jackc/edge.svg?branch=master)](https://travis-ci.org/jackc/edge)
Edge provides graph functionality to ActiveRecord using recursive common table
expressions. It has only been tested with PostgreSQL, but it uses Arel for
SQL generation so it should work with any database and adapter that support
recursive CTEs.acts_as_forest enables an entire tree or even an entire forest of trees to
be loaded in a single query. All parent and children associations are
preloaded.## Installation
Add this line to your application's Gemfile:
gem 'edge'
And then execute:
$ bundle
Or install it yourself as:
$ gem install edge
## Usage
acts_as_forest adds tree / multi-tree functionality. All it needs a parent_id
column. This can be overridden by passing a :foreign_key option to
acts_as_forest.class Location < ActiveRecord::Base
acts_as_forest :order => "name"
endusa = Location.create! :name => "USA"
illinois = usa.children.create! :name => "Illinois"
chicago = illinois.children.create! :name => "Chicago"
indiana = usa.children.create! :name => "Indiana"
canada = Location.create! :name => "Canada"
british_columbia = canada.children.create! :name => "British Columbia"Location.root.all # [usa, canada]
Location.find_forest # [usa, canada] with all children and parents preloaded
Location.find_tree usa.id # load a single tree.It also provides the with_descendants scope to get all currently selected
nodes and all their descendents. It can be chained after where scopes, but
must not be used after any other type of scope.Location.where(name: "Illinois").with_descendants.all # [illinois, chicago]
Also supported is `ancestors` instance method which returns an array of all ancestors ordered by nearest ancestors first.
city = Location.where(name: 'Chicago')
ancestors = city.ancestors # [illinois, usa]## Benchmarks
Edge includes a performance benchmarks. You can create test forests with a
configurable number of trees, depth, number of children per node, and
size of payload per node.jack@moya:~/work/edge$ ruby -I lib -I bench bench/forest_find.rb --help
Usage: forest_find [options]
-t, --trees NUM Number of trees to create
-d, --depth NUM Depth of trees
-c, --children NUM Number of children per node
-p, --payload NUM Characters of payload per nodeEven on slower machines entire trees can be loaded quickly.
jack@moya:~/work/edge$ ruby -I lib -I bench bench/forest_find.rb
Trees: 50
Depth: 3
Children per node: 10
Payload characters per node: 16
Descendants per tree: 110
Total records: 5550
user system total real
Load entire forest 10 times 4.260000 0.010000 4.270000 ( 4.422442)
Load one tree 100 times 0.830000 0.040000 0.870000 ( 0.984642)### Running the benchmarks
1. Create a database such as edge_bench.
2. Configure bench/database.yml to connect to it.
3. Load bench/database_structure.sql into your bench database.
4. Run benchmark scripts from root of gem directory (remember to pass ruby
the include paths for lib and bench)## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Added some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request## License
MIT