class TableData::Row

Attributes

data[R]
index[R]
table[R]
to_a[R]

Public Class Methods

new(table, index, data) click to toggle source
# File lib/tabledata/row.rb, line 13
def initialize(table, index, data)
  @table  = table
  @index  = index
  @data   = data
end

Public Instance Methods

[](a,b=nil) click to toggle source

@see slice for a faster way to use ranges or offset+length @see at_accessor for a faster way to access by name @see at_index for a faster way to access by index @see at_header for a faster way to access by header value

# File lib/tabledata/row.rb, line 28
def [](a,b=nil)
  if b || a.is_a?(Range) then
    slice(a,b)
  else
    at(a)
  end
end
at(column) click to toggle source
# File lib/tabledata/row.rb, line 40
def at(column)
  case column
    when Symbol then at_accessor(column)
    when String then at_header(column)
    when Integer then at_index(column)
    else raise InvalidColumnSpecifier, "Invalid index type, expected Symbol, String or Integer, but got #{column.class}"
  end
end
at_accessor(name) click to toggle source
# File lib/tabledata/row.rb, line 56
def at_accessor(name)
  index = @table.index_for_accessor(name)
  raise InvalidColumnAccessor, "No column named #{name}" unless index

  @data[index]
end
at_header(name) click to toggle source
# File lib/tabledata/row.rb, line 49
def at_header(name)
  index = @table.index_for_header(name)
  raise InvalidColumnName, "No column named #{name}" unless index

  @data[index]
end
at_index(index) click to toggle source
# File lib/tabledata/row.rb, line 63
def at_index(index)
  @data.at(index)
end
each(&block) click to toggle source

Iterate over each cell in this row

# File lib/tabledata/row.rb, line 20
def each(&block)
  @data.each(&block)
end
inspect() click to toggle source
# File lib/tabledata/row.rb, line 105
def inspect
  sprintf "%s%p", self.class, to_a
end
method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/tabledata/row.rb, line 86
def method_missing(name, *args, &block)
  return super unless @table.accessors?

  name              =~ /^(\w+)(=)?$/
  name_mod, assign  = $1, $2
  index             = @table.index_for_accessor(name_mod)
  arg_count         = assign ? 1 : 0

  return super unless index

  raise ArgumentError, "Wrong number of arguments (#{args.size} for #{arg_count})" if args.size > arg_count

  if assign then
    @data[index] = args.first
  else
    @data[index]
  end
end
respond_to_missing?(name, include_private) click to toggle source
# File lib/tabledata/row.rb, line 82
def respond_to_missing?(name, include_private)
  @table.index_for_accessor(name) ? true : false
end
size() click to toggle source

@return [Integer] The number of cells in this row

# File lib/tabledata/row.rb, line 72
def size
  @data.size
end
slice(*args) click to toggle source
# File lib/tabledata/row.rb, line 36
def slice(*args)
  @data[*args]
end
to_hash() click to toggle source
# File lib/tabledata/row.rb, line 76
def to_hash
  Hash[@table.accessor_columns.map { |accessor, index| [accessor, @data[index]] }]
end
values_at(*columns) click to toggle source
# File lib/tabledata/row.rb, line 67
def values_at(*columns)
  columns.map { |column| at(column) }
end