diff options
| author | Randy Morgan <[email protected]> | 2012-09-30 18:29:19 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2012-09-30 18:29:19 +0900 |
| commit | a942008d3b0f4618a3d35541de6f97ad487a463c (patch) | |
| tree | 4444bf99b68d64d1ea072509b70cff8e2d2fd2e2 | |
| parent | abc54f36bfd9975c5e2f2968cfac3405c7a4d795 (diff) | |
| download | caxlsx-a942008d3b0f4618a3d35541de6f97ad487a463c.tar.gz caxlsx-a942008d3b0f4618a3d35541de6f97ad487a463c.zip | |
Extracted STRING_ATTRIBUTES and BOOLEAN_ATTRIBUTES into mixin from module
replaced with attr_accessor like methods:
string_attr_accessor
boolean_attr_accessor
defined in module.rb
| -rw-r--r-- | lib/axlsx/drawing/d_lbls.rb | 32 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/sheet_pr.rb | 32 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/table_style_info.rb | 5 | ||||
| -rw-r--r-- | test/drawing/tc_d_lbls.rb | 18 |
4 files changed, 32 insertions, 55 deletions
diff --git a/lib/axlsx/drawing/d_lbls.rb b/lib/axlsx/drawing/d_lbls.rb index 7dccf911..cb0f9ad5 100644 --- a/lib/axlsx/drawing/d_lbls.rb +++ b/lib/axlsx/drawing/d_lbls.rb @@ -23,13 +23,20 @@ module Axlsx # not all charts support all methods! # Bar3DChart and Line3DChart and ScatterChart do not support d_lbl_pos or show_leader_lines # - BOOLEAN_ATTRIBUTES = [:show_legend_key, :show_val, :show_cat_name, :show_ser_name, :show_percent, :show_bubble_size, :show_leader_lines] - include BooleanAttributes + boolean_attr_accessor :show_legend_key, + :show_val, + :show_cat_name, + :show_ser_name, + :show_percent, + :show_bubble_size, + :show_leader_lines # Initialize all the values to false as Excel requires them to # explicitly be disabled or all will show. def initialize_defaults - BOOLEAN_ATTRIBUTES.each do |attr| + [:show_legend_key, :show_val, :show_cat_name, + :show_ser_name, :show_percent, :show_bubble_size, + :show_leader_lines].each do |attr| self.send("#{attr}=", false) end end @@ -59,24 +66,7 @@ module Axlsx @d_lbl_pos = label_position end - # Dynamically create accessors for boolean attriubtes - 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 - - + # serializes the data labels # @return [String] def to_xml_string(str = '') diff --git a/lib/axlsx/workbook/worksheet/sheet_pr.rb b/lib/axlsx/workbook/worksheet/sheet_pr.rb index a62f4fe9..94b33f5e 100644 --- a/lib/axlsx/workbook/worksheet/sheet_pr.rb +++ b/lib/axlsx/workbook/worksheet/sheet_pr.rb @@ -6,15 +6,15 @@ module Axlsx # These attributes are all boolean so I'm doing a bit of a hand # waving magic show to set up the attriubte accessors - BOOLEAN_ATTRIBUTES = [:sync_horizontal, - :sync_vertical, - :transtion_evaluation, + boolean_attr_accessor :sync_horizontal, + :sync_vertical, + :transtion_evaluation, :transition_entry, :published, :filter_mode, - :enable_format_conditions_calculation] + :enable_format_conditions_calculation - include BooleanAttributes + string_attr_accessor :code_name, :sync_ref # Creates a new SheetPr object # @param [Worksheet] worksheet The worksheet that owns this SheetPr object @@ -27,32 +27,10 @@ module Axlsx end end - # Anchor point for worksheet's window. - # @return [String] - attr_reader :code_name - - # Specifies a stable name of the sheet, which should not change over time, - # and does not change from user input. This name should be used by code - # to reference a particular sheet. - # @return [String] - attr_reader :sync_ref - # The worksheet these properties apply to! # @return [Worksheet] attr_reader :worksheet - # @see code_name - # @param [String] name - def code_name=(name) - @code_name = name - end - - # @see sync_ref - # @param [String] ref A cell reference (e.g. "A1") - def sync_ref=(ref) - @sync_ref = ref - end - # Serialize the object # @param [String] str serialized output will be appended to this object if provided. # @return [String] diff --git a/lib/axlsx/workbook/worksheet/table_style_info.rb b/lib/axlsx/workbook/worksheet/table_style_info.rb index 4bb34580..82320b56 100644 --- a/lib/axlsx/workbook/worksheet/table_style_info.rb +++ b/lib/axlsx/workbook/worksheet/table_style_info.rb @@ -25,13 +25,12 @@ module Axlsx end # boolean attributes for this object - BOOLEAN_ATTRIBUTES = %w(show_first_column show_last_column show_row_stripes show_column_stripes) - include BooleanAttributes + boolean_attr_accessor :show_first_column, :show_last_column, :show_row_stripes, :show_column_stripes # Initialize all the values to false as Excel requires them to # explicitly be disabled or all will show. def initialize_defaults - BOOLEAN_ATTRIBUTES.each do |attr| + %w(show_first_column show_last_column show_row_stripes show_column_stripes).each do |attr| self.send("#{attr}=", 0) end end diff --git a/test/drawing/tc_d_lbls.rb b/test/drawing/tc_d_lbls.rb index 5bd52a92..b8d3d1c4 100644 --- a/test/drawing/tc_d_lbls.rb +++ b/test/drawing/tc_d_lbls.rb @@ -4,19 +4,29 @@ class TestDLbls < Test::Unit::TestCase def setup @d_lbls = Axlsx::DLbls.new(Axlsx::Pie3DChart) + @boolean_attributes =[:show_legend_key, + :show_val, + :show_cat_name, + :show_ser_name, + :show_percent, + :show_bubble_size, + :show_leader_lines] end def test_initialization assert_equal(:bestFit, @d_lbls.d_lbl_pos) - Axlsx::DLbls::BOOLEAN_ATTRIBUTES.each do |attr| + @boolean_attributes.each do |attr| assert_equal(false, @d_lbls.send(attr)) end end def test_initialization_with_optoins - options_hash = Hash[*[Axlsx::DLbls::BOOLEAN_ATTRIBUTES.map { |name| [name, true] }] ] + + options_hash = Hash[*[@boolean_attributes.map { |name| [name, true] }] ] + d_lbls = Axlsx::DLbls.new(Axlsx::Pie3DChart, options_hash.merge( { :d_lbl_pos => :t })) - Axlsx::DLbls::BOOLEAN_ATTRIBUTES.each do |attr| + + @boolean_attributes.each do |attr| assert_equal(true, d_lbls.send(attr), "boolean attributes set by options") end assert_equal(:t, d_lbls.d_lbl_pos, "d_lbl_pos set by options") @@ -27,7 +37,7 @@ class TestDLbls < Test::Unit::TestCase end def test_boolean_attributes - Axlsx::DLbls::BOOLEAN_ATTRIBUTES.each do |attr| + @boolean_attributes.each do |attr| assert_raise(ArgumentError, "rejects non boolean value for #{attr}") { @d_lbls.send("#{attr}=", :foo) } assert_nothing_raised("accepts boolean value for #{attr}") { @d_lbls.send("#{attr}=", true) } assert_nothing_raised("accepts boolean value for #{attr}") { @d_lbls.send("#{attr}=", false) } |
