caxlsx 3.3.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -0
  3. data/README.md +25 -7
  4. data/Rakefile +7 -6
  5. data/lib/axlsx/content_type/abstract_content_type.rb +11 -9
  6. data/lib/axlsx/content_type/content_type.rb +7 -9
  7. data/lib/axlsx/content_type/default.rb +4 -6
  8. data/lib/axlsx/content_type/override.rb +3 -5
  9. data/lib/axlsx/doc_props/app.rb +27 -30
  10. data/lib/axlsx/doc_props/core.rb +9 -12
  11. data/lib/axlsx/drawing/area_chart.rb +13 -14
  12. data/lib/axlsx/drawing/area_series.rb +13 -14
  13. data/lib/axlsx/drawing/ax_data_source.rb +3 -6
  14. data/lib/axlsx/drawing/axes.rb +10 -9
  15. data/lib/axlsx/drawing/axis.rb +27 -30
  16. data/lib/axlsx/drawing/bar_3D_chart.rb +17 -18
  17. data/lib/axlsx/drawing/bar_chart.rb +16 -17
  18. data/lib/axlsx/drawing/bar_series.rb +9 -13
  19. data/lib/axlsx/drawing/bubble_chart.rb +8 -9
  20. data/lib/axlsx/drawing/bubble_series.rb +9 -10
  21. data/lib/axlsx/drawing/cat_axis.rb +14 -17
  22. data/lib/axlsx/drawing/chart.rb +25 -28
  23. data/lib/axlsx/drawing/d_lbls.rb +29 -26
  24. data/lib/axlsx/drawing/drawing.rb +60 -62
  25. data/lib/axlsx/drawing/graphic_frame.rb +6 -7
  26. data/lib/axlsx/drawing/hyperlink.rb +12 -13
  27. data/lib/axlsx/drawing/line_3D_chart.rb +13 -15
  28. data/lib/axlsx/drawing/line_chart.rb +13 -14
  29. data/lib/axlsx/drawing/line_series.rb +13 -14
  30. data/lib/axlsx/drawing/marker.rb +14 -16
  31. data/lib/axlsx/drawing/num_data.rb +13 -16
  32. data/lib/axlsx/drawing/num_data_source.rb +11 -13
  33. data/lib/axlsx/drawing/num_val.rb +9 -10
  34. data/lib/axlsx/drawing/one_cell_anchor.rb +10 -10
  35. data/lib/axlsx/drawing/pic.rb +57 -22
  36. data/lib/axlsx/drawing/picture_locking.rb +6 -7
  37. data/lib/axlsx/drawing/pie_3D_chart.rb +6 -9
  38. data/lib/axlsx/drawing/pie_series.rb +9 -12
  39. data/lib/axlsx/drawing/scaling.rb +9 -10
  40. data/lib/axlsx/drawing/scatter_chart.rb +9 -10
  41. data/lib/axlsx/drawing/scatter_series.rb +15 -16
  42. data/lib/axlsx/drawing/ser_axis.rb +9 -11
  43. data/lib/axlsx/drawing/series.rb +8 -8
  44. data/lib/axlsx/drawing/series_title.rb +6 -6
  45. data/lib/axlsx/drawing/str_data.rb +10 -13
  46. data/lib/axlsx/drawing/str_val.rb +8 -9
  47. data/lib/axlsx/drawing/title.rb +23 -27
  48. data/lib/axlsx/drawing/two_cell_anchor.rb +8 -8
  49. data/lib/axlsx/drawing/val_axis.rb +5 -6
  50. data/lib/axlsx/drawing/view_3D.rb +32 -30
  51. data/lib/axlsx/drawing/vml_drawing.rb +19 -20
  52. data/lib/axlsx/drawing/vml_shape.rb +25 -26
  53. data/lib/axlsx/package.rb +81 -79
  54. data/lib/axlsx/rels/relationship.rb +30 -28
  55. data/lib/axlsx/rels/relationships.rb +7 -8
  56. data/lib/axlsx/stylesheet/border.rb +7 -8
  57. data/lib/axlsx/stylesheet/border_pr.rb +8 -8
  58. data/lib/axlsx/stylesheet/cell_alignment.rb +14 -20
  59. data/lib/axlsx/stylesheet/cell_protection.rb +6 -7
  60. data/lib/axlsx/stylesheet/cell_style.rb +12 -14
  61. data/lib/axlsx/stylesheet/color.rb +15 -12
  62. data/lib/axlsx/stylesheet/dxf.rb +7 -9
  63. data/lib/axlsx/stylesheet/fill.rb +3 -5
  64. data/lib/axlsx/stylesheet/font.rb +24 -21
  65. data/lib/axlsx/stylesheet/gradient_fill.rb +9 -9
  66. data/lib/axlsx/stylesheet/gradient_stop.rb +7 -6
  67. data/lib/axlsx/stylesheet/num_fmt.rb +9 -14
  68. data/lib/axlsx/stylesheet/pattern_fill.rb +8 -8
  69. data/lib/axlsx/stylesheet/styles.rb +104 -98
  70. data/lib/axlsx/stylesheet/table_style.rb +8 -9
  71. data/lib/axlsx/stylesheet/table_style_element.rb +7 -8
  72. data/lib/axlsx/stylesheet/table_styles.rb +8 -10
  73. data/lib/axlsx/stylesheet/xf.rb +21 -22
  74. data/lib/axlsx/util/accessors.rb +6 -6
  75. data/lib/axlsx/util/buffered_zip_output_stream.rb +60 -0
  76. data/lib/axlsx/util/constants.rb +119 -108
  77. data/lib/axlsx/util/mime_type_utils.rb +11 -0
  78. data/lib/axlsx/util/options_parser.rb +4 -3
  79. data/lib/axlsx/util/serialized_attributes.rb +45 -21
  80. data/lib/axlsx/util/simple_typed_list.rb +58 -57
  81. data/lib/axlsx/util/storage.rb +38 -41
  82. data/lib/axlsx/util/validators.rb +107 -44
  83. data/lib/axlsx/util/zip_command.rb +10 -12
  84. data/lib/axlsx/version.rb +3 -2
  85. data/lib/axlsx/workbook/defined_name.rb +11 -8
  86. data/lib/axlsx/workbook/defined_names.rb +4 -3
  87. data/lib/axlsx/workbook/shared_strings_table.rb +10 -11
  88. data/lib/axlsx/workbook/workbook.rb +121 -114
  89. data/lib/axlsx/workbook/workbook_view.rb +8 -11
  90. data/lib/axlsx/workbook/workbook_views.rb +4 -4
  91. data/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb +72 -14
  92. data/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb +11 -7
  93. data/lib/axlsx/workbook/worksheet/auto_filter/filters.rb +24 -21
  94. data/lib/axlsx/workbook/worksheet/auto_filter/sort_condition.rb +51 -0
  95. data/lib/axlsx/workbook/worksheet/auto_filter/sort_state.rb +56 -0
  96. data/lib/axlsx/workbook/worksheet/border_creator.rb +30 -25
  97. data/lib/axlsx/workbook/worksheet/break.rb +4 -5
  98. data/lib/axlsx/workbook/worksheet/cell.rb +92 -65
  99. data/lib/axlsx/workbook/worksheet/cell_serializer.rb +32 -28
  100. data/lib/axlsx/workbook/worksheet/cfvo.rb +7 -5
  101. data/lib/axlsx/workbook/worksheet/cfvos.rb +5 -5
  102. data/lib/axlsx/workbook/worksheet/col.rb +9 -10
  103. data/lib/axlsx/workbook/worksheet/col_breaks.rb +8 -7
  104. data/lib/axlsx/workbook/worksheet/color_scale.rb +16 -16
  105. data/lib/axlsx/workbook/worksheet/cols.rb +9 -7
  106. data/lib/axlsx/workbook/worksheet/comment.rb +12 -11
  107. data/lib/axlsx/workbook/worksheet/comments.rb +10 -12
  108. data/lib/axlsx/workbook/worksheet/conditional_formatting.rb +12 -8
  109. data/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb +19 -21
  110. data/lib/axlsx/workbook/worksheet/conditional_formattings.rb +5 -5
  111. data/lib/axlsx/workbook/worksheet/data_bar.rb +29 -30
  112. data/lib/axlsx/workbook/worksheet/data_validation.rb +34 -33
  113. data/lib/axlsx/workbook/worksheet/data_validations.rb +5 -6
  114. data/lib/axlsx/workbook/worksheet/date_time_converter.rb +8 -8
  115. data/lib/axlsx/workbook/worksheet/dimension.rb +9 -6
  116. data/lib/axlsx/workbook/worksheet/header_footer.rb +4 -3
  117. data/lib/axlsx/workbook/worksheet/icon_set.rb +24 -8
  118. data/lib/axlsx/workbook/worksheet/merged_cells.rb +10 -10
  119. data/lib/axlsx/workbook/worksheet/outline_pr.rb +6 -3
  120. data/lib/axlsx/workbook/worksheet/page_margins.rb +17 -12
  121. data/lib/axlsx/workbook/worksheet/page_set_up_pr.rb +6 -4
  122. data/lib/axlsx/workbook/worksheet/page_setup.rb +128 -129
  123. data/lib/axlsx/workbook/worksheet/pane.rb +27 -26
  124. data/lib/axlsx/workbook/worksheet/pivot_table.rb +23 -25
  125. data/lib/axlsx/workbook/worksheet/pivot_table_cache_definition.rb +12 -13
  126. data/lib/axlsx/workbook/worksheet/pivot_tables.rb +5 -4
  127. data/lib/axlsx/workbook/worksheet/print_options.rb +3 -2
  128. data/lib/axlsx/workbook/worksheet/protected_range.rb +6 -5
  129. data/lib/axlsx/workbook/worksheet/protected_ranges.rb +12 -10
  130. data/lib/axlsx/workbook/worksheet/rich_text.rb +6 -6
  131. data/lib/axlsx/workbook/worksheet/rich_text_run.rb +35 -17
  132. data/lib/axlsx/workbook/worksheet/row.rb +30 -22
  133. data/lib/axlsx/workbook/worksheet/row_breaks.rb +8 -7
  134. data/lib/axlsx/workbook/worksheet/selection.rb +16 -16
  135. data/lib/axlsx/workbook/worksheet/sheet_calc_pr.rb +10 -7
  136. data/lib/axlsx/workbook/worksheet/sheet_data.rb +7 -7
  137. data/lib/axlsx/workbook/worksheet/sheet_format_pr.rb +23 -19
  138. data/lib/axlsx/workbook/worksheet/sheet_pr.rb +11 -7
  139. data/lib/axlsx/workbook/worksheet/sheet_protection.rb +21 -20
  140. data/lib/axlsx/workbook/worksheet/sheet_view.rb +48 -53
  141. data/lib/axlsx/workbook/worksheet/table.rb +13 -13
  142. data/lib/axlsx/workbook/worksheet/table_style_info.rb +6 -5
  143. data/lib/axlsx/workbook/worksheet/tables.rb +7 -5
  144. data/lib/axlsx/workbook/worksheet/worksheet.rb +92 -63
  145. data/lib/axlsx/workbook/worksheet/worksheet_comments.rb +10 -8
  146. data/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +11 -4
  147. data/lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb +9 -8
  148. data/lib/axlsx/workbook/worksheet/worksheet_hyperlinks.rb +7 -5
  149. data/lib/axlsx.rb +75 -47
  150. data/lib/caxlsx.rb +3 -2
  151. metadata +50 -44
