Module: Workbook::Modules::Cell

Includes:
TypeParser
Included in:
Cell, Types::Date, Types::FalseClass
Defined in:
lib/workbook/modules/cell.rb

Constant Summary collapse

CHARACTER_REPACEMENTS =
{
  [/[\(\)\.\?\,\!\=\$\:]/,] => '',
  [/\&/] => 'amp',
  [/\+/] => '_plus_',
  [/\s/,'/_','/',"\\"] => '_',
  ['–_','-_','+_','-'] => '',
  ['__']=>'_',
  ['>']=>'gt',
  ['<']=>'lt',
  ['á','à','â','ä','ã','å'] => 'a',
  ['Ã','Ä','Â','À','�?','Å'] => 'A',
  ['é','è','ê','ë'] => 'e',
  ['Ë','É','È','Ê'] => 'E',
  ['í','ì','î','ï'] => 'i',
  ['�?','Î','Ì','�?'] => 'I',
  ['ó','ò','ô','ö','õ'] => 'o',
  ['Õ','Ö','Ô','Ò','Ó'] => 'O',
  ['ú','ù','û','ü'] => 'u',
  ['Ú','Û','Ù','Ü'] => 'U',
  ['ç'] => 'c',
  ['Ç'] => 'C',
  ['š', 'ś'] => 's',
  ['Š', 'Ś'] => 'S',
  ['ž','ź'] => 'z',
  ['Ž','Ź'] => 'Z',
  ['ñ'] => 'n',
  ['Ñ'] => 'N',
  ['#'] => 'hash',
  ['*'] => 'asterisk'
}
CLASS_CELLTYPE_MAPPING =
{
 'Numeric' => :integer,
 'Integer' => :integer,
 'Fixnum' => :integer,
 'Float' => :float,
 'String' => :string,
 'Symbol' => :string,
 'Time' => :time,
 'Date' => :date,
 'DateTime' => :datetime,
 'ActiveSupport::TimeWithZone' => :datetime,
 'TrueClass' => :boolean,
 'FalseClass' => :boolean,
 'NilClass' => :nil,
 'Workbook::NilValue' => :nil
}

Instance Method Summary collapse

Methods included from TypeParser

#clean!, #parse, #parse!, #string_american_date_converter, #string_boolean_converter, #string_cleaner, #string_integer_converter, #string_nil_converter, #string_non_american_date_converter, #string_optimistic_date_converter, #string_parsers, #string_parsers=, #string_parsers_as_procs, #strip_win_chars

Instance Method Details

#<=>(other) ⇒ Fixnum

Compare

Parameters:

  • other (Workbook::Cell)

    cell to compare against (based on value), can compare different value-types using #compare_on_class

Returns:

  • (Fixnum)

    -1, 0, 1



213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/workbook/modules/cell.rb', line 213

def <=> other
  rv = nil
  begin
    rv = self.value <=> other.value
  rescue NoMethodError
    rv = compare_on_class other
  end
  if rv == nil
    rv = compare_on_class other
  end
  return rv
end

#==(other) ⇒ Boolean

Tests for equality based on its value (formatting is irrelevant)

Parameters:

Returns:

  • (Boolean)


156
157
158
159
160
161
162
# File 'lib/workbook/modules/cell.rb', line 156

def ==(other)
  if other.is_a? Cell
    other.value == self.value
  else
    other == self.value
  end
end

#cell_typeSymbol

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

Returns:

  • (Symbol)

    the type of cell, compatible with Workbook::Column’types



101
102
103
# File 'lib/workbook/modules/cell.rb', line 101

def cell_type
  CLASS_CELLTYPE_MAPPING[value.class.to_s]
end

#colspanObject



291
292
293
# File 'lib/workbook/modules/cell.rb', line 291

def colspan
  @colspan.to_i if defined?(@colspan) and @colspan.to_i > 1
end

#colspan=(c) ⇒ Object



284
285
286
# File 'lib/workbook/modules/cell.rb', line 284

def colspan= c
  @colspan = c
end

#compare_on_class(other) ⇒ Object

Compare on class level

Parameters:



229
230
231
232
233
234
235
# File 'lib/workbook/modules/cell.rb', line 229

def compare_on_class other
  other_value = nil
  other_value = other.value if other
  self_value = importance_of_class self.value
  other_value = importance_of_class other_value
  self_value <=> other_value
end

#formatWorkbook::Format

Returns current format

Returns:



142
143
144
145
146
147
148
149
150
# File 'lib/workbook/modules/cell.rb', line 142

def format
  # return @workbook_format if @workbook_format
  if row and template and row.header? and !defined?(@workbook_format)
    @workbook_format = template.create_or_find_format_by(:header)
  else
    @workbook_format ||= Workbook::Format.new
  end
  @workbook_format
end

#format=(f) ⇒ Object

Change the current format

Parameters:

  • f (Workbook::Format, Hash)

    set the formatting properties of this Cell, see Workbook::Format#initialize



129
130
131
132
133
134
135
136
137
# File 'lib/workbook/modules/cell.rb', line 129

def format= f
  if f.is_a? Workbook::Format
    @workbook_format = f
  elsif f.is_a? Hash
    @workbook_format = Workbook::Format.new(f)
  elsif f.class == NilClass
    @workbook_format = Workbook::Format.new
  end
end

#format?Boolean

Returns whether special formatting is present on this cell

Returns:

  • (Boolean)

    index of the cell



247
248
249
# File 'lib/workbook/modules/cell.rb', line 247

def format?
  format and format.keys.count > 0
end

#formulaObject



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

def formula
  @formula
