axlsx 1.3.1 → 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. data/README.md +13 -5
  2. data/examples/colored_links.rb +59 -0
  3. data/examples/example.rb +421 -266
  4. data/examples/example.xlsx +0 -0
  5. data/examples/example_streamed.xlsx +0 -0
  6. data/examples/finance.rb +82 -0
  7. data/examples/finance.xlsx +0 -0
  8. data/examples/financial.xlsx +0 -0
  9. data/examples/no-use_autowidth.xlsx +0 -0
  10. data/examples/shared_strings_example.xlsx +0 -0
  11. data/examples/where_is_my_color.xlsx +0 -0
  12. data/lib/axlsx.rb +11 -4
  13. data/lib/axlsx/content_type/abstract_content_type.rb +32 -0
  14. data/lib/axlsx/content_type/content_type.rb +1 -1
  15. data/lib/axlsx/content_type/default.rb +6 -37
  16. data/lib/axlsx/content_type/override.rb +6 -38
  17. data/lib/axlsx/doc_props/app.rb +7 -4
  18. data/lib/axlsx/drawing/axis.rb +3 -3
  19. data/lib/axlsx/drawing/chart.rb +2 -3
  20. data/lib/axlsx/drawing/d_lbls.rb +21 -31
  21. data/lib/axlsx/drawing/drawing.rb +6 -0
  22. data/lib/axlsx/drawing/hyperlink.rb +40 -32
  23. data/lib/axlsx/drawing/marker.rb +13 -13
  24. data/lib/axlsx/drawing/num_data.rb +6 -6
  25. data/lib/axlsx/drawing/num_data_source.rb +17 -16
  26. data/lib/axlsx/drawing/one_cell_anchor.rb +20 -22
  27. data/lib/axlsx/drawing/pic.rb +25 -27
  28. data/lib/axlsx/drawing/picture_locking.rb +12 -44
  29. data/lib/axlsx/drawing/scaling.rb +13 -13
  30. data/lib/axlsx/drawing/scatter_chart.rb +3 -3
  31. data/lib/axlsx/drawing/series.rb +3 -6
  32. data/lib/axlsx/drawing/str_data.rb +3 -3
  33. data/lib/axlsx/drawing/str_val.rb +7 -8
  34. data/lib/axlsx/drawing/view_3D.rb +51 -37
  35. data/lib/axlsx/drawing/vml_shape.rb +23 -23
  36. data/lib/axlsx/package.rb +14 -16
  37. data/lib/axlsx/stylesheet/border.rb +29 -20
  38. data/lib/axlsx/stylesheet/border_pr.rb +5 -4
  39. data/lib/axlsx/stylesheet/cell_alignment.rb +55 -29
  40. data/lib/axlsx/stylesheet/cell_protection.rb +7 -4
  41. data/lib/axlsx/stylesheet/cell_style.rb +19 -14
  42. data/lib/axlsx/stylesheet/color.rb +19 -16
  43. data/lib/axlsx/stylesheet/dxf.rb +4 -4
  44. data/lib/axlsx/stylesheet/font.rb +22 -22
  45. data/lib/axlsx/stylesheet/gradient_fill.rb +45 -21
  46. data/lib/axlsx/stylesheet/num_fmt.rb +22 -13
  47. data/lib/axlsx/stylesheet/pattern_fill.rb +12 -21
  48. data/lib/axlsx/stylesheet/styles.rb +1 -1
  49. data/lib/axlsx/stylesheet/table_style.rb +17 -16
  50. data/lib/axlsx/stylesheet/table_style_element.rb +15 -11
  51. data/lib/axlsx/stylesheet/table_styles.rb +14 -11
  52. data/lib/axlsx/stylesheet/xf.rb +28 -26
  53. data/lib/axlsx/util/accessors.rb +49 -0
  54. data/lib/axlsx/util/options_parser.rb +15 -0
  55. data/lib/axlsx/util/serialized_attributes.rb +46 -0
  56. data/lib/axlsx/util/simple_typed_list.rb +16 -4
  57. data/lib/axlsx/version.rb +1 -1
  58. data/lib/axlsx/workbook/defined_name.rb +13 -58
  59. data/lib/axlsx/workbook/workbook.rb +27 -1
  60. data/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb +11 -19
  61. data/lib/axlsx/workbook/worksheet/auto_filter/filters.rb +20 -27
  62. data/lib/axlsx/workbook/worksheet/cell.rb +38 -39
  63. data/lib/axlsx/workbook/worksheet/cfvo.rb +15 -15
  64. data/lib/axlsx/workbook/worksheet/cfvos.rb +18 -0
  65. data/lib/axlsx/workbook/worksheet/col.rb +34 -27
  66. data/lib/axlsx/workbook/worksheet/color_scale.rb +7 -13
  67. data/lib/axlsx/workbook/worksheet/comment.rb +14 -11
  68. data/lib/axlsx/workbook/worksheet/conditional_formatting.rb +11 -11
  69. data/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb +27 -25
  70. data/lib/axlsx/workbook/worksheet/data_bar.rb +44 -34
  71. data/lib/axlsx/workbook/worksheet/data_validation.rb +61 -62
  72. data/lib/axlsx/workbook/worksheet/dimension.rb +0 -1
  73. data/lib/axlsx/workbook/worksheet/icon_set.rb +20 -20
  74. data/lib/axlsx/workbook/worksheet/page_margins.rb +21 -19
  75. data/lib/axlsx/workbook/worksheet/page_set_up_pr.rb +6 -9
  76. data/lib/axlsx/workbook/worksheet/page_setup.rb +20 -19
  77. data/lib/axlsx/workbook/worksheet/pane.rb +48 -51
  78. data/lib/axlsx/workbook/worksheet/print_options.rb +8 -30
  79. data/lib/axlsx/workbook/worksheet/protected_range.rb +16 -13
  80. data/lib/axlsx/workbook/worksheet/selection.rb +30 -38
  81. data/lib/axlsx/workbook/worksheet/sheet_calc_pr.rb +6 -26
  82. data/lib/axlsx/workbook/worksheet/sheet_pr.rb +19 -57
  83. data/lib/axlsx/workbook/worksheet/sheet_protection.rb +51 -155
  84. data/lib/axlsx/workbook/worksheet/sheet_view.rb +68 -234
  85. data/lib/axlsx/workbook/worksheet/table.rb +16 -18
  86. data/lib/axlsx/workbook/worksheet/table_style_info.rb +10 -27
  87. data/lib/axlsx/workbook/worksheet/worksheet.rb +6 -7
  88. data/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +1 -1
  89. data/lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb +11 -33
  90. data/test/content_type/tc_default.rb +0 -11
  91. data/test/content_type/tc_override.rb +0 -13
  92. data/test/drawing/tc_d_lbls.rb +14 -4
  93. data/test/tc_axlsx.rb +20 -2
  94. data/test/workbook/tc_defined_name.rb +2 -2
  95. data/test/workbook/tc_workbook.rb +15 -0
  96. data/test/workbook/worksheet/tc_col.rb +11 -1
  97. data/test/workbook/worksheet/tc_pane.rb +5 -45
  98. data/test/workbook/worksheet/tc_selection.rb +9 -48
  99. metadata +13 -3