@@ -1,8 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Axlsx
2
4
  # This module allows us to define a list of symbols defining which
3
5
  # attributes will be serialized for a class.
4
6
  module SerializedAttributes
5
-
6
7
  # Extend with class methods
7
8
  def self.included(base)
8
9
  base.send :extend, ClassMethods
@@ -10,16 +11,25 @@ module Axlsx
10
11
 
11
12
  # class methods applied to all includers
12
13
  module ClassMethods
13
-
14
- # This is the method to be used in inheriting classes to specify
14
+ # This is the method to be used in inheriting classes to specify
15
15
  # which of the instance values are serializable
16
16
  def serializable_attributes(*symbols)
17
17
  @xml_attributes = symbols
18
+ @camel_xml_attributes = nil
19
+ @ivar_xml_attributes = nil
18
20
  end
19
21
 
20
22
  # a reader for those attributes
21
23
  attr_reader :xml_attributes
22
24
 
25
+ def camel_xml_attributes
26
+ @camel_xml_attributes ||= @xml_attributes.map { |attr| Axlsx.camel(attr, false) }
27
+ end
28
+
29
+ def ivar_xml_attributes
30
+ @ivar_xml_attributes ||= @xml_attributes.map { |attr| :"@#{attr}" }
31
+ end
32
+
23
33
  # This helper registers the attributes that will be formatted as elements.
