Class: TableData::Row
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- Defined in:
- lib/tabledata/row.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(table, index, data) ⇒ Row
Returns a new instance of Row.
13
14
15
16
17
|
# File 'lib/tabledata/row.rb', line 13
def initialize(table, index, data)
@table = table
@index = index
@data = data
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# 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
|
Instance Attribute Details
#data ⇒ Object
Also known as:
to_a
Returns the value of attribute data.
9
10
11
|
# File 'lib/tabledata/row.rb', line 9
def data
@data
end
|
#index ⇒ Object
Returns the value of attribute index.
8
9
10
|
# File 'lib/tabledata/row.rb', line 8
def index
@index
end
|
#table ⇒ Object
Returns the value of attribute table.
7
8
9
|
# File 'lib/tabledata/row.rb', line 7
def table
@table
end
|
Instance Method Details
#[](a, b = nil) ⇒ Object
28
29
30
31
32
33
34
|
# 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) ⇒ Object
40
41
42
43
44
45
46
47
|
# File 'lib/tabledata/row.rb', line 40
def at(column)
case column
when Symbol then at_accessor(column)
when String then (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) ⇒ Object
56
57
58
59
60
61
|
# 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
|
49
50
51
52
53
54
|
# File 'lib/tabledata/row.rb', line 49
def (name)
index = @table.(name)
raise InvalidColumnName, "No column named #{name}" unless index
@data[index]
end
|
#at_index(index) ⇒ Object
63
64
65
|
# File 'lib/tabledata/row.rb', line 63
def at_index(index)
@data.at(index)
end
|
#each(&block) ⇒ Object
Iterate over each cell in this row
20
21
22
|
# File 'lib/tabledata/row.rb', line 20
def each(&block)
@data.each(&block)
end
|
#inspect ⇒ Object
105
106
107
|
# File 'lib/tabledata/row.rb', line 105
def inspect
sprintf "%s%p", self.class, to_a
end
|
#respond_to_missing?(name, include_private) ⇒ Boolean
82
83
84
|
# File 'lib/tabledata/row.rb', line 82
def respond_to_missing?(name, include_private)
@table.index_for_accessor(name) ? true : false
end
|
#size ⇒ Integer
Returns The number of cells in this row.
72
73
74
|
# File 'lib/tabledata/row.rb', line 72
def size
@data.size
end
|
#slice(*args) ⇒ Object
36
37
38
|
# File 'lib/tabledata/row.rb', line 36
def slice(*args)
@data[*args]
end
|
#to_hash ⇒ Object
76
77
78
|
# File 'lib/tabledata/row.rb', line 76
def to_hash
Hash[@table.accessor_columns.map { |accessor, index| [accessor, @data[index]] }]
end
|
#values_at(*columns) ⇒ Object
67
68
69
|
# File 'lib/tabledata/row.rb', line 67
def values_at(*columns)
columns.map { |column| at(column) }
end
|