diff options
| author | Randy Morgan <[email protected]> | 2012-10-19 09:36:51 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2012-10-19 09:36:51 +0900 |
| commit | 51dcfe0c141096da1bc513aef8c901f3872778c7 (patch) | |
| tree | b77285fc604d9d8cb703bae430e0a46d30b0c17b | |
| parent | 0acc2dc619d98075d4e971d9e9cc765b8ff4f62c (diff) | |
| download | caxlsx-51dcfe0c141096da1bc513aef8c901f3872778c7.tar.gz caxlsx-51dcfe0c141096da1bc513aef8c901f3872778c7.zip | |
refactored content types.
| -rw-r--r-- | lib/axlsx/content_type/abstract_content_type.rb | 28 | ||||
| -rw-r--r-- | lib/axlsx/content_type/content_type.rb | 2 | ||||
| -rw-r--r-- | lib/axlsx/content_type/default.rb | 41 | ||||
| -rw-r--r-- | lib/axlsx/content_type/override.rb | 42 | ||||
| -rw-r--r-- | test/content_type/tc_default.rb | 8 | ||||
| -rw-r--r-- | test/content_type/tc_override.rb | 10 |
6 files changed, 38 insertions, 93 deletions
diff --git a/lib/axlsx/content_type/abstract_content_type.rb b/lib/axlsx/content_type/abstract_content_type.rb new file mode 100644 index 00000000..ac17dce9 --- /dev/null +++ b/lib/axlsx/content_type/abstract_content_type.rb @@ -0,0 +1,28 @@ +module Axlsx + + class AbstractContentType + + include Axlsx::OptionsParser + + def initialize(options={}) + parse_options options + end + + # The type of content. + # @return [String] + attr_reader :content_type + alias :ContentType :content_type + + # The content type. + # @see Axlsx#validate_content_type + def content_type=(v) Axlsx::validate_content_type v; @content_type = v end + alias :ContentType= :content_type= + + def to_xml_string(node_name = '', str = '') + str << "<#{node_name} " + str << instance_values.map { |key, value| '' << Axlsx::camel(key) << '="' << value.to_s << '"' }.join(' ') + str << '/>' + end + + end +end diff --git a/lib/axlsx/content_type/content_type.rb b/lib/axlsx/content_type/content_type.rb index 1d528097..94d65296 100644 --- a/lib/axlsx/content_type/content_type.rb +++ b/lib/axlsx/content_type/content_type.rb @@ -1,6 +1,6 @@ # encoding: UTF-8 module Axlsx - + require 'axlsx/content_type/abstract_content_type.rb' require 'axlsx/content_type/default.rb' require 'axlsx/content_type/override.rb' diff --git a/lib/axlsx/content_type/default.rb b/lib/axlsx/content_type/default.rb index 2b784ebf..0cd00d48 100644 --- a/lib/axlsx/content_type/default.rb +++ b/lib/axlsx/content_type/default.rb @@ -2,55 +2,22 @@ module Axlsx # An default content part. These parts are automatically created by for you based on the content of your package. - class Default + class Default < AbstractContentType - include Axlsx::OptionsParser - - #Creates a new Default object - # @option options [String] extension - # @option options [String] content_type - # @raise [ArgumentError] An argument error is raised if both extension and content_type are not specified. - def initialize(options={}) - raise ArgumentError, INVALID_ARGUMENTS unless validate_options(options) - parse_options options - end - - # Error string for option validation - INVALID_ARGUMENTS = "extension and content_type are required" + NODE_NAME = 'Default' # The extension of the content type. # @return [String] attr_reader :extension alias :Extension :extension - # The type of content. - # @return [String] - attr_reader :content_type - alias :ContentType :content_type - # Sets the file extension for this content type. def extension=(v) Axlsx::validate_string v; @extension = v end alias :Extension= :extension= - # Sets the content type - # @see Axlsx#validate_content_type - def content_type=(v) Axlsx::validate_content_type v; @content_type = v end - alias :ContentType= :content_type= - - # Serializes the object - # @param [String] str - # @return [String] - def to_xml_string(str = '') - str << '<Default ' - str << instance_values.map { |key, value| '' << Axlsx::camel(key) << '="' << value.to_s << '"' }.join(' ') - str << '/>' + def to_xml_string(str ='') + super(NODE_NAME, str) end - - private - def validate_options(options) - (options[:Extension] || options[:extension]) && (options[:content_type] || options[:ContentType]) - end - end end diff --git a/lib/axlsx/content_type/override.rb b/lib/axlsx/content_type/override.rb index a3947d99..01918321 100644 --- a/lib/axlsx/content_type/override.rb +++ b/lib/axlsx/content_type/override.rb @@ -1,27 +1,11 @@ + # encoding: UTF-8 module Axlsx # An override content part. These parts are automatically created by for you based on the content of your package. - class Override - - include Axlsx::OptionsParser - - #Creates a new Override object - # @option options [String] PartName - # @option options [String] ContentType - # @raise [ArgumentError] An argument error is raised if both PartName and ContentType are not specified. - def initialize(options={}) - raise ArgumentError, INVALID_ARGUMENTS unless validate_options(options) - parse_options options - end + class Override < AbstractContentType - # Error message for invalid options - INVALID_ARGUMENTS = 'part_name and content_type are required' - - # The type of content. - # @return [String] - attr_reader :content_type - alias :ContentType :content_type + NODE_NAME = 'Override' # The name and location of the part. # @return [String] @@ -32,26 +16,8 @@ module Axlsx def part_name=(v) Axlsx::validate_string v; @part_name = v end alias :PartName= :part_name= - # The content type. - # @see Axlsx#validate_content_type - def content_type=(v) Axlsx::validate_content_type v; @content_type = v end - alias :ContentType= :content_type= - - # Serializes the object - # @param [String] str - # @return [String] def to_xml_string(str = '') - str << '<Override ' - str << instance_values.map { |key, value| '' << Axlsx::camel(key) << '="' << value.to_s << '"' }.join(' ') - str << '/>' - end - - private - - def validate_options(options) - (options[:PartName] || options[:part_name]) && (options[:ContentType] || options[:content_type]) + super(NODE_NAME, str) end - end - end diff --git a/test/content_type/tc_default.rb b/test/content_type/tc_default.rb index 2fe0d965..3abfd793 100644 --- a/test/content_type/tc_default.rb +++ b/test/content_type/tc_default.rb @@ -3,14 +3,6 @@ require 'tc_helper.rb' class TestDefault < Test::Unit::TestCase - def test_initialization_requires_Extension_and_ContentType - assert_raise(ArgumentError, "raises argument error if Extension and/or ContentType are not specified") { Axlsx::Default.new } - assert_raise(ArgumentError, "raises argument error if Extension and/or ContentType are not specified") { Axlsx::Default.new :Extension=>"xml" } - assert_raise(ArgumentError, "raises argument error if Extension and/or ContentType are not specified") { Axlsx::Default.new :ContentType=>"asdf" } - - assert_nothing_raised {Axlsx::Default.new :Extension=>"foo", :ContentType=>Axlsx::XML_CT} - - end def test_content_type_restriction assert_raise(ArgumentError, "raises argument error if invlalid ContentType is") { Axlsx::Default.new :ContentType=>"asdf" } end diff --git a/test/content_type/tc_override.rb b/test/content_type/tc_override.rb index 920f1667..c042299a 100644 --- a/test/content_type/tc_override.rb +++ b/test/content_type/tc_override.rb @@ -3,15 +3,7 @@ require 'tc_helper.rb' class TestOverride < Test::Unit::TestCase - def test_initialization_requires_Extension_and_ContentType - err = "requires PartName and ContentType options" - assert_raise(ArgumentError, err) { Axlsx::Override.new } - assert_raise(ArgumentError, err) { Axlsx::Override.new :PartName=>"xml" } - assert_raise(ArgumentError, err) { Axlsx::Override.new :ContentType=>"asdf" } - assert_nothing_raised {Axlsx::Override.new :PartName=>"foo", :ContentType=>Axlsx::CHART_CT} - end - - def test_content_type_restriction + def test_content_type_restriction assert_raise(ArgumentError, "requires known content type") { Axlsx::Override.new :ContentType=>"asdf" } end |
