Class: Tablespoon::Record
- Inherits:
-
Object
- Object
- Tablespoon::Record
- Defined in:
- lib/tablespoon.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#id ⇒ Object
Returns the value of attribute id.
-
#row_num ⇒ Object
Returns the value of attribute row_num.
Instance Method Summary collapse
- #[](field) ⇒ Object
- #[]=(field, value) ⇒ Object
- #get_col_num(field) ⇒ Object
-
#initialize(table) ⇒ Record
constructor
A new instance of Record.
- #to_s ⇒ Object
Constructor Details
#initialize(table) ⇒ Record
Returns a new instance of Record.
182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/tablespoon.rb', line 182 def initialize( table ) @table = table @field_map = table.field_map @ws = table.ws @id_field = table.id_field @data = {} end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
140 141 142 |
# File 'lib/tablespoon.rb', line 140 def data @data end |
#id ⇒ Object
Returns the value of attribute id.
140 141 142 |
# File 'lib/tablespoon.rb', line 140 def id @id end |
#row_num ⇒ Object
Returns the value of attribute row_num.
140 141 142 |
# File 'lib/tablespoon.rb', line 140 def row_num @row_num end |
Instance Method Details
#[](field) ⇒ Object
142 143 144 |
# File 'lib/tablespoon.rb', line 142 def [] (field) return @data[field] end |
#[]=(field, value) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/tablespoon.rb', line 146 def []= (field,value) ## get the column number where we think it is ## if it's not there, rebuild the field map ## and try again col_num = get_col_num( field ) row_num = @row_num @ws[row_num, col_num] = value @ws.save if field == @id_field @id = value end end |
#get_col_num(field) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/tablespoon.rb', line 165 def get_col_num( field ) col_num = @field_map[field] if ! @ws[1,col_num] == field @table.build_column_map col_num = @field_map[field] if ! @ws[1,col_num] == field raise "Unable to find field #{field}" end end return col_num end |
#to_s ⇒ Object
194 195 196 |
# File 'lib/tablespoon.rb', line 194 def to_s return "Row #{@row_num} Id #{@id} " + @data.keys.collect { |a| "#{a}: #{@data[a]}" }.join(' - ') end |