Class: Taxonomite::Taxonomy
- Inherits:
-
Object
- Object
- Taxonomite::Taxonomy
- 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
-
#add(parent, child) ⇒ Object
try to add a child to a parent, with validation by this Taxonomy.
-
#belongs_under?(parent, child) ⇒ Boolean
see if this node belongs directly under a particular parent; this allows for assignment within a hierarchy.
-
#find_owner(node, root = nil) ⇒ Taxonomite::Node
Find the direct owner of a node within the tree.
-
#is_valid_down_relation?(parent, child) ⇒ Boolean
verify according to the down-looking taxonomy hash.
-
#is_valid_relation?(parent, child, require_both = self.require_both) ⇒ Boolean
determine whether the parent is a valid parent for the child.
-
#is_valid_up_relation?(parent, child) ⇒ Boolean
verify according to the down-looking taxonomy hash.
-
#valid_child_types(parent) ⇒ Array
access the appropriate child entity_types for a particular parent or parent entity_type.
-
#valid_parent_types(child) ⇒ Array
access the appropriate parent entity_types for a particular child or child entity_type.
Methods included from ConfiguredGlobally
Instance Method Details
#add(parent, child) ⇒ Object
try to add a child to a parent, with validation by this Taxonomy
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).
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.
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.
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.
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.
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
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
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 |