24
34
  def serializable_element_attributes(*symbols)
25
35
  @xml_element_attributes = symbols
@@ -31,39 +41,52 @@ module Axlsx
31
41
 
32
42
  # creates a XML tag with serialized attributes
33
43
  # @see SerializedAttributes#serialized_attributes
34
- def serialized_tag(tagname, str, additional_attributes = {}, &block)
35
- str << "<#{tagname} "
44
+ def serialized_tag(tagname, str, additional_attributes = {})
45
+ str << '<' << tagname << ' '
36
46
  serialized_attributes(str, additional_attributes)
37
47
  if block_given?
38
48
  str << '>'
39
49
  yield
40
- str << "</#{tagname}>"
50
+ str << '</' << tagname << '>'
41
51
  else
42
52
  str << '/>'
43
53
  end
44
54
  end
45
55
 
46
- # serializes the instance values of the defining object based on the
56
+ # serializes the instance values of the defining object based on the
47
57
  # list of serializable attributes.
48
58
  # @param [String] str The string instance to append this
49
59
  # serialization to.
50
60
  # @param [Hash] additional_attributes An option key value hash for
51
61
  # defining values that are not serializable attributes list.
52
- def serialized_attributes(str = '', additional_attributes = {})
53
- attributes = declared_attributes.merge! additional_attributes
54
- attributes.each do |key, value|
55
- str << "#{Axlsx.camel(key, false)}=\"#{Axlsx.camel(Axlsx.booleanize(value), false)}\" "
62
+ # @param [Boolean] camelize_value Should the attribute values be camelized
63
+ def serialized_attributes(str = +'', additional_attributes = {}, camelize_value = true)
64
+ camel_xml_attributes = self.class.camel_xml_attributes
65
+ ivar_xml_attributes = self.class.ivar_xml_attributes
66
+
67
+ self.class.xml_attributes.each_with_index do |attr, index|
68
+ next if additional_attributes.key?(attr)
69
+ next unless instance_variable_defined?(ivar_xml_attributes[index])
70
+
71
+ value = instance_variable_get(ivar_xml_attributes[index])
72
+ next if value.nil?
73
+
74
+ value = Axlsx.booleanize(value)
75
+ value = Axlsx.camel(value, false) if camelize_value
76
+
77
+ str << camel_xml_attributes[index] << '="' << value.to_s << '" '
56
78
  end
