Class: Taxonomite::Taxonomy

Inherits:
Object
  • Object
show all
Extended by:
ConfiguredGlobally
Includes:
Mongoid::Document
Defined in:
lib/taxonomite/taxonomy.rb

Overview

Class which enforces a particular hierarchy among objects.

Instance Method Summary collapse

Methods included from ConfiguredGlobally

config, reset

Instance Method Details

#add(parent, child) ⇒ Object

try to add a child to a parent, with validation by this Taxonomy

Parameters:



127
128
129
130
# File 'lib/taxonomite/taxonomy.rb', line 127

def add(parent, child)
  raise InvalidChild::create(parent,child) unless self.is_valid_relation?(parent, child)
  parent.add_child(child)
end

#belongs_under?(parent, child) ⇒ Boolean

see if this node belongs directly under a particular parent; this allows for assignment within a hierarchy. Subclasses should override to provide better functionality. Default behavior asks the node if it contains(self).

Parameters:

  • child (Taxonomite::Node)

    the node to evaluate

  • parent (Taxonomite::Parent)

    the parent node to evaluate for the child

Returns:

  • (Boolean)


107
108
109
# File 'lib/taxonomite/taxonomy.rb', line 107

def belongs_under?(parent, child)
  self.find_owner(child, parent) != nil
end

#find_owner(node, root = nil) ⇒ Taxonomite::Node

Find the direct owner of a node within the tree. Returns nil if no direct owner exists within the tree starting at root self. This works down the tree (rather than up.) If root is nil it simply trys all of the Node objects which match the appropriate parent entity_type for node. The default simply finds the first available valid parent depending upon the search method employed.

Parameters:

  • node (Taxonomite::Node)

    the node to evaluate

  • root (Taxonomite::Node) (defaults to: nil)

    the root of the tree to evaluate; if nil will search for the parents of the node first

Returns:



92
93
94
95
96
97
98
99
# File 'lib/taxonomite/taxonomy.rb', line 92

def find_owner(node, root = nil)
  valid_parent_types(node).presence.each do |t|
    getallnodes = lambda { |v| v == '*' ? Taxonomite::Node.find() : Taxonomite::Node.find_by(:entity_type => t) }
    getrootnodes = lambda { |v| v == '*' ? root.self_and_descendants : root.self_and_descendants.find_by(:entity_type => t) }
    ( root.nil? ? getallnodes.call(t) : getrootnodes.call(t) ).each { |n| return n if is_valid_relation(n,node) }
  end
  nil
end

#is_valid_down_relation?(parent, child) ⇒ Boolean

verify according to the down-looking taxonomy hash.

Parameters:

Returns:

  • (Boolean)

    whether the child appropriate for the parent, default true



22
23
24
25
# File 'lib/taxonomite/taxonomy.rb', line 22

def is_valid_down_relation?(parent, child)
  [self.down_taxonomy[parent.entity_type]].map { |t| return true if [child.entity_type, "*"].include?(t) }
  false
end

#is_valid_relation?(parent, child, require_both = self.require_both) ⇒ Boolean

determine whether the parent is a valid parent for the child. If no taxonomy is defined (i.e. the hashes are empty) then default is to return true. Requires that both an updward and a downward relation are present if the require_both flag is set (default is true – this can be set via Taxonomite::Configuration). This flag can also be passed into the method here.

Parameters:

  • parent (Taxonomite::Node)

    the proposed parent node

  • child (Taxonomite::Node)

    the proposed child node

  • require_both (Boolean) (defaults to: self.require_both)

    to require both downward and upward match, or just one or the other

Returns:

  • (Boolean)

    whether the child appropriate for the parent, default true



47
48
49
50
51
# File 'lib/taxonomite/taxonomy.rb', line 47

def is_valid_relation?(parent, child, require_both = self.require_both)
  # depending upon the
  require_both ? is_valid_down_relation?(parent, child) && is_valid_up_relation?(parent, child)
               : is_valid_down_relation?(parent, child) || is_valid_up_relation?(parent, child)
end

#is_valid_up_relation?(parent, child) ⇒ Boolean

verify according to the down-looking taxonomy hash.

Parameters:

Returns:

  • (Boolean)

    whether the child appropriate for the parent, default true



32
33
34
35
# File 'lib/taxonomite/taxonomy.rb', line 32

def is_valid_up_relation?(parent, child)
  [self.up_taxonomy[child.entity_type]].map { |t| return true if [parent.entity_type, "*"].include?(t) }
  false
end

#valid_child_types(parent) ⇒ Array

access the appropriate child entity_types for a particular parent or parent entity_type

Parameters:

  • parent (Taxonomy::Node, String)

    the parent object or entity_type string

Returns:

  • (Array)

    an array of strings which are the valid child types for the child



67
68
69
70
71
# File 'lib/taxonomite/taxonomy.rb', line 67

def valid_child_types(parent)
  # could be a node object, or maybe a string
  str = parent.respond_to?(:entity_type) ? parent.entity_type : child
  self.down_taxonomy[str]
end

#valid_parent_types(child) ⇒ Array

access the appropriate parent entity_types for a particular child or child entity_type

Parameters:

  • child (Taxonomy::Node, String)

    the child object or entity_type string

Returns:

  • (Array)

    an array of strings which are the valid parent types for the child



57
58
59
60
61
# File 'lib/taxonomite/taxonomy.rb', line 57

def valid_parent_types(child)
  # could be a node object, or maybe a string
  str = child.respond_to?(:entity_type) ? child.entity_type : child
  self.up_taxonomy[str]
end