Class: HashOfArrays

Inherits:
Hash
  • Object
show all
Defined in:
lib/id_hash.rb

Overview

require ‘model’

Direct Known Subclasses

IdHash

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ HashOfArrays

Returns a new instance of HashOfArrays.



4
5
6
7
8
# File 'lib/id_hash.rb', line 4

def initialize(hash = {})
  hash.each do |key, array|
    self[key] = hash[key].clone
  end
end

Class Method Details

.join(*hashes) ⇒ Object



33
34
35
36
# File 'lib/id_hash.rb', line 33

def self.join(*hashes)
  result = self.new
  result.join(*hashes)
end

Instance Method Details

#add(key, *values) ⇒ Object



28
29
30
31
# File 'lib/id_hash.rb', line 28

def add(key, *values)
  self[key] = [] if self[key].nil?
  self[key].concat(values)
end

#cloneObject



10
11
12
# File 'lib/id_hash.rb', line 10

def clone
  self.class.new(self)
end

#join(*hashes) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/id_hash.rb', line 38

def join(*hashes)
  hashes.each do |hash|
    hash.each do |key, array|
      self.add(key, *array)
    end
  end
  self
end

#sort_arrays!Object



47
48
49
50
51
# File 'lib/id_hash.rb', line 47

def sort_arrays!
  self.each do |key, array|
    array.sort!
  end
end

#subtract(hash) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/id_hash.rb', line 14

def subtract(hash)
  result = self.clone
  hash.each do |key, array|
    next if result[key].nil?

    array.each do |el|
      result[key].delete(el)
    end

    result.delete(key) if result[key].empty?
  end
  result
end