end

#formula=(f) ⇒ Object



74
75
76
# File 'lib/workbook/modules/cell.rb', line 74

def formula= f
  @formula = f
end

#importance_of_class(value) ⇒ Object

Returns the importance of a value’s class

Parameters:

  • value

    a potential value for a cell



240
241
242
# File 'lib/workbook/modules/cell.rb', line 240

def importance_of_class value
  CLASS_CELLTYPE_MAPPING.keys.index value.class.to_s
end

#indexInteger, NilClass

Returns the index of the cell within the row, returns nil if no row is present

Returns:

  • (Integer, NilClass)

    index of the cell



254
255
256
# File 'lib/workbook/modules/cell.rb', line 254

def index
  row.index self if row
end

#inspectObject



265
266
267
268
269
270
# File 'lib/workbook/modules/cell.rb', line 265

def inspect
  txt = "<Workbook::Cell @value=#{value}"
  txt += " @format=#{format}" if format?
  txt += ">"
  txt
end

#keySymbol, NilClass

Returns the key (a Symbol) of the cell, based on its table’s header

Returns:

  • (Symbol, NilClass)

    key of the cell, returns nil if the cell doesn’t belong to a table



261
262
263
# File 'lib/workbook/modules/cell.rb', line 261

def key
  table.header[index].to_sym if table
end

#nil?Boolean

returns true when the value of the cell is nil.

Returns:

  • (Boolean)


166
167
168
# File 'lib/workbook/modules/cell.rb', line 166

def nil?
  value.nil?
end

#nil_or_empty?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/workbook/modules/cell.rb', line 170

def nil_or_empty?
  value.nil? || value.to_s == ""
end

#rowObject



78
79
80
# File 'lib/workbook/modules/cell.rb', line 78

def row
  @row
end

#row=(r) ⇒ Object



82
83
84
# File 'lib/workbook/modules/cell.rb', line 82

def row= r
  @row= r
end

#rowspanObject



294
295
296
# File 'lib/workbook/modules/cell.rb', line 294

def rowspan
  @rowspan.to_i if defined?(@rowspan) and @rowspan.to_i > 1
end

#rowspan=(r) ⇒ Object



287
288
289
# File 'lib/workbook/modules/cell.rb', line 287

def rowspan= r
  @rowspan = r
end

#tableWorkbook::Table

Returns the sheet its at.

Returns:



115
116
117
# File 'lib/workbook/modules/cell.rb', line 115

def table
  row.table if row
end

#templateWorkbook::Template

Quick assessor to the book’s template, if it exists

Returns:



122
123
124
# File 'lib/workbook/modules/cell.rb', line 122

def template
  row.template if row
end

#to_sString

convert value to string, and in case of a Date or Time value, apply formatting

Returns:

  • (String)


274
275
276
277
278
279
280
281
282
# File 'lib/workbook/modules/cell.rb', line 274

def to_s
  if (self.is_a? Date or self.is_a? Time) and format[:number_format]
    value.strftime(format[:number_format])
  elsif (self.class == Workbook::Cell)
    value.to_s
  else
    super
  end
end

#to_symSymbol

returns a symbol representation of the cell’s value

Examples:


<Workbook::Cell value="yet another value">.to_sym # returns :yet_another_value

Returns:

  • (Symbol)

    a symbol representation



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/workbook/modules/cell.rb', line 183

def to_sym
  return @to_sym if @to_sym
  v = nil
  unless nil_or_empty?
    if cell_type == :integer
      v = "num#{value}".to_sym
    elsif cell_type == :float
      v = "num#{value}".sub(".","_").to_sym
    else
      v = value_to_s.strip
      ends_with_exclamationmark = (v[-1] == '!')
      ends_with_questionmark = (v[-1] == '?')

      v = _replace_possibly_problematic_characters_from_string(v)

      v = v.encode(Encoding.find('ASCII'), {:invalid => :replace, :undef => :replace, :replace => ''})

      v = "#{v}!" if ends_with_exclamationmark
      v = "#{v}?" if ends_with_questionmark
      v = v.downcase.to_sym
    end
  end
  @to_sym = v
  return @to_sym
end

#valid_value?(value) ⇒ Boolean

Evaluates a value for class-validity

Parameters:

  • value (Numeric, String, Time, Date, TrueClass, FalseClass, NilClass, Object)

    the value to evaluate

Returns:

  • (Boolean)

    returns true when the value is a valid cell value



66
67
68
# File 'lib/workbook/modules/cell.rb', line 66

def valid_value? value
  !CLASS_CELLTYPE_MAPPING[value.class.to_s].nil?
end

#valueNumeric, ...

Returns the current value

Returns:

  • (Numeric, String, Time, Date, TrueClass, FalseClass, NilClass)

    a valid value



108
109
110
# File 'lib/workbook/modules/cell.rb', line 108

def value
  @value
end

#value=(value) ⇒ Object

Change the current value

Parameters:

  • value (Numeric, String, Time, Date, TrueClass, FalseClass, NilClass, Symbol)

    a valid value



89
90
91
92
93
94
95
96
# File 'lib/workbook/modules/cell.rb', line 89

def value= value
  if valid_value? value
    @value = value
    @to_sym = nil
  else
    raise ArgumentError, "value should be of a primitive type, e.g. a string, or an integer, not a #{value.class} (is_a? [TrueClass,FalseClass,Date,Time,Numeric,String, NilClass, Symbol])"
  end
end

#value_to_sObject



174
175
176
# File 'lib/workbook/modules/cell.rb', line 174

def value_to_s
  value.to_s.downcase
end