diff options
| author | Randy Morgan <[email protected]> | 2012-09-30 18:29:48 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2012-09-30 18:29:48 +0900 |
| commit | e7a2916e33db7582ae8da307692cab6bd21b6bab (patch) | |
| tree | c5f2d0710747b817e737bbb9811b5a9ef4585f7f | |
| parent | a942008d3b0f4618a3d35541de6f97ad487a463c (diff) | |
| download | caxlsx-e7a2916e33db7582ae8da307692cab6bd21b6bab.tar.gz caxlsx-e7a2916e33db7582ae8da307692cab6bd21b6bab.zip | |
Forgot to add these files to last commit!
extraction refactoring
| -rw-r--r-- | lib/axlsx.rb | 2 | ||||
| -rw-r--r-- | lib/axlsx/util/boolean_attributes.rb | 43 | ||||
| -rw-r--r-- | lib/axlsx/util/module.rb | 20 | ||||
| -rw-r--r-- | lib/axlsx/workbook/defined_name.rb | 7 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb | 8 | ||||
| -rw-r--r-- | test/workbook/tc_defined_name.rb | 4 |
6 files changed, 28 insertions, 56 deletions
diff --git a/lib/axlsx.rb b/lib/axlsx.rb index fe6f8ec3..c0ed2e9c 100644 --- a/lib/axlsx.rb +++ b/lib/axlsx.rb @@ -5,7 +5,7 @@ require 'axlsx/version.rb' require 'axlsx/util/simple_typed_list.rb' require 'axlsx/util/constants.rb' require 'axlsx/util/validators.rb' -require 'axlsx/util/boolean_attributes.rb' +require 'axlsx/util/module.rb' # to be included with parsable intitites. #require 'axlsx/util/parser.rb' diff --git a/lib/axlsx/util/boolean_attributes.rb b/lib/axlsx/util/boolean_attributes.rb deleted file mode 100644 index 48918b63..00000000 --- a/lib/axlsx/util/boolean_attributes.rb +++ /dev/null @@ -1,43 +0,0 @@ -module Axlsx - module BooleanAttributes - def BooleanAttributes.included(mod) - mod::BOOLEAN_ATTRIBUTES.each do |attr| - class_eval %{ - # The #{attr} attribute reader - # @return [Boolean] - attr_reader :#{attr} - - # The #{attr} writer - # @param [Boolean] value The value to assign to #{attr} - # @return [Boolean] - def #{attr}=(value) - Axlsx::validate_boolean(value) - @#{attr} = value - end - } - end - end - end - - module StringAttributes - def StringAttributes.included(mod) - mod::STRING_ATTRIBUTES.each do |attr| - class_eval %{ - # The #{attr} attribute reader - # @return [String] - attr_reader :#{attr} - - # The #{attr} writer - # @param [String] value The value to assign to #{attr} - # @return [String] - def #{attr}=(value) - Axlsx::validate_string(value) - @#{attr}= value - end - } - - end - end - end - -end diff --git a/lib/axlsx/util/module.rb b/lib/axlsx/util/module.rb new file mode 100644 index 00000000..ecc62326 --- /dev/null +++ b/lib/axlsx/util/module.rb @@ -0,0 +1,20 @@ +class Module + def string_attr_accessor(*symbols) + validated_attr_accessor(symbols, 'validate_string') + end + + def boolean_attr_accessor(*symbols) + validated_attr_accessor(symbols, 'validate_boolean') + end + + private + + SETTER = "def %s=(value) Axlsx::%s(value); @%s = value; end" + def validated_attr_accessor(symbols, validator) + symbols.each do |symbol| + attr_reader symbol + module_eval(SETTER % [symbol.to_s, validator, symbol.to_s], __FILE__, __LINE__) + end + end + +end diff --git a/lib/axlsx/workbook/defined_name.rb b/lib/axlsx/workbook/defined_name.rb index b7a752a6..81265280 100644 --- a/lib/axlsx/workbook/defined_name.rb +++ b/lib/axlsx/workbook/defined_name.rb @@ -109,13 +109,10 @@ module Axlsx @local_sheet_id = value end - # string attributes that will be added when this class is evaluated - STRING_ATTRIBUTES = [:short_cut_key, :status_bar, :help, :description, :custom_menu, :comment] - include StringAttributes + string_attr_accessor :short_cut_key, :status_bar, :help, :description, :custom_menu, :comment # boolean attributes that will be added when this class is evaluated - BOOLEAN_ATTRIBUTES = [:workbook_parameter, :publish_to_server, :xlm, :vb_proceedure, :function, :hidden] - include BooleanAttributes + boolean_attr_accessor :workbook_parameter, :publish_to_server, :xlm, :vb_proceedure, :function, :hidden attr_reader :name # The name of this defined name. Please refer to the class documentation for more information diff --git a/lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb b/lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb index 42e99d08..73f769ce 100644 --- a/lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb +++ b/lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb @@ -21,11 +21,9 @@ module Axlsx end yield self if block_given? end - - # String attributes for this object - STRING_ATTRIBUTES = %w(display location tooltip) - include StringAttributes - + + string_attr_accessor :display, :location, :tooltip + #Cell location of hyperlink on worksheet. # @return [String] attr_reader :ref diff --git a/test/workbook/tc_defined_name.rb b/test/workbook/tc_defined_name.rb index 51f5a484..b16b3346 100644 --- a/test/workbook/tc_defined_name.rb +++ b/test/workbook/tc_defined_name.rb @@ -10,14 +10,14 @@ class TestDefinedNames < Test::Unit::TestCase end def test_string_attributes - Axlsx::DefinedName::STRING_ATTRIBUTES.each do |attr| + %w(short_cut_key status_bar help description custom_menu comment).each do |attr| assert_raise(ArgumentError, 'only strings allowed in string attributes') { @dn.send("#{attr}=", 1) } assert_nothing_raised { @dn.send("#{attr}=", '_xlnm.Sheet_Title') } end end def test_boolean_attributes - Axlsx::DefinedName::BOOLEAN_ATTRIBUTES.each do |attr| + %w(workbook_parameter publish_to_server xlm vb_proceedure function hidden).each do |attr| assert_raise(ArgumentError, 'only booleanish allowed in string attributes') { @dn.send("#{attr}=", 'foo') } assert_nothing_raised { @dn.send("#{attr}=", 1) } end |
