Class: Ditch::IndexedRow

Inherits:
Object
  • Object
show all
Defined in:
lib/ditch/indexed_row.rb

Overview

Class wrapping the data-structure used or Row data in Creek

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers, index, data) ⇒ IndexedRow

Create instance of IndexedRow

  • headers Array holding column-headers

  • index Row Index in the sheet

  • data The 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_rowObject (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

#indexObject (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

  • cell column-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

  • title String containing header-title of a column

Returns:

  • (Boolean)


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’

  • title Sting describing column by name

Raises:



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_sObject

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