class Taxonomite::Taxonomy

Class which enforces a particular hierarchy among objects.

Public Instance Methods

add(parent, child) click to toggle source

try to add a child to a parent, with validation by this Taxonomy @param [Taxonomite::Node] parent the parent node @param [Taxonomite::Node] child the child node

# 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) click to toggle source

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). @param [Taxonomite::Node] child the node to evaluate @param [Taxonomite::Parent] parent the parent node to evaluate for the child

# File lib/taxonomite/taxonomy.rb, line 107
def belongs_under?(parent, child)
  self.find_owner(child, parent) != nil
end
find_owner(node, root = nil) click to toggle source

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. @param [Taxonomite::Node] node the node to evaluate @param [Taxonomite::Node] root the root of the tree to evaluate; if nil will search for the parents of the node first @return [Taxonomite::Node] the appropriate node or nil if none found

# 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) click to toggle source

verify according to the down-looking taxonomy hash. @param [Taxonomite::Node] parent the proposed parent node @param [Taxonomite::Node] child the proposed child node @return [Boolean] whether the child appropriate for the parent, default true

# 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) click to toggle source

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. @param [Taxonomite::Node] parent the proposed parent node @param [Taxonomite::Node] child the proposed child node @param [Boolean] require_both to require both downward and upward match, or just one or the other @return [Boolean] whether the child appropriate for the parent, default true

# 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) click to toggle source

verify according to the down-looking taxonomy hash. @param [Taxonomite::Node] parent the proposed parent node @param [Taxonomite::Node] child the proposed child node @return [Boolean] whether the child appropriate for the parent, default true

# 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) click to toggle source

access the appropriate child entity_types for a particular parent or parent entity_type @param [Taxonomy::Node, String] parent the parent object or entity_type string @return [Array] an array of strings which are the valid child types for the child

# 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) click to toggle source

access the appropriate parent entity_types for a particular child or child entity_type @param [Taxonomy::Node, String] child the child object or entity_type string @return [Array] an array of strings which are the valid parent types for the child

# 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