Class: DependencyTree::Tree
- Inherits:
-
Hash
- Object
- Hash
- DependencyTree::Tree
- Defined in:
- lib/dependency_tree.rb
Instance Method Summary collapse
- #call_method_recursive(method, *args, &block) ⇒ Object
- #deep_tree_clone ⇒ Object
- #delete_key_recursive(key) ⇒ Object
- #do_recursive(&block) ⇒ Object
- #ids_tree ⇒ Object
-
#initialize(hash = {}) ⇒ Tree
constructor
A new instance of Tree.
- #last_to_beginning! ⇒ Object
- #root_referenced_earlier_summary ⇒ Object
- #root_summary ⇒ Object
- #status_tree ⇒ Object
- #status_tree_condensed ⇒ Object
Constructor Details
#initialize(hash = {}) ⇒ Tree
Returns a new instance of Tree.
5 6 7 8 9 |
# File 'lib/dependency_tree.rb', line 5 def initialize(hash={}) hash.each do |key, value| self[key] = value end end |
Instance Method Details
#call_method_recursive(method, *args, &block) ⇒ Object
94 95 96 97 98 |
# File 'lib/dependency_tree.rb', line 94 def call_method_recursive(method, *args, &block) do_recursive do |tree| tree.send(method, *args, &block) end end |
#deep_tree_clone ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/dependency_tree.rb', line 72 def deep_tree_clone hash = self.clone.map do |key, array| next [key, array] unless array.class == Array new_array = array.map do |subtree| if subtree.class == Tree subtree.deep_tree_clone else subtree end end [key, new_array] end Tree.new(hash) end |
#delete_key_recursive(key) ⇒ Object
90 91 92 |
# File 'lib/dependency_tree.rb', line 90 def delete_key_recursive(key) call_method_recursive(:delete, key) end |
#do_recursive(&block) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/dependency_tree.rb', line 100 def do_recursive(&block) block.call(self) self.each do |key, array| next unless array.is_a? Array array.each do |subtree| subtree.do_recursive(&block) if subtree.is_a? Tree end end self end |
#ids_tree ⇒ Object
11 12 13 14 |
# File 'lib/dependency_tree.rb', line 11 def ids_tree self_cloned = self.deep_tree_clone self_cloned.delete_key_recursive(:instance) end |
#last_to_beginning! ⇒ Object
57 58 59 60 61 62 |
# File 'lib/dependency_tree.rb', line 57 def last_to_beginning! arr = self.to_a arr.unshift(arr.pop) self.clear self.merge!(arr.to_h) end |
#root_referenced_earlier_summary ⇒ Object
68 69 70 |
# File 'lib/dependency_tree.rb', line 68 def root_referenced_earlier_summary root_summary + ", referenced_earlier" end |
#root_summary ⇒ Object
64 65 66 |
# File 'lib/dependency_tree.rb', line 64 def root_summary "#{self[:class]}, id #{self[:id]}, #{self[:status]}" end |
#status_tree ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/dependency_tree.rb', line 16 def status_tree self_cloned = self.deep_tree_clone self_cloned.do_recursive do |tree| begin tree[:instance].reload tree[:status] = 'present' rescue ActiveRecord::RecordNotFound => e tree[:status] = 'removed' end tree.delete(:instance) end end |
#status_tree_condensed ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/dependency_tree.rb', line 31 def status_tree_condensed result = status_tree.do_recursive do |tree| tree.each do |name, array| next unless array.class == Array new_array = array.map do |subtree| next subtree.root_referenced_earlier_summary if subtree[:referenced_earlier] next subtree if subtree.class != Tree || subtree.size > 3 subtree.root_summary end array.clear array.concat(new_array) end end result.do_recursive do |tree| tree[:_] = tree.root_summary tree.delete(:id) tree.delete(:status) tree.delete(:class) tree.last_to_beginning! end end |