Module: Tunable::Hasher

Defined in:
lib/tunable/hasher.rb

Class Method Summary collapse

Class Method Details

.double_hashify_using(hash, primary_key, secondary_key) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/tunable/hasher.rb', line 20

def self.double_hashify_using(hash, primary_key, secondary_key)
  return {} if hash.empty?
  c = {}
  hash.collect { |e| c[e.send(primary_key).to_sym] = {} }
  hash.collect { |e| c[e.send(primary_key).to_sym][e.send(secondary_key).to_sym] = e.normalized_value }
  c
end

.flatten(hash, primary_key, secondary_key = nil) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/tunable/hasher.rb', line 5

def self.flatten(hash, primary_key, secondary_key = nil)
  if secondary_key.nil?
    hashify_using(hash, primary_key)
  else
    double_hashify_using(hash, primary_key, secondary_key)
  end
end

.hashify_using(hash, key) ⇒ Object



15
16
17
18
# File 'lib/tunable/hasher.rb', line 15

def self.hashify_using(hash, key)
  return {} if hash.empty?
  Hash[*hash.collect { |v| [v.send(key).to_sym, v.normalized_value] }.flatten]
end