Class: Goldmine::HashMiner
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Goldmine::HashMiner
- Defined in:
- lib/goldmine/hash_miner.rb
Instance Attribute Summary collapse
-
#goldmine ⇒ Object
Returns the value of attribute goldmine.
Instance Method Summary collapse
-
#assign_mined(name, key, value) ⇒ Object
Assigns a key/value pair to the Hash.
-
#goldmine_key(name, key) ⇒ Object
Creates a key for a pivot-name/key combo.
-
#initialize(hash = {}) ⇒ HashMiner
constructor
A new instance of HashMiner.
-
#pivot(name = nil) {|Object| ... } ⇒ Hash
Further pivots the Hash into mined data.
Constructor Details
#initialize(hash = {}) ⇒ HashMiner
Returns a new instance of HashMiner.
6 7 8 |
# File 'lib/goldmine/hash_miner.rb', line 6 def initialize(hash={}) super hash end |
Instance Attribute Details
#goldmine ⇒ Object
Returns the value of attribute goldmine.
10 11 12 |
# File 'lib/goldmine/hash_miner.rb', line 10 def goldmine @goldmine end |
Instance Method Details
#assign_mined(name, key, value) ⇒ Object
Assigns a key/value pair to the Hash.
57 58 59 60 61 |
# File 'lib/goldmine/hash_miner.rb', line 57 def assign_mined(name, key, value) goldmine_key = goldmine_key(name, key) self[goldmine_key] ||= [] self[goldmine_key] << value end |
#goldmine_key(name, key) ⇒ Object
Creates a key for a pivot-name/key combo.
67 68 69 70 |
# File 'lib/goldmine/hash_miner.rb', line 67 def goldmine_key(name, key) goldmine_key = { name => key } if name goldmine_key ||= key end |
#pivot(name = nil) {|Object| ... } ⇒ Hash
Note:
This method should not be called directly. Call Array#pivot instead.
Further pivots the Hash into mined data. This method is what enables the pivot method chaining.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/goldmine/hash_miner.rb', line 32 def pivot(name=nil, &block) return self unless goldmine reduce(HashMiner.new) do |memo, item| key = item.first value = Goldmine.miner(item.last) value.pivot(name, &block).each do |k, v| if key.is_a? Hash k = { block.to_s => k } unless k.is_a?(Hash) new_key = key.merge(k) else new_key = [key, k].flatten end memo[new_key] = v end memo.goldmine = true memo end end |