Class: NWN::TwoDA::Row

Inherits:
Array
  • Object
show all
Defined in:
lib/nwn/twoda.rb

Overview

A Row is simply an Array with some helpers. It wraps a data row in a TwoDA::Table.

You can access Table columns in a row by simply calling a method with the same name.

For example (spells.2da):

table.rows.select {|x| x.Wiz_Sorc == "9" }

selects all level 9 arcane spells.

Instance Attribute Summary collapse

Attributes inherited from Array

#to_yaml_style

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/nwn/twoda.rb', line 41

def method_missing meth, *args
  col = meth.to_s
  assignment = if col =~ /(.+?)=$/
    col = $1
    true
  else
    false
  end

  if idx = @table.columns.index(col.downcase) ||
      idx = @table.columns.index(col)
    if assignment
      self[idx] = args.shift or raise ArgumentError,
        "Need a paramenter for assignments .."
    else
      self[idx]
    end
  else
    super
  end
end

Instance Attribute Details

#tableObject

Returns the value of attribute table.



34
35
36
# File 'lib/nwn/twoda.rb', line 34

def table
  @table
end

Instance Method Details

#IDObject

Returns the id of this row.



37
38
39
# File 'lib/nwn/twoda.rb', line 37

def ID
  @table.rows.index(self)
end