57
- str
58
- end
59
79
 
60
- # A hash of instance variables that have been declared with
61
- # seraialized_attributes and are not nil.
62
- # This requires ruby 1.9.3 or higher
63
- def declared_attributes
64
- Axlsx.instance_values_for(self).select do |key, value|
65
- value != nil && self.class.xml_attributes.include?(key.to_sym)
80
+ additional_attributes.each do |attr, value|
81
+ next if value.nil?
82
+
83
+ value = Axlsx.booleanize(value)
84
+ value = Axlsx.camel(value, false) if camelize_value
85
+
86
+ str << Axlsx.camel(attr, false) << '="' << value.to_s << '" '
66
87
  end
88
+
89
+ str
67
90
  end
68
91
 
69
92
  # serialized instance values at text nodes on a camelized element of the
@@ -73,15 +96,16 @@ module Axlsx
73
96
  # @param [String] str The string instance to which serialized data is appended
74
97
  # @param [Array] additional_attributes An array of additional attribute names.
75
98
  # @return [String] The serialized output.
76
- def serialized_element_attributes(str='', additional_attributes=[], &block)
99
+ def serialized_element_attributes(str = +'', additional_attributes = [])
77
100
  attrs = self.class.xml_element_attributes + additional_attributes
78
101
  values = Axlsx.instance_values_for(self)