@@ -3,6 +3,26 @@ module Axlsx
3
3
  # A NumFmt object defines an identifier and formatting code for data in cells.
4
4
  # @note The recommended way to manage styles is Styles#add_style
5
5
  class NumFmt
6
+
7
+ include Axlsx::OptionsParser
8
+ include Axlsx::SerializedAttributes
9
+
10
+ # Creates a new NumFmt object
11
+ # @param [Hash] options Options for the number format object
12
+ # @option [Integer] numFmtId The predefined format id or new format id for this format
13
+ # @option [String] fomratCode The format code for this number format
14
+ def initialize(options={})
15
+ @numFmtId = 0
16
+ @formatCode = ""
17
+ parse_options options
18
+ end
19
+
20
+ serializable_attributes :formatCode, :numFmtId
21
+
22
+ # @return [String] The formatting to use for this number format.
23
+ # @see http://support.microsoft.com/kb/264372
24
+ attr_reader :formatCode
25
+
6
26
  # @return [Integer] An unsinged integer referencing a standard or custom number format.
7
27
  # @note
8
28
  # These are the known formats I can dig up. The constant NUM_FMT_PERCENT is 9, and uses the default % formatting. Axlsx also defines a few formats for date and time that are commonly used in asia as NUM_FMT_YYYYMMDD and NUM_FRM_YYYYMMDDHHMMSS.
@@ -40,29 +60,18 @@ module Axlsx
40
60
  # @see Axlsx
