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
-
#<=>(other) ⇒ Fixnum
Compare.
-
#==(other) ⇒ Boolean
Tests for equality based on its value (formatting is irrelevant).
-
#cell_type ⇒ Symbol
Returns column type, either :primary_key, :string, :text, :integer, :float, :decimal, :datetime, :date, :binary, :boolean.
- #colspan ⇒ Object
- #colspan=(c) ⇒ Object
-
#compare_on_class(other) ⇒ Object
Compare on class level.
-
#format ⇒ Workbook::Format
Returns current format.
-
#format=(f) ⇒ Object
Change the current format.
-
#format? ⇒ Boolean
Returns whether special formatting is present on this cell.
- #formula ⇒ Object
- #formula=(f) ⇒ Object
-
#importance_of_class(value) ⇒ Object
Returns the importance of a value’s class.
-
#index ⇒ Integer, NilClass
Returns the index of the cell within the row, returns nil if no row is present.
- #inspect ⇒ Object
-
#key ⇒ Symbol, NilClass
Returns the key (a Symbol) of the cell, based on its table’s header.
-
#nil? ⇒ Boolean
returns true when the value of the cell is nil.
- #nil_or_empty? ⇒ Boolean
- #row ⇒ Object
- #row=(r) ⇒ Object
- #rowspan ⇒ Object
- #rowspan=(r) ⇒ Object
-
#table ⇒ Workbook::Table
Returns the sheet its at.
-
#template ⇒ Workbook::Template
Quick assessor to the book’s template, if it exists.
-
#to_s ⇒ String
convert value to string, and in case of a Date or Time value, apply formatting.
-
#to_sym ⇒ Symbol
returns a symbol representation of the cell’s value.
-
#valid_value?(value) ⇒ Boolean
Evaluates a value for class-validity.
-
#value ⇒ Numeric, ...
Returns the current value.
-
#value=(value) ⇒ Object
Change the current value.
- #value_to_s ⇒ Object
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
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)
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_type ⇒ Symbol
Returns column type, either :primary_key, :string, :text, :integer, :float, :decimal, :datetime, :date, :binary, :boolean
101 102 103 |
# File 'lib/workbook/modules/cell.rb', line 101 def cell_type CLASS_CELLTYPE_MAPPING[value.class.to_s] end |
#colspan ⇒ Object
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
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 |
#format ⇒ Workbook::Format
Returns current format
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
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
247 248 249 |
# File 'lib/workbook/modules/cell.rb', line 247 def format? format and format.keys.count > 0 end |
#formula ⇒ Object
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
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 |
#index ⇒ Integer, NilClass
Returns the index of the cell within the row, returns nil if no row is present
254 255 256 |
# File 'lib/workbook/modules/cell.rb', line 254 def index row.index self if row end |
#inspect ⇒ Object
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 |
#key ⇒ Symbol, NilClass
Returns the key (a Symbol) of the cell, based on its table’s header
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.
166 167 168 |
# File 'lib/workbook/modules/cell.rb', line 166 def nil? value.nil? end |
#nil_or_empty? ⇒ Boolean
170 171 172 |
# File 'lib/workbook/modules/cell.rb', line 170 def nil_or_empty? value.nil? || value.to_s == "" end |
#row ⇒ Object
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 |
#rowspan ⇒ Object
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 |
#table ⇒ Workbook::Table
Returns the sheet its at.
115 116 117 |
# File 'lib/workbook/modules/cell.rb', line 115 def table row.table if row end |
#template ⇒ Workbook::Template
Quick assessor to the book’s template, if it exists
122 123 124 |
# File 'lib/workbook/modules/cell.rb', line 122 def template row.template if row end |
#to_s ⇒ String
convert value to string, and in case of a Date or Time value, apply formatting
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_sym ⇒ Symbol
returns a symbol representation of the cell’s value
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
66 67 68 |
# File 'lib/workbook/modules/cell.rb', line 66 def valid_value? value !CLASS_CELLTYPE_MAPPING[value.class.to_s].nil? end |
#value ⇒ Numeric, ...
Returns the current value
108 109 110 |
# File 'lib/workbook/modules/cell.rb', line 108 def value @value end |
#value=(value) ⇒ Object
Change the current 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_s ⇒ Object
174 175 176 |
# File 'lib/workbook/modules/cell.rb', line 174 def value_to_s value.to_s.downcase end |