79
102
  attrs.each do |attribute_name|
80
103
  value = values[attribute_name.to_s]
81
104
  next if value.nil?
105
+
82
106
  value = yield value if block_given?
83
107
  element_name = Axlsx.camel(attribute_name, false)
84
- str << "<#{element_name}>#{value}</#{element_name}>"
108
+ str << '<' << element_name << '>' << value << '</' << element_name << '>'
85
109
  end
86
110
  str
87
111
  end
@@ -1,23 +1,42 @@
1
- # encoding: UTF-8
2
- module Axlsx
1
+ # frozen_string_literal: true
3
2
 
3
+ module Axlsx
4
4
  # A SimpleTypedList is a type restrictive collection that allows some of the methods from Array and supports basic xml serialization.
5
5
  # @private
6
- class SimpleTypedList
6
+ class SimpleTypedList < Array
7
+ DESTRUCTIVE = [
8
+ 'replace', 'insert', 'collect!', 'map!', 'pop', 'delete_if',
9
+ 'reverse!', 'shift', 'shuffle!', 'slice!', 'sort!', 'uniq!',
10
+ 'unshift', 'zip', 'flatten!', 'fill', 'drop', 'drop_while',
11
+ 'clear'
12
+ ].freeze
13
+
14
+ DESTRUCTIVE.each do |name|
15
+ undef_method name
16
+ end
17
+
18
+ # We often call index(element) on instances of SimpleTypedList. Thus, we do not want to inherit Array
19
+ # implementation of == / eql? which walks the elements calling == / eql?. Instead we want the fast
20
+ # and original versions from BasicObject.
21
+ alias :== :equal?
22
+ alias :eql? :equal?
23
+
7
24
  # Creats a new typed list
8
25
  # @param [Array, Class] type An array of Class objects or a single Class object
9
26
  # @param [String] serialize_as The tag name to use in serialization
10
27
  # @raise [ArgumentError] if all members of type are not Class objects
11
- def initialize type, serialize_as=nil, start_size = 0
28
+ def initialize(type, serialize_as = nil, start_size = 0)
29
+ super(start_size)
30
+
12
31
  if type.is_a? Array
13
32
  type.each { |item| raise ArgumentError, "All members of type must be Class objects" unless item.is_a? Class }
14
33
  @allowed_types = type
15
34
  else
16
35
  raise ArgumentError, "Type must be a Class object or array of Class objects" unless type.is_a? Class
36
+
17
37
  @allowed_types = [type]
18
38
  end
19
39
  @serialize_as = serialize_as unless serialize_as.nil?
20
- @list = Array.new(start_size)
21
40
  end
22
41
 
23
42
  # The class constants of allowed types
@@ -38,15 +57,16 @@ module Axlsx
38
57
  # Transposes the list (without blowing up like ruby does)
39
58
  # any non populated cell in the matrix will be a nil value
40
59
  def transpose
41
- return @list.clone if @list.size == 0
42
- row_count = @list.size
43
- max_column_count = @list.map{|row| row.cells.size}.max
60
+ return clone if size.zero?
61
+
62
+ row_count = size
63
+ max_column_count = map { |row| row.cells.size }.max
44
64
  result = Array.new(max_column_count) { Array.new(row_count) }
45
65
  # yes, I know it is silly, but that warning is really annoying
