Class: TupleList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tuplelist.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(required_keys, args = {}) ⇒ TupleList

Returns a new instance of TupleList.



6
7
8
9
10
11
12
# File 'lib/tuplelist.rb', line 6

def initialize(required_keys, args = {})
  @keys = required_keys
  @current_tuple = {}
  @tuple_list = []

  @ignore = (args.delete :ignore) || false
end

Instance Attribute Details

#keysObject (readonly)

Returns the value of attribute keys.



2
3
4
# File 'lib/tuplelist.rb', line 2

def keys
  @keys
end

Instance Method Details

#[]=(key, value) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/tuplelist.rb', line 14

def []=(key, value)
  if @keys.include? key
    @current_tuple[key] = value
  else
    raise "Unspecified key '#{key}' entered" unless @ignore
  end
end

#eachObject



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

def each
  @tuple_list.each do |tuple_hash|
    yield tuple_hash.values_at(*@keys)
  end
end

#nextObject



22
23
24
25
26
# File 'lib/tuplelist.rb', line 22

def next
  raise "No keys for #{@current_tuple.keys | @keys} specified" unless @current_tuple.length == @keys.length
  @tuple_list << @current_tuple
  @current_tuple = {}
end