Class: One::Pivoter
Overview
Class that can be used for mining data from lists of objects.
Instance Method Summary collapse
-
#multi_pivot(list, *pivots) ⇒ Hash
Runs multiple pivots against a list of Objects.
-
#pivot(list, options = {}) {|item| ... } ⇒ Hash
Pivots a list of Objects grouping them into an organized Hash.
Instance Method Details
#multi_pivot(list, *pivots) ⇒ Hash
Runs multiple pivots against a list of Objects.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/one/pivoter.rb', line 93 def multi_pivot(list, *pivots) = pivots.pop if pivots.last.is_a?(Hash) ||= {} delimiter = [:delimiter] || "[PIVOT]" pivoted = nil pass = 0 while pivots.length > 0 p = pivots.shift # handle the case where the pivots are One::Pivot objects = {} if p.is_a?(One::Pivot) [:identifier] = p.identifier p = p.pivot_proc end if pass == 0 pivoted = pivot(list, , &p) else new_pivoted = {} pivoted.each do |old_key, old_list| tmp_pivoted = pivot(old_list, , &p) tmp_pivoted.each do |key, list| new_key = "#{safe_key(old_key)}#{delimiter}#{safe_key(key)}" new_pivoted[new_key] = list end end pivoted = new_pivoted end pass += 1 end pivoted end |
#pivot(list, options = {}) {|item| ... } ⇒ Hash
Pivots a list of Objects grouping them into an organized Hash.
29 30 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 56 |
# File 'lib/one/pivoter.rb', line 29 def pivot(list, ={}, &block) pivoted = {} list.each do |item| value = yield(item) # notify observers that a pivot block was just called identifier = [:identifier] || "#{item.hash}:#{block.hash}" changed notify_observers(identifier, item, value) if value.is_a?(Array) if value.empty? pivoted[nil] ||= [] pivoted[nil] << item else value.each do |val| pivoted[val] ||= [] pivoted[val] << item end end else pivoted[value] ||= [] pivoted[value] << item end end pivoted end |