46
66
  row_count.times do |row_index|
47
- max_column_count.times do |column_index|
48
- datum = if @list[row_index].cells.size >= max_column_count
49
- @list[row_index].cells[column_index]
67
+ max_column_count.times do |column_index|
68
+ datum = if self[row_index].cells.size >= max_column_count
69
+ self[row_index].cells[column_index]
50
70
  elsif block_given?
51
71
  yield(column_index, row_index)
52
72
  end
@@ -55,11 +75,11 @@ module Axlsx
55
75
  end
56
76
  result
57
77
  end
58
-
78
+
59
79
  # Lock this list at the current size
60
80
  # @return [self]
61
81
  def lock
62
- @locked_at = @list.size
82
+ @locked_at = size
63
83
  self
64
84
  end
65
85
 
@@ -69,23 +89,17 @@ module Axlsx
69
89
  @locked_at = nil
70
90
  self
71
91
  end
72
-
73
- def to_ary
74
- @list
75
- end
76
-
77
- alias :to_a :to_ary
78
92
 
79
93
  # join operator
80
- # @param [Array] v the array to join
94
+ # @param [Array] other the array to join
81
95
  # @raise [ArgumentError] if any of the values being joined are not
82
96
  # one of the allowed types
83
97
  # @return [SimpleTypedList]
84
- def +(v)
85
- v.each do |item|
86
- DataTypeValidator.validate :SimpleTypedList_plus, @allowed_types, item
87
- @list << item
98
+ def +(other)
99
+ other.each do |item|
100
+ self << item
88
101
  end
102
+ super
89
103
  end
90
104
 
91
105
  # Concat operator
@@ -94,12 +108,11 @@ module Axlsx
94
108
  # @return [Integer] returns the index of the item added.
95
109
  def <<(v)
96
110
  DataTypeValidator.validate :SimpleTypedList_push, @allowed_types, v
97
- @list << v
98
- @list.size - 1
99
- end
100
-
111
+ super
112
+ size - 1
113
+ end
114
+
101
115
  alias :push :<<
102
-
103
116
 
104
117
  # delete the item from the list
105
118
  # @param [Any] v The item to be deleted.
@@ -108,16 +121,17 @@ module Axlsx
108
121
  def delete(v)
109
122
  return unless include? v
110
123
  raise ArgumentError, "Item is protected and cannot be deleted" if protected? index(v)
111
- @list.delete v
124
+
125
+ super
112
126
  end
113
127
 
114
128
  # delete the item from the list at the index position provided
115
129
  # @raise [ArgumentError] if the index is protected by locking
116
130
  # @return [Any] The item deleted
117
131
  def delete_at(index)
118
- @list[index]
119
132
  raise ArgumentError, "Item is protected and cannot be deleted" if protected? index
120
- @list.delete_at index
133
+
134
+ super
121
135
  end
122
136
 
123
137
  # positional assignment. Adds the item at the index specified
@@ -125,11 +139,12 @@ module Axlsx
125
139
  # @param [Any] v
126
140
  # @raise [ArgumentError] if the index is protected by locking
127
141
  # @raise [ArgumentError] if the item is not one of the allowed types
142
+ # @return [Any] The item added
128
143
  def []=(index, v)
129
144
  DataTypeValidator.validate :SimpleTypedList_insert, @allowed_types, v
130
145
  raise ArgumentError, "Item is protected and cannot be changed" if protected? index
131
- @list[index] = v
132
- v
146
+
147
+ super
133
148
  end
134
149
 
135
150
  # inserts an item at the index specfied
@@ -137,43 +152,29 @@ module Axlsx
137
152
  # @param [Any] v
138
153
  # @raise [ArgumentError] if the index is protected by locking
139
154
  # @raise [ArgumentError] if the index is not one of the allowed types
155
+ # @return [Any] The item inserted
140
156
  def insert(index, v)
141
157
  DataTypeValidator.validate :SimpleTypedList_insert, @allowed_types, v
