class Taxonomite::Node

Class which defines a node within a tree hierarchy. Validation on addition of children and parents does occur in this class to provide for class specific validation in subclasses. That said, enforcing a taxonomy (rules about how the hierarchy is constructed) falls to the Taxonomite::Taxonomy class which is used to join parents and children according to specified rules. Thus, if someone does Obj.children << node – no special validation will occur, other than that provided within this class or subclasses via is_valid_child? and is_valid_parent?

Public Instance Methods

add_child(child) click to toggle source

add a child to this object; default is that each parent may have many children; this will validate the child using thetaxonomy object passed in to the field. @param [Taxonomite::Node] child the node to evaluate

# File lib/taxonomite/node.rb, line 87
def add_child(child)
  self.children << child
end
add_parent(parent) click to toggle source

add a parent for this object (default is that each object can have only one parent). this will validate the child using the taxonomy object passed in to the field. @param [Taxonomite::Node] parent the node to evaluate @param [Taxonomite::Taxonomy] taxonomy to use to validate the hierarchy

# File lib/taxonomite/node.rb, line 97
def add_parent(parent)
  parent.add_child(self)
end
evaluate(m) click to toggle source

evaluate a method on the owner of this node (if present). If an owner is not present, then the method is evaluated on this object. In either case a check is made to ensure that the object will respond_to? the method call. If the owner exists but does not respond to the method, then the method is tried on this node object in similar fashion. !!! SHOULD THIS JUST OVERRIDE instance_eval ?? @param [Method] m method to call @return [] the result of the method call, or nil if unable to evaluate

# File lib/taxonomite/node.rb, line 48
def evaluate(m)
  return self.owner.instance_eval(m) if self.owner != nil && self.owner.respond_to?(m)
  return self.instance_eval(m) if self.respond_to?(m)
  nil
end
is_valid_child?(child) click to toggle source

determine whether child is a valid child based upon class evalution (outside of a taxonomy). Default is always true - subclasses should override if validation outside of a separate taxonomy class is desired. @param [Taxonomite::Node] child child to evaluate @return [Boolean] default is true

# File lib/taxonomite/node.rb, line 69
def is_valid_child?(child)
  return true
end
is_valid_parent?(child) click to toggle source

determine whether parent is a valid parent based upon class evalution (outside of a taxonomy). Default is always true - subclasses should override if validation outside of a separate taxonomy class is desired. @param [Taxonomite::Node] parent parent to evaluate @return [Boolean] default is true

# File lib/taxonomite/node.rb, line 79
def is_valid_parent?(child)
  return true
end
remove_child(child) click to toggle source

remove a child from this node. @param [Taxonomite::Node] child node to remove

# File lib/taxonomite/node.rb, line 104
def remove_child(child)
  self.children.delete(child)
end
remove_parent() click to toggle source

remove the parent from this node. This should cause a reciprocal removal of self from the parent's children

# File lib/taxonomite/node.rb, line 111
def remove_parent
  self.parent.remove_child(self) unless self.parent.nil?
end
typeifiedname() click to toggle source

typeify name w entity (i.e. 'Washington state' vs. 'Seattle') @return [String] the typeified name

# File lib/taxonomite/node.rb, line 57
def typeifiedname
  s = self.name
  s += (" " + self.entity_type.capitalize) if self.includetypeinname?
  return s
end

Protected Instance Methods

get_entity_type() click to toggle source

access the entity type for this Object @return [String]

# File lib/taxonomite/node.rb, line 126
def get_entity_type
  'node'
end
includetypeinname?() click to toggle source

include type in the name of this place (i.e. 'Washington state') @return [Boolean]

# File lib/taxonomite/node.rb, line 119
def includetypeinname?
  return false
end

Private Instance Methods

validate_child(child) click to toggle source

determine whether the child is valid for this object; there are two layers to validation 1) provided by this method is_valid_parent? (and subclasses which override is_valid_parent?). @param [Taxonomite::Node] child the parent to validate @return [Boolean] whether validation was successful

# File lib/taxonomite/node.rb, line 137
def validate_child(child)
  raise InvalidParent.create(self, child) unless (!child.nil? && is_valid_child?(child))
  true
end
validate_parent(parent) click to toggle source

determine whether the parent is valid for this object. See description of validate_child for more detail. This method calls validate_child to perform the actual validation. @param [Taxonomite::Node] parent the parent to validate @return [Boolean] whether validation was successful

# File lib/taxonomite/node.rb, line 148
def validate_parent(parent)
  raise InvalidParent.create(parent, self) unless (!parent.nil? && is_valid_parent?(parent))
  parent.validate_child(self)
end