41
61
  attr_reader :numFmtId
42
62
 
43
- # @return [String] The formatting to use for this number format.
44
- # @see http://support.microsoft.com/kb/264372
45
- attr_reader :formatCode
46
- def initialize(options={})
47
- @numFmtId = 0
48
- @formatCode = ""
49
- options.each do |o|
50
- self.send("#{o[0]}=", o[1]) if self.respond_to? o[0]
51
- end
52
- end
53
-
54
63
  # @see numFmtId
55
64
  def numFmtId=(v) Axlsx::validate_unsigned_int v; @numFmtId = v end
65
+
56
66
  # @see formatCode
57
67
  def formatCode=(v) Axlsx::validate_string v; @formatCode = v end
58
68
 
59
-
60
69
  # Serializes the object
61
70
  # @param [String] str
62
71
  # @return [String]
63
72
  def to_xml_string(str = '')
64
73
  str << '<numFmt '
65
- str << instance_values.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ')
74
+ serialized_attributes str
66
75
  str << '/>'
67
76
  end
68
77
 
@@ -5,6 +5,16 @@ module Axlsx
5
5
  # @see Style#add_style
6
6
  class PatternFill
7
7
 
8
+ include Axlsx::OptionsParser
9
+ # Creates a new PatternFill Object
10
+ # @option options [Symbol] patternType
11
+ # @option options [Color] fgColor
12
+ # @option options [Color] bgColor
13
+ def initialize(options={})
14
+ @patternType = :none
15
+ parse_options options
16
+ end
17
+
8
18
  # The color to use for the the background in solid fills.
9
19
  # @return [Color]
10
20
  attr_reader :fgColor
@@ -38,16 +48,6 @@ module Axlsx
38
48
  # @see Office Open XML Part 1 18.18.55
39
49
  attr_reader :patternType
40
50
 
41
- # Creates a new PatternFill Object
42
- # @option options [Symbol] patternType
43
- # @option options [Color] fgColor
44
- # @option options [Color] bgColor
45
- def initialize(options={})
46
- @patternType = :none
47
- options.each do |o|
48
- self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
49
- end
50
- end
51
51
  # @see fgColor
52
52
  def fgColor=(v) DataTypeValidator.validate "PatternFill.fgColor", Color, v; @fgColor=v end
53
53
  # @see bgColor
@@ -61,22 +61,13 @@ module Axlsx
61
61
  def to_xml_string(str = '')
62
62
  str << '<patternFill patternType="' << patternType.to_s << '">'
63
63
  if fgColor.is_a?(Color)
64
- str << "<fgColor "
65
- fgColor.instance_values.each do |key, value|
66
- str << key.to_s << '="' << value.to_s << '" '
67
- end
68
- str << "/>"
64
+ fgColor.to_xml_string str, "fgColor"
69
65
  end
70
66
 
71
67
  if bgColor.is_a?(Color)
72
- str << "<bgColor "
73
- bgColor.instance_values.each do |key, value|
74
- str << key.to_s << '="' << value.to_s << '" '
75
- end
76
- str << "/>"
68
+ bgColor.to_xml_string str, "bgColor"
77
69
  end
78
70
  str << '</patternFill>'
79
71
  end
80
-
81
72
  end
82
73
  end
@@ -316,7 +316,7 @@ module Axlsx
316
316
  return unless options[:border]
317
317
  b_opts = options[:border]
318
318
  if b_opts.is_a?(Hash)
319
- raise ArgumentError, (ERR_INVALID_BORDER_OPTIONS % b_opts) unless b_opts.values_at(:style, :color).size == 2
319
+ raise ArgumentError, (ERR_INVALID_BORDER_OPTIONS % b_opts) unless b_opts.keys.include?(:style) && b_opts.keys.include?(:color)
320
320
  border = Border.new b_opts
321
321
  (b_opts[:edges] || [:left, :right, :top, :bottom]).each do |edge|
322
322
  b_options = { :name => edge, :style => b_opts[:style], :color => Color.new(:rgb => b_opts[:color]) }
@@ -4,6 +4,22 @@ module Axlsx
4
4
  # @note Table are not supported in this version and only the defaults required for a valid workbook are created.
5
5
  class TableStyle < SimpleTypedList
6
6
 
