Module: ActsAsDAG::Deprecated::HelperMethods
- Defined in:
- lib/acts_as_dag/deprecated.rb
Class Method Summary collapse
- .matching_word_count(current, other) ⇒ Object
-
.plinko(current, other) ⇒ Object
Searches the subtree for the best parent for the other i.e.
-
.plinko_multiple(current, others) ⇒ Object
Convenience method for plinkoing multiple categories Plinko’s multiple categories from shortest to longest in order to prevent the need for reorganization.
-
.should_descend_from?(current, other) ⇒ Boolean
Checks if other should descend from
current
based on name matching Returns true if other contains all the words fromcurrent
, but has words that are not contained incurrent
. -
.unique_name_portion(current) ⇒ Object
Returns the portion of this category’s name that is not present in any of it’s parents.
- .word_count(current) ⇒ Object
Class Method Details
.matching_word_count(current, other) ⇒ Object
113 114 115 116 117 |
# File 'lib/acts_as_dag/deprecated.rb', line 113 def self.matching_word_count(current, other) other_words = other.name.split self_words = current.name.split return (other_words & self_words).count end |
.plinko(current, other) ⇒ Object
Searches the subtree for the best parent for the other i.e. it lets you drop the category in at the top and it drops down the list until it finds its final resting place
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/acts_as_dag/deprecated.rb', line 51 def self.plinko(current, other) # ActiveRecord::Base.logger.info { "Plinkoing '#{other.name}' into '#{current.name}'..." } if should_descend_from?(current, other) # Find the subtree of the current category that +other+ should descend from subtree_other_should_descend_from = current.subtree.select{|record| should_descend_from?(record, other) } # Of those, find the categories with the most number of matching words and make +other+ their child # We find all suitable candidates to provide support for categories whose names are permutations of each other # e.g. 'goat wool fibre' should be a child of 'goat wool' and 'wool goat' if both are present under 'goat' new_parents_group = subtree_other_should_descend_from.group_by{|category| matching_word_count(other, category)}.sort.reverse.first if new_parents_group.present? for new_parent in new_parents_group[1] ActiveRecord::Base.logger.info { " '#{other.name}' landed under '#{new_parent.name}'" } other.add_parent(new_parent) # We've just affected the associations in ways we can not possibly imagine, so let's clear the association cache current.clear_association_cache end return true end end end |
.plinko_multiple(current, others) ⇒ Object
Convenience method for plinkoing multiple categories Plinko’s multiple categories from shortest to longest in order to prevent the need for reorganization
75 76 77 78 79 80 81 82 83 |
# File 'lib/acts_as_dag/deprecated.rb', line 75 def self.plinko_multiple(current, others) groups = others.group_by{|category| word_count(category)}.sort groups.each do |word_count, categories| categories.each do |category| unless plinko(current, category) end end end end |
.should_descend_from?(current, other) ⇒ Boolean
Checks if other should descend from current
based on name matching Returns true if other contains all the words from current
, but has words that are not contained in current
99 100 101 102 103 104 105 106 107 |
# File 'lib/acts_as_dag/deprecated.rb', line 99 def self.should_descend_from?(current, other) return false if current == other other_words = other.name.split current_words = current.name.split # (other contains all the words from current and more) && (current contains no words that are not also in other) return (other_words - (current_words & other_words)).count > 0 && (current_words - other_words).count == 0 end |
.unique_name_portion(current) ⇒ Object
Returns the portion of this category’s name that is not present in any of it’s parents
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/acts_as_dag/deprecated.rb', line 86 def self.unique_name_portion(current) unique_portion = current.name.split for parent in current.parents for word in parent.name.split unique_portion.delete(word) end end return unique_portion.empty? ? nil : unique_portion.join(' ') end |
.word_count(current) ⇒ Object
109 110 111 |
# File 'lib/acts_as_dag/deprecated.rb', line 109 def self.word_count(current) current.name.split.count end |