Class: Containers::AutoassociativeArray

Inherits:
Object
  • Object
show all
Defined in:
lib/duct_tape/autoassociative_array.rb

Instance Method Summary collapse

Constructor Details

#initializeAutoassociativeArray

Returns a new instance of AutoassociativeArray.



3
4
5
6
7
8
# File 'lib/duct_tape/autoassociative_array.rb', line 3

def initialize
  @values = []
  @columns = Hash.new { |hsh,key| hsh[key] = {} }
  @hash = Hash.new { |hsh,key| hsh[key] = [] }
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



54
55
56
57
58
# File 'lib/duct_tape/autoassociative_array.rb', line 54

def method_missing(*args, &block)
  ret = @values.__send__(*args, &block)
  rebuild_hash
  ret
end

Instance Method Details

#<<(ary) ⇒ Object



81
82
83
# File 'lib/duct_tape/autoassociative_array.rb', line 81

def <<(ary)
  insert(*ary)
end

#[](*args) ⇒ Object



37
38
39
# File 'lib/duct_tape/autoassociative_array.rb', line 37

def [](*args)
  match_impl(*args)
end

#by_column(col, key) ⇒ Object



41
42
43
# File 'lib/duct_tape/autoassociative_array.rb', line 41

def by_column(col, key)
  (@columns.has_key?(col) && @columns[col].has_key?(key)) ? @columns[col][key] : nil
end

#clearObject



60
61
62
63
64
65
# File 'lib/duct_tape/autoassociative_array.rb', line 60

def clear
  @hash.clear
  @values.clear
  @columns.clear
  self
end

#dupObject



75
76
77
78
79
# File 'lib/duct_tape/autoassociative_array.rb', line 75

def dup
  ret = self.class.new
  @values.each { |set| ret.insert(*set) }
  ret
end

#empty?Boolean

Returns:



45
46
47
# File 'lib/duct_tape/autoassociative_array.rb', line 45

def empty?
  @values.empty?
end

#insert(*args) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/duct_tape/autoassociative_array.rb', line 10

def insert(*args)
  @values |= [args]
  args.size.times do |i|
    @columns[i][@values[-1][i]] = @values[-1]
    @hash[@values[-1][i]].push(@values[-1])
  end
  self
end

#inspectObject



67
68
69
# File 'lib/duct_tape/autoassociative_array.rb', line 67

def inspect
  @values.inspect
end

#lengthObject Also known as: size



49
50
51
# File 'lib/duct_tape/autoassociative_array.rb', line 49

def length
  @values.size
end

#partial_match(*args) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/duct_tape/autoassociative_array.rb', line 19

def partial_match(*args)
  matches = {}
  ret = []
  (1..args.size).reverse_each do |i|
    args.combination(i) do |subset|
      check = match_impl(*subset)
      matches[subset] = check unless check.empty?
    end
    most_matches = matches.map { |k,v| v.size }.max
    matches.reject! { |k,v| v.size < most_matches }
    unless matches.empty?
      matches.each { |k,v| ret |= v }
      break
    end
  end
  ret
end

#to_sObject



71
72
73
# File 'lib/duct_tape/autoassociative_array.rb', line 71

def to_s
  @values.to_s
end