7
+ include Axlsx::OptionsParser
8
+ include Axlsx::SerializedAttributes
9
+
10
+ # creates a new TableStyle object
11
+ # @raise [ArgumentError] if name option is not provided.
12
+ # @param [String] name
13
+ # @option options [Boolean] pivot
14
+ # @option options [Boolean] table
15
+ def initialize(name, options={})
16
+ self.name = name
17
+ parse_options options
18
+ super TableStyleElement
19
+ end
20
+
21
+ serializable_attributes :name, :pivot, :table
22
+
7
23
  # The name of this table style
8
24
  # @return [string]
9
25
  attr_reader :name
@@ -16,19 +32,6 @@ module Axlsx
16
32
  # @return [Boolean]
17
33
  attr_reader :table
18
34
 
19
- # creates a new TableStyle object
20
- # @raise [ArgumentError] if name option is not provided.
21
- # @param [String] name
22
- # @option options [Boolean] pivot
23
- # @option options [Boolean] table
24
- def initialize(name, options={})
25
- self.name = name
26
- options.each do |o|
27
- self.send("#{o[0]}=", o[1]) if self.respond_to? o[0]
28
- end
29
- super TableStyleElement
30
- end
31
-
32
35
  # @see name
33
36
  def name=(v) Axlsx::validate_string v; @name=v end
34
37
  # @see pivot
@@ -40,10 +43,8 @@ module Axlsx
40
43
  # @param [String] str
41
44
  # @return [String]
42
45
  def to_xml_string(str = '')
43
- attrs = instance_values.reject { |k, v| ![:name, :pivot, :table].include?(k) }
44
- attrs[:count] = self.size
45
46
  str << '<tableStyle '
46
- str << attrs.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ')
47
+ serialized_attributes str, {:count => self.size}
47
48
  str << '>'
48
49
  each { |table_style_el| table_style_el.to_xml_string(str) }
49
50
  str << '</tableStyle>'
@@ -3,6 +3,20 @@ module Axlsx
3
3
  # an element of style that belongs to a table style.
4
4
  # @note tables and table styles are not supported in this version. This class exists in preparation for that support.
5
5
  class TableStyleElement
6
+
7
+ include Axlsx::OptionsParser
8
+ include Axlsx::SerializedAttributes
9
+
10
+ # creates a new TableStyleElement object
11
+ # @option options [Symbol] type
12
+ # @option options [Integer] size
13
+ # @option options [Integer] dxfId
14
+ def initialize(options={})
15
+ parse_options options
16
+ end
17
+
18
+ serializable_attributes :type, :size, :dxfId
19
+
6
20
  # The type of style element. The following type are allowed
7
21
  # :wholeTable
8
22
  # :headerRow
@@ -43,16 +57,6 @@ module Axlsx
43
57
  # @return [Integer]
44
58
  attr_reader :dxfId
45
59
 
46
- # creates a new TableStyleElement object
47
- # @option options [Symbol] type
48
- # @option options [Integer] size
49
- # @option options [Integer] dxfId
50
- def initialize(options={})
51
- options.each do |o|
52
- self.send("#{o[0]}=", o[1]) if self.respond_to? o[0]
53
- end
54
- end
55
-
56
60
  # @see type
57
61
  def type=(v) Axlsx::validate_table_element_type v; @type = v end
58
62
 
@@ -67,7 +71,7 @@ module Axlsx
67
71
  # @return [String]
68
72
  def to_xml_string(str = '')
69
73
  str << '<tableStyleElement '
70
- str << instance_values.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ')
74
+ serialized_attributes str
71
75
  str << '/>'
72
76
  end
73
77
 
@@ -4,13 +4,7 @@ module Axlsx
4
4
  # @note Support for custom table styles does not exist in this version. Many of the classes required are defined in preparation for future release. Please do not attempt to add custom table styles.
5
5
  class TableStyles < SimpleTypedList
6
6
 
7
- # The default table style. The default value is 'TableStyleMedium9'
8
- # @return [String]
9
- attr_reader :defaultTableStyle
10
-
11
- # The default pivot table style. The default value is 'PivotStyleLight6'
12
- # @return [String]
13
- attr_reader :defaultPivotStyle
7
+ include Axlsx::SerializedAttributes
14
8
 
15
9
  # Creates a new TableStyles object that is a container for TableStyle objects
16
10
  # @option options [String] defaultTableStyle
@@ -20,7 +14,18 @@ module Axlsx
20
14
  @defaultPivotStyle = options[:defaultPivotStyle] || "PivotStyleLight16"
