From 3810e583746152e84acb2c9d5477a9c3e501c7e3 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Thu, 20 Dec 2012 23:57:35 +0900 Subject: Simplified to_xml_string serialization Spit out cell type based serializers and got rid of the case statement. --- lib/axlsx/workbook/worksheet/cell.rb | 74 +++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 30 deletions(-) diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index 67b11633..c68f7e47 100644 --- a/lib/axlsx/workbook/worksheet/cell.rb +++ b/lib/axlsx/workbook/worksheet/cell.rb @@ -87,7 +87,7 @@ module Axlsx # The value of this cell. - # @return [String, Integer, Float, Time] casted value based on cell's type attribute. + # @return [String, Integer, Float, Time, Boolean] casted value based on cell's type attribute. attr_reader :value # @see value def value=(v) @@ -219,7 +219,7 @@ module Axlsx end # The inline sz property for the cell - # @return [Boolean] + # @return [Inteter] attr_reader :sz # @see sz def sz=(v) set_run_style :validate_unsigned_int, :sz, v; end @@ -326,35 +326,10 @@ module Axlsx def to_xml_string(r_index, c_index, str = '') str << '' if @value.nil? - - case @type - - when :string - #parse formula - if is_formula? - str << 't="str">' << @value.to_s.sub('=', '') << '' - str << '' << @formula_value.to_s << '' if @formula_value - else - #parse shared - if @ssti - str << 't="s">' << @ssti.to_s << '' - else - str << 't="inlineStr">' << run_xml_string << '' - end - end - when :date - # TODO: See if this is subject to the same restriction as Time below - str << '>' << DateTimeConverter::date_to_serial(@value).to_s << '' - when :time - str << '>' << DateTimeConverter::time_to_serial(@value).to_s << '' - when :boolean - str << 't="b">' << @value.to_s << '' - else - str << '>' << @value.to_s << '' - end + self.send (@type.to_s << '_type_serialization').to_sym, str str << '' end - + def is_formula? @type == :string && @value.to_s.start_with?('=') end @@ -450,11 +425,50 @@ module Axlsx v ? 1 : 0 else @type = :string - v.to_s # TODO find a better way to do this as it accounts for 30% of # processing time in benchmarking... ::CGI.escapeHTML(v.to_s) end end + + def date_type_serialization(str='') + value_serialization 'd', DateTimeConverter::date_to_serial(@value).to_s, str + end + + def time_type_serialization(str='') + value_serialization 'd', DateTimeConverter::time_to_serial(@value).to_s, str + end + + def boolean_type_serialization(str='') + value_serialization 'b', @value.to_s, str + end + + def float_type_serialization(str='') + numeric_type_serialization str + end + + def integer_type_serialization(str = '') + numeric_type_serialization str + end + + def numeric_type_serialization(str = '') + value_serialization('n', @value.to_s, str) + end + + def value_serialization(serialization_type, serialization_value, str = '') + str << 't="' << serialization_type << '">' << serialization_value << '' + end + + def string_type_serialization(str='') + if is_formula? + str << 't="str">' << '' << value.to_s.sub('=', '') << '' + str << '' << formula_value.to_s << '' unless formula_value.nil? + elsif !@ssti.nil? + value_serialization 's', @ssti.to_s, str + else + str << 't="inlineStr">' << '' << run_xml_string << '' + end + end + end end -- cgit v1.2.3