Class: Ditch::IndexedRow
- Inherits:
-
Object
- Object
- Ditch::IndexedRow
- Defined in:
- lib/ditch/indexed_row.rb
Overview
Class wrapping the data-structure used or Row data in Creek
Instance Attribute Summary collapse
-
#creek_row ⇒ Object
readonly
Returns the value of attribute creek_row.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
Instance Method Summary collapse
-
#get(cell) ⇒ Object
Get cell value from column-designation *
cellcolumn-designation such as ‘A’. -
#has_value?(title) ⇒ Boolean
Test if cell non-nil value *
titleString containing header-title of a column. -
#initialize(headers, index, data) ⇒ IndexedRow
constructor
Create instance of IndexedRow *
headersArray holding column-headers *indexRow Index in the sheet *dataThe original creek row. -
#lookup(title) ⇒ Object
Get cell value from ‘header-title’ *
titleSting describing column by name. -
#to_s ⇒ Object
Format the row into a human-readable string.
Constructor Details
#initialize(headers, index, data) ⇒ IndexedRow
Create instance of IndexedRow
-
headersArray holding column-headers -
indexRow Index in the sheet -
dataThe original creek row
11 12 13 14 15 |
# File 'lib/ditch/indexed_row.rb', line 11 def initialize (headers,index, data) @headers = headers @index = index @creek_row = data end |
Instance Attribute Details
#creek_row ⇒ Object (readonly)
Returns the value of attribute creek_row.
5 6 7 |
# File 'lib/ditch/indexed_row.rb', line 5 def creek_row @creek_row end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
5 6 7 |
# File 'lib/ditch/indexed_row.rb', line 5 def index @index end |
Instance Method Details
#get(cell) ⇒ Object
Get cell value from column-designation
-
cellcolumn-designation such as ‘A’
19 20 21 22 |
# File 'lib/ditch/indexed_row.rb', line 19 def get( cell ) creek_value = @creek_row["#{cell.upcase}#{@index}"] (creek_value.nil? || creek_value == 'NULL' || creek_value.empty?) ? nil : creek_value end |
#has_value?(title) ⇒ Boolean
Test if cell non-nil value
-
titleString containing header-title of a column
36 37 38 |
# File 'lib/ditch/indexed_row.rb', line 36 def has_value?(title) !lookup(title).nil? end |
#lookup(title) ⇒ Object
Get cell value from ‘header-title’
-
titleSting describing column by name
26 27 28 29 30 31 32 |
# File 'lib/ditch/indexed_row.rb', line 26 def lookup( title ) raise Ditch::DitchError.new('No headers..') if @headers.nil? cell = @headers[title] raise Ditch::DitchError.new("Title '#{title}' not found in headers..") if cell.nil? get cell.strip end |
#to_s ⇒ Object
Format the row into a human-readable string
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/ditch/indexed_row.rb', line 41 def to_s if @headers.nil? "#{@index}: #{@creek_row.to_s}" else parts = Array.new @headers.each_key do |h| parts << "'#{h}'=>'#{lookup(h)}'" end "#{@index}: {#{parts.join(', ')}}" end end |