142
158
  raise ArgumentError, "Item is protected and cannot be changed" if protected? index
143
- @list.insert(index, v)
159
+
160
+ super
144
161
  v
145
162
  end
146
163
 
147
164
  # determines if the index is protected
148
165
  # @param [Integer] index
149
- def protected? index
166
+ def protected?(index)
150
167
  return false unless locked_at.is_a? Integer
168
+
151
169
  index < locked_at
152
170
  end
153
171
 
154
- DESTRUCTIVE = ['replace', 'insert', 'collect!', 'map!', 'pop', 'delete_if',
155
- 'reverse!', 'shift', 'shuffle!', 'slice!', 'sort!', 'uniq!',
156
- 'unshift', 'zip', 'flatten!', 'fill', 'drop', 'drop_while',
157
- 'delete_if', 'clear']
158
- DELEGATES = Array.instance_methods - self.instance_methods - DESTRUCTIVE
159
-
160
- DELEGATES.each do |method|
161
- class_eval %{
162
- def #{method}(*args, &block)
163
- @list.send(:#{method}, *args, &block)
164
- end
165
- }
166
- end
167
-
168
- def to_xml_string(str = '')
172
+ def to_xml_string(str = +'')
169
173
  classname = @allowed_types[0].name.split('::').last
170
- el_name = serialize_as.to_s || (classname[0,1].downcase + classname[1..-1])
171
- str << ('<' << el_name << ' count="' << size.to_s << '">')
174
+ el_name = serialize_as.to_s || (classname[0, 1].downcase + classname[1..])
175
+ str << '<' << el_name << ' count="' << size.to_s << '">'
172
176
  each { |item| item.to_xml_string(str) }
173
- str << ('</' << el_name << '>')
177
+ str << '</' << el_name << '>'
174
178
  end
175
-
176
179
  end
177
-
178
-
179
180
  end
@@ -1,61 +1,60 @@
1
- # encoding: UTF-8
2
- module Axlsx
1
+ # frozen_string_literal: true
3
2
 
3
+ module Axlsx
4
4
  # The Storage class represents a storage object or stream in a compound file.
5
5
  class Storage
6
-
7
6
  # Packing for the Storage when pushing an array of items into a byte stream
8
7
  # Name, name length, type, color, left sibling, right sibling, child, classid, state, created, modified, sector, size
9
- PACKING = "s32 s1 c2 l3 x16 x4 q2 l q".freeze
8
+ PACKING = "s32 s1 c2 l3 x16 x4 q2 l q"
10
9
 
11
10
  # storage types
12
11
  TYPES = {
13
- :root=>5,
14
- :stream=>2,
15
- :storage=>1
12
+ root: 5,
13
+ stream: 2,
14
+ storage: 1
16
15
  }.freeze
17
16
 
18
17
  # Creates a byte string for this storage
19
- # @return [String]
18
+ # @return [String]
20
19
  def to_s
21
- data = [@name.concat(Array.new(32-@name.size, 0)),
22
- @name_size,
23
- @type,
24
- @color,
25
- @left,
26
- @right,
27
- @child,
28
- @created,
29
- @modified,
30
- @sector,
31
- @size].flatten
20
+ data = [@name.concat(Array.new(32 - @name.size, 0)),
21
+ @name_size,
22
+ @type,
23
+ @color,
24
+ @left,
25
+ @right,
26
+ @child,
27
+ @created,
28
+ @modified,
29
+ @sector,
30
+ @size].flatten
32
31
  data.pack(PACKING)
33
32
  end
34
33
 
35
34
  # storage colors
36
35
  COLORS = {
37
- :red=>0,
38
- :black=>1
39
- }
36
+ red: 0,
37
+ black: 1
38
+ }.freeze
40
39
 
41
40
  # The color of this node in the directory tree. Defaults to black if not specified
42
41
  # @return [Integer] color
43
42
  attr_reader :color
44
-
43
+
45
44
  # Sets the color for this storage
46
45
  # @param [Integer] v Must be one of the COLORS constant hash values
47
46
  def color=(v)
