Module: Dagable::Model

Defined in:
lib/dagable/model.rb

Overview

Provides the dagable class method that activates DAG behaviour on an ActiveRecord model. Extend this module in your model, then call dagable to set up edge and ancestry classes, associations, and instance methods.

Usage

class Category < ActiveRecord::Base
  extend Dagable::Model

  dagable
end

Calling dagable will:

  1. Define Category::Edge (subclass of Dagable::Edge) backed by categories_edges

  2. Define Category::Ancestry (subclass of Dagable::Ancestry) backed by categories_ancestries

  3. Include Dagable::Associations setting up has_many relationships for edges and ancestry connections

  4. Include Dagable::InstanceMethods providing add_child, add_parent, traversal methods, etc.

  5. Register an after_create callback to insert the self-referential ancestry row (depth 0) for every new record

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.mutexObject (readonly)

Returns the value of attribute mutex.



39
40
41
# File 'lib/dagable/model.rb', line 39

def mutex
  @mutex
end

Instance Method Details

#dagableObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dagable/model.rb', line 42

def dagable
  Dagable::Model.mutex.synchronize do
    edge_klass = const_set("Edge", Class.new(Dagable::Edge))
    ancestry_klass = const_set("Ancestry", Class.new(Dagable::Ancestry))

    configure_edge_class(edge_klass)
    configure_ancestry_class(ancestry_klass)

    include Dagable::Associations.new(edge_klass.name, ancestry_klass.name)
    include Dagable::InstanceMethods

    after_create :create_self_ancestry_row
  end
end