Class: Workbook::Row

Inherits:
Array
  • Object
show all
Defined in:
lib/workbook/row.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cells = [], table = nil, options = {}) ⇒ Row

Returns a new instance of Row



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/workbook/row.rb', line 10

def initialize cells=[], table=nil, options={}
  options=options ? {:parse_cells_on_batch_creation=>false,:cell_parse_options=>{}}.merge(options) : {} 
  cells = [] if cells==nil
  self.table= table
  cells.each do |c| 
    if c.is_a? Workbook::Cell
      c = c
    else
      c = Workbook::Cell.new(c)
      c.parse!(options[:cell_parse_options]) if options[:parse_cells_on_batch_creation]
    end
    push c
  end
end

Instance Attribute Details

#formatObject

Returns the value of attribute format



7
8
9
# File 'lib/workbook/row.rb', line 7

def format
  @format
end

#placeholderObject

used in compares



6
7
8
# File 'lib/workbook/row.rb', line 6

def placeholder
  @placeholder
end

Instance Method Details

#<=>(other) ⇒ Object



84
85
86
87
88
89
# File 'lib/workbook/row.rb', line 84

def <=> other
  a = self.header? ? 0 : 1
  b = other.header? ? 0 : 1
  return (a <=> b) if (a==0 or b==0)
  compare_without_header other
end

#[](index_or_hash) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/workbook/row.rb', line 42

def [](index_or_hash)
  if index_or_hash.is_a? Fixnum
    return to_a[index_or_hash]
  elsif index_or_hash.is_a? Symbol
    rv = nil
    begin
      rv = to_hash[index_or_hash]
    rescue NoMethodError
    end
    return rv
  end
end

#compactObject

Compact detaches the row from the table



96
97
98
99
# File 'lib/workbook/row.rb', line 96

def compact
  r = self.clone
  r = r.collect{|c| c unless c.nil?}.compact
end

#compare_without_headerObject



3
# File 'lib/workbook/row.rb', line 3

alias_method :compare_without_header, :<=>

#find_cells_by_background_color(color = :any, options = {}) ⇒ Object

find_cells_by_color returns an array of cells allows you to find cells by a given color, normally a string containing a hex



56
57
58
59
60
61
# File 'lib/workbook/row.rb', line 56

def find_cells_by_background_color color=:any, options={}
  options = {:hash_keys=>true}.merge(options)
  cells = self.collect {|c| c if c.format.has_background_color?(color) }.compact
  r = Row.new cells
  options[:hash_keys] ? r.to_symbols : r
end

#header?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/workbook/row.rb', line 63

def header?
  table != nil and self.object_id == table.header.object_id
end

#keyObject



91
92
93
# File 'lib/workbook/row.rb', line 91

def key
  first
end

#placeholder?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/workbook/row.rb', line 25

def placeholder?
  placeholder ? true : false
end

#tableObject



29
30
31
# File 'lib/workbook/row.rb', line 29

def table
  @table
end

#table=(t) ⇒ Object

Parameters:

  • Reference (Workbook::Table)

    to the table this row belongs to (and adds the row to this table)

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
# File 'lib/workbook/row.rb', line 34

def table= t
  raise ArgumentError, "table should be a Workbook::Table (you passed a #{t.class})" unless t.is_a?(Workbook::Table) or t == nil
  if t
    @table = t
    table << self
  end
end

#to_aObject



71
72
73
# File 'lib/workbook/row.rb', line 71

def to_a
  self.collect{|c| c}
end

#to_hashObject



75
76
77
78
79
80
81
82
# File 'lib/workbook/row.rb', line 75

def to_hash
  return @hash if @hash
  keys = table.header.to_symbols
  values = self
  @hash = {}
  keys.each_with_index {|k,i| @hash[k]=values[i]}
  return @hash
end

#to_symbolsObject



67
68
69
# File 'lib/workbook/row.rb', line 67

def to_symbols
  collect{|c| c.to_sym}
end