48
- RestrictionValidator.validate :storage_color, COLORS.values, v
47
+ RestrictionValidator.validate :storage_color, COLORS.values, v
49
48
  @color = v
50
49
  end
51
50
 
52
51
  # The size of the name for this node.
53
- # interesting to see that office actually uses 'R' for the root directory and lists the size as 2 bytes - thus is it *NOT* null
52
+ # interesting to see that office actually uses 'R' for the root directory and lists the size as 2 bytes - thus is it *NOT* null
54
53
  # terminated. I am making this r/w so that I can override the size
55
54
  # @return [Integer] color
56
55
  attr_reader :name_size
57
56
 
58
- # the name of the stream
57
+ # the name of the stream
59
58
  attr_reader :name
60
59
 
61
60
  # sets the name of the stream.
@@ -64,7 +63,6 @@ module Axlsx
64
63
  def name=(v)
65
64
  @name = v.bytes.to_a << 0
66
65
  @name_size = @name.size * 2
67
- @name
68
66
  end
69
67
 
70
68
  # The size of the stream
@@ -77,7 +75,7 @@ module Axlsx
77
75
  # @param [String] v The data for this storages stream
78
76
  # @return [Array]
79
77
  def data=(v)
80
- Axlsx::validate_string(v)
78
+ Axlsx.validate_string(v)
81
79
  self.type = TYPES[:stream] unless @type
82
80
  @size = v.size
83
81
  @data = v.bytes.to_a
@@ -87,16 +85,16 @@ module Axlsx
87
85
  # @return [Integer] sector
88
86
  attr_accessor :sector
89
87
 
90
- # The 0 based index in the directoies chain for this the left sibling of this storage.
91
-
88
+ # The 0 based index in the directoies chain for this the left sibling of this storage.
89
+
92
90
  # @return [Integer] left
93
- attr_accessor :left
91
+ attr_accessor :left
94
92
 
95
- # The 0 based index in the directoies chain for this the right sibling of this storage.
93
+ # The 0 based index in the directoies chain for this the right sibling of this storage.
96
94
  # @return [Integer] right
97
- attr_accessor :right
95
+ attr_accessor :right
98
96
 
99
- # The 0 based index in the directoies chain for the child of this storage.
97
+ # The 0 based index in the directoies chain for the child of this storage.
100
98
  # @return [Integer] child
101
99
  attr_accessor :child
102
100
 
@@ -113,14 +111,14 @@ module Axlsx
113
111
  # @return [Integer] type
114
112
  attr_reader :type
115
113
 
116
- # Sets the type for this storage.
117
- # @param [Integer] v the type to specify must be one of the TYPES constant hash values.
114
+ # Sets the type for this storage.
115
+ # @param [Integer] v the type to specify must be one of the TYPES constant hash values.
118
116
  def type=(v)
119
- RestrictionValidator.validate :storage_type, TYPES.values, v
117
+ RestrictionValidator.validate :storage_type, TYPES.values, v
120
118
  @type = v
121
119
  end
122
120
 
123
- # Creates a new storage object.
121
+ # Creates a new storage object.
124
122
  # @param [String] name the name of the storage
125
123
  # @option options [Integer] color (black)
126
124
  # @option options [Integer] type (storage)
@@ -131,16 +129,15 @@ module Axlsx
131
129
  # @option options [Integer] created (0)
132
130
  # @option options [Integer] modified (0)
133
131
  # @option options [Integer] sector (0)
134
- def initialize(name, options= {})
132
+ def initialize(name, options = {})
135
133
  @left = @right = @child = -1
136
134
  @sector = @size = @created = @modified = 0
137
135
  options.each do |o|
138
- self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
136
+ send("#{o[0]}=", o[1]) if respond_to? "#{o[0]}="
139
137
  end
140
138
  @color ||= COLORS[:black]
141
139
  @type ||= (data.nil? ? TYPES[:storage] : TYPES[:stream])
142
140
  self.name = name
143
141
  end
144
-
145
142
  end
146
143
  end