Module: ActsAsDAG::Deprecated::HelperMethods

Defined in:
lib/acts_as_dag/deprecated.rb

Class Method Summary collapse

Class Method Details

.matching_word_count(current, other) ⇒ Object


118
119
120
121
122
# File 'lib/acts_as_dag/deprecated.rb', line 118

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


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/acts_as_dag/deprecated.rb', line 56

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


80
81
82
83
84
85
86
87
88
# File 'lib/acts_as_dag/deprecated.rb', line 80

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

Returns:

  • (Boolean)

104
105
106
107
108
109
110
111
112
# File 'lib/acts_as_dag/deprecated.rb', line 104

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


91
92
93
94
95
96
97
98
99
100
# File 'lib/acts_as_dag/deprecated.rb', line 91

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


114
115
116
# File 'lib/acts_as_dag/deprecated.rb', line 114

def self.word_count(current)
  current.name.split.count
end