Trees with time-travel capabilities. See post for the rationale behind this gem. This one implements the node closures pattern.
Depends on activerecord
and activerecord-import
gems.
Should be compatible with Rails 4 and 5. Should work with sqlite3, postgres and mysql.
Add this line to your application's Gemfile:
gem 'temporalis'
And then execute:
$ bundle
Or install it yourself as:
$ gem install temporalis
Given models Node
and NodeClosure
, do something like this:
require "temporalis"
class Node < ActiveRecord::Base
include Temporalis::ActiveRecord
temporalis_tree closure_class: NodeClosure, use_unprefixed_aliases: true
end
t = Time.current
Node.add_node(t - 3.seconds, 100, nil)
Node.add_node(t - 2.seconds, 100, 101)
Node.add_node(t - 1.second, 102, 101)
Node.descendants(t - 2.seconds, 100) # [101]
Node.descendants(t - 1.second, 100) # [101, 102]
All the methods are always available in prefixed versions (temporalis_add_node
), aliases without prefix are created for convenience.
Node.add_node(timestamp, key, parent_key)
— adds a node to the treeNode.batch_add_nodes(timestamp, tuples)
— adds multiple nodes to the tree (tuples are[key, parent_key]
, and expected to be sorted reasonably, so that you don't add a child before its parent); uses one batch query for insertion instead of multiple smaller onesNode.implode_node(timestamp, key)
— "implodes" a node (removes it from tree, attaches all children to the parent)Node.change_parent(timestamp, key, new_parent_key)
— moves a nodeNode.ancestors(timestamp, key)
— list of ancestor keys, from bottom to topNode.descendants(timestamp, key)
— list of descendant keys, unorderedNode.active_at(timestamp)
— scope of nodes active at timestampNodeClosure.active_at(timestamp)
,NodeClosure.ancestors_of(key)
,NodeClosure.descendants_of(key)
— closure scopes that you probably shouldn't use
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.
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.
Bug reports and pull requests are welcome on GitHub at https://github.com/chloeandisabel/temporalis.
The gem is available as open source under the terms of the MIT License.
-[x] automatic testing -[ ] rails generators -[ ] docs