summaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2012-11-25 13:53:46 +0900
committerRandy Morgan <[email protected]>2012-11-25 13:53:46 +0900
commitbc256511fc600030746206f25b25d18385f5f2c2 (patch)
tree237edfd1cf5e32702fb163b7b172a763d149a3fd /lib
parent171ffca724ba0bbf845af1f4c7712e663500d4e8 (diff)
downloadcaxlsx-bc256511fc600030746206f25b25d18385f5f2c2.tar.gz
caxlsx-bc256511fc600030746206f25b25d18385f5f2c2.zip
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.
Diffstat (limited to 'lib')
-rw-r--r--lib/axlsx/util/serialized_attributes.rb31
-rw-r--r--lib/axlsx/workbook/worksheet/header_footer.rb15
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}</#{element_name}>"
+ 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 << "<headerFooter "
serialized_attributes str
str << ">"
-
- str << "<oddHeader>#{::CGI.escapeHTML(odd_header)}</oddHeader>" unless odd_header.nil?
- str << "<oddFooter>#{::CGI.escapeHTML(odd_footer)}</oddFooter>" unless odd_footer.nil?
-
- str << "<evenHeader>#{::CGI.escapeHTML(even_header)}</evenHeader>" unless even_header.nil?
- str << "<evenFooter>#{::CGI.escapeHTML(even_footer)}</evenFooter>" unless even_footer.nil?
-
- str << "<firstHeader>#{::CGI.escapeHTML(first_header)}</firstHeader>" unless first_header.nil?
- str << "<firstFooter>#{::CGI.escapeHTML(first_footer)}</firstFooter>" unless first_footer.nil?
-
+ serialized_element_attributes(str) do |value|
+ value = ::CGI.escapeHTML(value)
+ end
str << "</headerFooter>"
end
end