21
15
  super TableStyle
22
16
  end
23
- # @see defaultTableStyle
17
+
18
+ serializable_attributes :defaultTableStyle, :defaultPivotStyle
19
+
20
+ # The default table style. The default value is 'TableStyleMedium9'
21
+ # @return [String]
22
+ attr_reader :defaultTableStyle
23
+
24
+ # The default pivot table style. The default value is 'PivotStyleLight6'
25
+ # @return [String]
26
+ attr_reader :defaultPivotStyle
27
+
28
+ # @see defaultTableStyle
24
29
  def defaultTableStyle=(v) Axlsx::validate_string(v); @defaultTableStyle = v; end
25
30
  # @see defaultPivotStyle
26
31
  def defaultPivotStyle=(v) Axlsx::validate_string(v); @defaultPivotStyle = v; end
@@ -29,10 +34,8 @@ module Axlsx
29
34
  # @param [String] str
30
35
  # @return [String]
31
36
  def to_xml_string(str = '')
32
- attr = self.instance_values.reject {|k, v| ![:defaultTableStyle, :defaultPivotStyle].include?(k.to_sym) }
33
- attr[:count] = self.size
34
37
  str << '<tableStyles '
35
- str << attr.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ')
38
+ serialized_attributes str, {:count => self.size }
36
39
  str << '>'
37
40
  each { |table_style| table_style.to_xml_string(str) }
38
41
  str << '</tableStyles>'
@@ -5,6 +5,32 @@ module Axlsx
5
5
  class Xf
6
6
  #does not support extList (ExtensionList)
7
7
 
8
+ include Axlsx::SerializedAttributes
9
+ include Axlsx::OptionsParser
10
+ # Creates a new Xf object
11
+ # @option options [Integer] numFmtId
12
+ # @option options [Integer] fontId
13
+ # @option options [Integer] fillId
14
+ # @option options [Integer] borderId
15
+ # @option options [Integer] xfId
16
+ # @option options [Boolean] quotePrefix
17
+ # @option options [Boolean] pivotButton
18
+ # @option options [Boolean] applyNumberFormat
19
+ # @option options [Boolean] applyFont
20
+ # @option options [Boolean] applyFill
21
+ # @option options [Boolean] applyBorder
22
+ # @option options [Boolean] applyAlignment
23
+ # @option options [Boolean] applyProtection
24
+ # @option options [CellAlignment] alignment
25
+ # @option options [CellProtection] protection
26
+ def initialize(options={})
27
+ parse_options options
28
+ end
29
+
30
+ serializable_attributes :numFmtId, :fontId, :fillId, :borderId, :xfId, :quotePrefix,
31
+ :pivotButton, :applyNumberFormat, :applyFont, :applyFill, :applyBorder, :applyAlignment,
32
+ :applyProtection
33
+
8
34
  # The cell alignment for this style
9
35
  # @return [CellAlignment]
10
36
  # @see CellAlignment
@@ -67,30 +93,7 @@ module Axlsx
67
93
  # @return [Boolean]
68
94
  attr_reader :applyProtection
69
95
 
70
- # Creates a new Xf object
71
- # @option options [Integer] numFmtId
72
- # @option options [Integer] fontId
73
- # @option options [Integer] fillId
74
- # @option options [Integer] borderId
75
- # @option options [Integer] xfId
76
- # @option options [Boolean] quotePrefix
77
- # @option options [Boolean] pivotButton
78
- # @option options [Boolean] applyNumberFormat
79
- # @option options [Boolean] applyFont
80
- # @option options [Boolean] applyFill
81
- # @option options [Boolean] applyBorder
82
- # @option options [Boolean] applyAlignment
83
- # @option options [Boolean] applyProtection
84
- # @option options [CellAlignment] alignment
85
- # @option options [CellProtection] protection
86
- def initialize(options={})
87
- options.each do |o|
88
- next if o[1].nil?
89
- self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
90
- end
91
- end
92
-
93
- # @see Xf#alignment
96
+ # @see Xf#alignment
94
97
  def alignment=(v) DataTypeValidator.validate "Xf.alignment", CellAlignment, v; @alignment = v end
95
98
 
96
99
  # @see protection
@@ -132,8 +135,7 @@ module Axlsx
132
135
  # @return [String]
133
136
  def to_xml_string(str = '')
