From bc256511fc600030746206f25b25d18385f5f2c2 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Sun, 25 Nov 2012 13:53:46 +0900 Subject: Refactored header_footer element serialization. This adds a serializable_element_attributes helper that should be integrated as we have time for serializing child elements that are based on object attributes. --- lib/axlsx/util/serialized_attributes.rb | 31 ++++++++++++++++++++++++++- lib/axlsx/workbook/worksheet/header_footer.rb | 15 ++++--------- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/lib/axlsx/util/serialized_attributes.rb b/lib/axlsx/util/serialized_attributes.rb index bffa0a10..3372dd5c 100644 --- a/lib/axlsx/util/serialized_attributes.rb +++ b/lib/axlsx/util/serialized_attributes.rb @@ -21,6 +21,14 @@ module Axlsx def xml_attributes @xml_attributes end + + def serializable_element_attributes(*symbols) + @xml_element_attributes = symbols + end + + def xml_element_attributes + @xml_element_attributes + end end # serializes the instance values of the defining object based on the @@ -36,11 +44,32 @@ module Axlsx key_value_pairs.delete(key) unless self.class.xml_attributes.include?(key.to_sym) end key_value_pairs.merge! additional_attributes - key_value_pairs.each do |key, value| str << "#{Axlsx.camel(key, false)}=\"#{value}\" " end str end + + + # serialized instance values at text nodes on a camelized element of the + # attribute name. You may pass in a block for evaluation against non nil + # values. We use an array for element attributes becuase misordering will + # break the xml and 1.8.7 does not support ordered hashes. + # @param [String] str The string instance to which serialized data is appended + # @param [Array] additional_attributes An array of additional attribute names. + # @param [Proc] block A which will be called with the value for each element. + # @return [String] The serialized output. + def serialized_element_attributes(str='', additional_attributes=[], &block) + attrs = self.class.xml_element_attributes + additional_attributes + values = instance_values + attrs.each do |attribute_name| + value = values[attribute_name.to_s] + next if value.nil? + yield value if block_given? + element_name = Axlsx.camel(attribute_name, false) + str << "<#{element_name}>#{value}" + end + str + end end end diff --git a/lib/axlsx/workbook/worksheet/header_footer.rb b/lib/axlsx/workbook/worksheet/header_footer.rb index 20d7b0eb..a40ad853 100644 --- a/lib/axlsx/workbook/worksheet/header_footer.rb +++ b/lib/axlsx/workbook/worksheet/header_footer.rb @@ -28,7 +28,7 @@ module Axlsx end serializable_attributes :different_odd_even, :different_first - + serializable_element_attributes :odd_header, :odd_footer, :even_header, :even_footer, :first_header, :first_footer string_attr_accessor :odd_header, :odd_footer, :even_header, :even_footer, :first_header, :first_footer boolean_attr_accessor :different_odd_even, :different_first @@ -45,16 +45,9 @@ module Axlsx str << "" - - str << "#{::CGI.escapeHTML(odd_header)}" unless odd_header.nil? - str << "#{::CGI.escapeHTML(odd_footer)}" unless odd_footer.nil? - - str << "#{::CGI.escapeHTML(even_header)}" unless even_header.nil? - str << "#{::CGI.escapeHTML(even_footer)}" unless even_footer.nil? - - str << "#{::CGI.escapeHTML(first_header)}" unless first_header.nil? - str << "#{::CGI.escapeHTML(first_footer)}" unless first_footer.nil? - + serialized_element_attributes(str) do |value| + value = ::CGI.escapeHTML(value) + end str << "" end end -- cgit v1.2.3