From 0ec328213fbe31cd4456dceecda6d7ba8d1c9021 Mon Sep 17 00:00:00 2001 From: Stefan Daschek Date: Wed, 12 Jun 2013 23:08:14 +0200 Subject: Allow overriding the 'created' timestamp in the docprops. Can be specified as option to Package#new: ``` Axlsx::Package.new :created_at => time ``` If omitted, the current time at the moment the document is serialized will be used. This change is therefore fully backward compatible. --- lib/axlsx/doc_props/core.rb | 7 ++++++- lib/axlsx/package.rb | 2 ++ test/doc_props/tc_core.rb | 7 +++++++ test/tc_package.rb | 6 ++++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/axlsx/doc_props/core.rb b/lib/axlsx/doc_props/core.rb index d71300e0..a74031f2 100644 --- a/lib/axlsx/doc_props/core.rb +++ b/lib/axlsx/doc_props/core.rb @@ -8,14 +8,19 @@ module Axlsx # Creates a new Core object. # @option options [String] creator + # @option options [Time] created def initialize(options={}) @creator = options[:creator] || 'axlsx' + @created = options[:created] end # The author of the document. By default this is 'axlsx' # @return [String] attr_accessor :creator + # Creation time of the document. If nil, the current time will be used. + attr_accessor :created + # serializes the core.xml document # @return [String] def to_xml_string(str = '') @@ -24,7 +29,7 @@ module Axlsx str << 'xmlns:dcmitype="' << CORE_NS_DCMIT << '" xmlns:dcterms="' << CORE_NS_DCT << '" ' str << 'xmlns:xsi="' << CORE_NS_XSI << '">' str << '' << self.creator << '' - str << '' << Time.now.strftime('%Y-%m-%dT%H:%M:%S') << 'Z' + str << '' << (created || Time.now).strftime('%Y-%m-%dT%H:%M:%S') << 'Z' str << '0' str << '' end diff --git a/lib/axlsx/package.rb b/lib/axlsx/package.rb index df87ed12..5a5a2169 100644 --- a/lib/axlsx/package.rb +++ b/lib/axlsx/package.rb @@ -17,12 +17,14 @@ module Axlsx # # @param [Hash] options A hash that you can use to specify the author and workbook for this package. # @option options [String] :author The author of the document + # @option options [Time] :created_at Timestamp in the document properties (defaults to current time). # @option options [Boolean] :use_shared_strings This is passed to the workbook to specify that shared strings should be used when serializing the package. # @example Package.new :author => 'you!', :workbook => Workbook.new def initialize(options={}) @workbook = nil @core, @app = Core.new, App.new @core.creator = options[:author] || @core.creator + @core.created = options[:created_at] parse_options options yield self if block_given? end diff --git a/test/doc_props/tc_core.rb b/test/doc_props/tc_core.rb index cfff4d59..0eddfed7 100644 --- a/test/doc_props/tc_core.rb +++ b/test/doc_props/tc_core.rb @@ -23,6 +23,13 @@ class TestCore < Test::Unit::TestCase assert_equal(@doc.xpath('//dcterms:created').text, @time, "dcterms:created incorrect") end + def test_created_as_option + time = Time.utc(2013, 1, 1, 12, 00) + c = Axlsx::Core.new :created => time + doc = Nokogiri::XML(c.to_xml_string) + assert_equal(doc.xpath('//dcterms:created').text, time.xmlschema, "dcterms:created incorrect") + end + def test_populates_default_name assert_equal(@doc.xpath('//dc:creator').text, "axlsx", "Default name not populated") end diff --git a/test/tc_package.rb b/test/tc_package.rb index 5fd411e0..df096a31 100644 --- a/test/tc_package.rb +++ b/test/tc_package.rb @@ -105,6 +105,12 @@ class TestPackage < Test::Unit::TestCase assert(Axlsx::Package.new.workbook.worksheets.size == 0, 'Workbook should not have sheets by default') end + def test_created_at_is_propagated_to_core + time = Time.utc(2013, 1, 1, 12, 0) + p = Axlsx::Package.new :created_at => time + assert_equal(time, p.core.created) + end + def test_serialization assert_nothing_raised do begin -- cgit v1.2.3