134
137
  str << '<xf '
135
- h = instance_values.reject { |k, v| [:alignment, :protection, :extList, :name].include? k.to_sym}
136
- str << h.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ')
138
+ serialized_attributes str
137
139
  str << '>'
138
140
  alignment.to_xml_string(str) if self.alignment
139
141
  protection.to_xml_string(str) if self.protection
@@ -0,0 +1,49 @@
1
+ module Axlsx
2
+ # This module defines some of the more common validating attribute
3
+ # accessors that we use in Axlsx
4
+ #
5
+ # When this module is included in your class you can simply call
6
+ #
7
+ # string_attr_access :foo
8
+ #
9
+ # To generate a new, validating set of accessors for foo.
10
+ module Accessors
11
+ def self.included(base)
12
+ base.send :extend, ClassMethods
13
+ end
14
+
15
+ # Defines the class level xxx_attr_accessor methods
16
+ module ClassMethods
17
+
18
+ # Creates one or more string validated attr_accessors
19
+ # @param [Array] symbols An array of symbols representing the
20
+ # names of the attributes you will add to your class.
21
+ def string_attr_accessor(*symbols)
22
+ validated_attr_accessor(symbols, 'validate_string')
23
+ end
24
+
25
+ # Creates on or more boolean validated attr_accessors
26
+ # @param [Array] symbols An array of symbols representing the
27
+ # names of the attributes you will add to your class.
28
+ def boolean_attr_accessor(*symbols)
29
+ validated_attr_accessor(symbols, 'validate_boolean')
30
+ end
31
+
32
+ # Template for defining validated write accessors
33
+ SETTER = "def %s=(value) Axlsx::%s(value); @%s = value; end"
34
+
35
+ # Creates the reader and writer access methods
36
+ # @param [Array] symbols The names of the attributes to create
37
+ # @param [String] validator The axlsx validation method to use when
38
+ # validating assignation.
39
+ # @see lib/axlsx/util/validators.rb
40
+ def validated_attr_accessor(symbols, validator)
41
+ symbols.each do |symbol|
42
+ attr_reader symbol
43
+ module_eval(SETTER % [symbol.to_s, validator, symbol.to_s], __FILE__, __LINE__)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+
@@ -0,0 +1,15 @@
1
+ module Axlsx
2
+ # This module defines a single method for parsing options in class
3
+ # initializers.
4
+ module OptionsParser
5
+
6
+ # Parses an options hash by calling any defined method by the same
7
+ # name of the key postfixed with an '='
8
+ # @param [Hash] options Options to parse.
9
+ def parse_options(options={})
10
+ options.each do |key, value|
11
+ self.send("#{key}=", value) if self.respond_to?("#{key}=") && value != nil
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,46 @@
1
+ module Axlsx
2
+ # This module allows us to define a list of symbols defining which
3
+ # attributes will be serialized for a class.
4
+ module SerializedAttributes
5
+
6
+ # Extend with class methods
7
+ def self.included(base)
8
+ base.send :extend, ClassMethods
9
+ end
10
+
11
+ # class methods applied to all includers
12
+ module ClassMethods
13
+
14
+ # This is the method to be used in inheriting classes to specify
15
+ # which of the instance values are serializable
16
+ def serializable_attributes(*symbols)
17
+ @xml_attributes = symbols
18
+ end
19
+
20
+ # a reader for those attributes
21
+ def xml_attributes
22
+ @xml_attributes
23
+ end
24
+ end
25
+
26
+ # serializes the instance values of the defining object based on the
27
+ # list of serializable attributes.
28
+ # @param [String] str The string instance to append this
29
+ # serialization to.
30
+ # @param [Hash] additional_attributes An option key value hash for
31
+ # defining values that are not serializable attributes list.
32
+ def serialized_attributes(str = '', additional_attributes = {})
33
+ key_value_pairs = instance_values
34
+ key_value_pairs.each do |key, value|
35
+ key_value_pairs.delete(key) if value == nil
36
+ key_value_pairs.delete(key) unless self.class.xml_attributes.include?(key.to_sym)
37
+ end
38
+ key_value_pairs.merge! additional_attributes
39
+
40
+ key_value_pairs.each do |key, value|
41
+ str << "#{Axlsx.camel(key, false)}=\"#{value}\" "
42
+ end
43
+ str
44
+ end
45
+ end
46
+ end