Class: Workbook::Column

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

Overview

Column helps us to store general properties of a column, and lets us easily perform operations on values within a column

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table = nil, options = {}) ⇒ Column

Returns a new instance of Column.



11
12
13
14
# File 'lib/workbook/column.rb', line 11

def initialize(table=nil, options={})
  self.table = table
  options.each{ |k,v| self.public_send("#{k}=",v) }
end

Instance Attribute Details

#limitObject

character limit



9
10
11
# File 'lib/workbook/column.rb', line 9

def limit
  @limit
end

#widthObject

character limit



9
10
11
# File 'lib/workbook/column.rb', line 9

def width
  @width
end

Class Method Details

.alpha_index_to_number_index(string) ⇒ Integer

Helps to convert from e.g. “AA” to 26

Parameters:

  • string (String)

    that typically identifies a column

Returns:

  • (Integer)


88
89
90
91
92
93
94
# File 'lib/workbook/column.rb', line 88

def alpha_index_to_number_index string
  sum = 0
  string.upcase.chars.each_with_index do | char, char_index|
    sum = sum * 26 + char.unpack('U')[0]-64
  end
  return sum-1
end

Instance Method Details

#column_typeObject

Returns column type, either :primary_key, :string, :text, :integer, :float, :decimal, :datetime, :date, :binary, :boolean



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/workbook/column.rb', line 17

def column_type
  return @column_type if defined?(@column_type)
  ind = self.index
  table[1..500].each do |row|
    if row[ind] and row[ind].cell_type
      cel_column_type = row[ind].cell_type
      if !defined?(@column_type) or @column_type.nil?
        @column_type = cel_column_type
      elsif cel_column_type == @column_type or cel_column_type == :nil
      else
        @column_type = :string
        break
      end
    end
  end
  return @column_type
end

#column_type=(column_type) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/workbook/column.rb', line 53

def column_type= column_type
  if [:primary_key, :string, :text, :integer, :float, :decimal, :datetime, :date, :binary, :boolean].include? column_type
    @column_type = column_type
  else
    raise ArgumentError, "value should be a symbol indicating a primitive type, e.g. a string, or an integer (valid values are: :primary_key, :string, :text, :integer, :float, :decimal, :datetime, :date, :binary, :boolean)"

  end
end

#defaultObject

default cell



75
76
77
# File 'lib/workbook/column.rb', line 75

def default
  return @default
end

#default=(value) ⇒ Object



79
80
81
82
# File 'lib/workbook/column.rb', line 79

def default= value
  @default = value if value.class == Cell
  @default = Cell.new(value)
end

#head_valueObject



62
63
64
65
66
67
68
# File 'lib/workbook/column.rb', line 62

def head_value
  begin
    table.header[index].value
  rescue
    return "!noheader!"
  end
end

#indexInteger, NilClass

Returns index of the column within the table’s columns-set

Returns:

  • (Integer, NilClass)


37
38
39
# File 'lib/workbook/column.rb', line 37

def index
  table.columns.index self
end

#inspectObject



70
71
72
# File 'lib/workbook/column.rb', line 70

def inspect
  "<Workbook::Column index=#{index}, header=#{head_value}>"
end

#tableWorkbook::Table

Returns:



49
50
51
# File 'lib/workbook/column.rb', line 49

def table
  @table
end

#table=(table) ⇒ Object

Set the table this column belongs to

Parameters:

Raises:

  • (ArgumentError)


43
44
45
46
# File 'lib/workbook/column.rb', line 43

def table= table
  raise(ArgumentError, "value should be nil or Workbook::Table") unless [NilClass,Workbook::Table].include? table.class
  @table = table
end