Class: Workbook::Column
- Inherits:
-
Object
- Object
- Workbook::Column
- 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
-
#limit ⇒ Object
character limit.
-
#width ⇒ Object
character limit.
Class Method Summary collapse
-
.alpha_index_to_number_index(string) ⇒ Integer
Helps to convert from e.g.
Instance Method Summary collapse
-
#column_type ⇒ Object
Returns column type, either :primary_key, :string, :text, :integer, :float, :decimal, :datetime, :date, :binary, :boolean.
- #column_type=(column_type) ⇒ Object
-
#default ⇒ Object
default cell.
- #default=(value) ⇒ Object
- #head_value ⇒ Object
-
#index ⇒ Integer, NilClass
Returns index of the column within the table’s columns-set.
-
#initialize(table = nil, options = {}) ⇒ Column
constructor
A new instance of Column.
- #inspect ⇒ Object
- #table ⇒ Workbook::Table
-
#table=(table) ⇒ Object
Set the table this column belongs to.
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, ={}) self.table = table .each{ |k,v| self.public_send("#{k}=",v) } end |
Instance Attribute Details
#limit ⇒ Object
character limit
9 10 11 |
# File 'lib/workbook/column.rb', line 9 def limit @limit end |
#width ⇒ Object
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
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_type ⇒ Object
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 |
#default ⇒ Object
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_value ⇒ Object
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 |
#index ⇒ Integer, NilClass
Returns index of the column within the table’s columns-set
37 38 39 |
# File 'lib/workbook/column.rb', line 37 def index table.columns.index self end |
#inspect ⇒ Object
70 71 72 |
# File 'lib/workbook/column.rb', line 70 def inspect "<Workbook::Column index=#{index}, header=#{head_value}>" end |
#table=(table) ⇒ Object
Set the table this column belongs to
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 |