From c99b7b2a138c9c7dd0b42662662f278b01ec7e75 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Mon, 4 Feb 2013 15:08:46 +0900 Subject: Extracted type based cell serializers --- lib/axlsx/workbook/worksheet/cell_serializer.rb | 68 +++++++++++++++++++++---- 1 file changed, 59 insertions(+), 9 deletions(-) diff --git a/lib/axlsx/workbook/worksheet/cell_serializer.rb b/lib/axlsx/workbook/worksheet/cell_serializer.rb index c7f43f45..e4c35c3a 100644 --- a/lib/axlsx/workbook/worksheet/cell_serializer.rb +++ b/lib/axlsx/workbook/worksheet/cell_serializer.rb @@ -1,6 +1,15 @@ module Axlsx + + # The Cell Serializer class contains the logic for serializing cells based on their type. class CellSerializer class << self + + + # Calls the proper serialization method based on type. + # @param [Integer] row_index The index of the cell's row + # @param [Integer] column_index The index of the cell's column + # @param [String] str The string to apend serialization to. + # @return [String] def to_xml_string(row_index, column_index, cell, str='') str << '' if cell.value.nil? @@ -35,51 +44,79 @@ module Axlsx str end - + # serializes cells that are type iso_8601 + # @param [Cell] cell The cell that is being serialized + # @param [String] str The string the serialized content will be appended to. + # @return [String] def iso_8601_type_serialization(cell, str='') value_serialization 'd', cell.value, str end + + # serializes cells that are type date + # @param [Cell] cell The cell that is being serialized + # @param [String] str The string the serialized content will be appended to. + # @return [String] def date_type_serialization(cell, str='') value_serialization false, DateTimeConverter::date_to_serial(cell.value).to_s, str end + # Serializes cells that are type time + # @param [Cell] cell The cell that is being serialized + # @param [String] str The string the serialized content will be appended to. + # @return [String] def time_type_serialization(cell, str='') value_serialization false, DateTimeConverter::time_to_serial(cell.value).to_s, str end + # Serializes cells that are type boolean + # @param [Cell] cell The cell that is being serialized + # @param [String] str The string the serialized content will be appended to. + # @return [String] def boolean_type_serialization(cell, str='') value_serialization 'b', cell.value.to_s, str end + # Serializes cells that are type float + # @param [Cell] cell The cell that is being serialized + # @param [String] str The string the serialized content will be appended to. + # @return [String] def float_type_serialization(cell, str='') numeric_type_serialization cell, str end + # Serializes cells that are type integer + # @param [Cell] cell The cell that is being serialized + # @param [String] str The string the serialized content will be appended to. + # @return [String] def integer_type_serialization(cell, str = '') numeric_type_serialization cell, str end - def numeric_type_serialization(cell, str = '') - value_serialization 'n', cell.value.to_s, str - end - - def value_serialization(serialization_type, serialization_value, str = '') - str << 't="' << serialization_type << '"' if serialization_type - str << '>' << serialization_value << '' - end + # Serializes cells that are type formula + # @param [Cell] cell The cell that is being serialized + # @param [String] str The string the serialized content will be appended to. + # @return [String] def formula_serialization(cell, str='') str << 't="str">' << '' << cell.value.to_s.sub('=', '') << '' str << '' << cell.formula_value.to_s << '' unless cell.formula_value.nil? end + # Serializes cells that are type inline_string + # @param [Cell] cell The cell that is being serialized + # @param [String] str The string the serialized content will be appended to. + # @return [String] def inline_string_serialization(cell, str = '') str << 't="inlineStr">' << '' run_xml_string cell, str str << '' end + # Serializes cells that are type string + # @param [Cell] cell The cell that is being serialized + # @param [String] str The string the serialized content will be appended to. + # @return [String] def string_type_serialization(cell, str='') if cell.is_formula? formula_serialization cell, str @@ -89,6 +126,19 @@ module Axlsx inline_string_serialization cell, str end end + + private + + def numeric_type_serialization(cell, str = '') + value_serialization 'n', cell.value.to_s, str + end + + def value_serialization(serialization_type, serialization_value, str = '') + str << 't="' << serialization_type << '"' if serialization_type + str << '>' << serialization_value << '' + end + + end end end -- cgit v1.2.3