caxlsx 3.3.0 → 4.5.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 (155) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +87 -11
  3. data/README.md +43 -16
  4. data/Rakefile +5 -11
  5. data/examples/generate.rb +3 -1
  6. data/lib/axlsx/content_type/abstract_content_type.rb +15 -10
  7. data/lib/axlsx/content_type/content_type.rb +8 -10
  8. data/lib/axlsx/content_type/default.rb +7 -6
  9. data/lib/axlsx/content_type/override.rb +6 -5
  10. data/lib/axlsx/doc_props/app.rb +96 -32
  11. data/lib/axlsx/doc_props/core.rb +9 -12
  12. data/lib/axlsx/drawing/area_chart.rb +16 -17
  13. data/lib/axlsx/drawing/area_series.rb +23 -18
  14. data/lib/axlsx/drawing/ax_data_source.rb +4 -7
  15. data/lib/axlsx/drawing/axes.rb +11 -10
  16. data/lib/axlsx/drawing/axis.rb +83 -36
  17. data/lib/axlsx/drawing/bar_3D_chart.rb +30 -21
  18. data/lib/axlsx/drawing/bar_chart.rb +29 -20
  19. data/lib/axlsx/drawing/bar_series.rb +23 -18
  20. data/lib/axlsx/drawing/bubble_chart.rb +9 -10
  21. data/lib/axlsx/drawing/bubble_series.rb +11 -12
  22. data/lib/axlsx/drawing/cat_axis.rb +38 -28
  23. data/lib/axlsx/drawing/chart.rb +52 -30
  24. data/lib/axlsx/drawing/d_lbls.rb +36 -32
  25. data/lib/axlsx/drawing/drawing.rb +61 -62
  26. data/lib/axlsx/drawing/graphic_frame.rb +6 -7
  27. data/lib/axlsx/drawing/hyperlink.rb +22 -14
  28. data/lib/axlsx/drawing/line_3D_chart.rb +15 -17
  29. data/lib/axlsx/drawing/line_chart.rb +16 -17
  30. data/lib/axlsx/drawing/line_series.rb +23 -18
  31. data/lib/axlsx/drawing/marker.rb +29 -16
  32. data/lib/axlsx/drawing/num_data.rb +13 -16
  33. data/lib/axlsx/drawing/num_data_source.rb +11 -13
  34. data/lib/axlsx/drawing/num_val.rb +10 -11
  35. data/lib/axlsx/drawing/one_cell_anchor.rb +16 -10
  36. data/lib/axlsx/drawing/pic.rb +68 -24
  37. data/lib/axlsx/drawing/picture_locking.rb +6 -7
  38. data/lib/axlsx/drawing/pie_3D_chart.rb +9 -12
  39. data/lib/axlsx/drawing/pie_chart.rb +36 -0
  40. data/lib/axlsx/drawing/pie_series.rb +26 -17
  41. data/lib/axlsx/drawing/scaling.rb +26 -13
  42. data/lib/axlsx/drawing/scatter_chart.rb +10 -11
  43. data/lib/axlsx/drawing/scatter_series.rb +17 -18
  44. data/lib/axlsx/drawing/ser_axis.rb +22 -19
  45. data/lib/axlsx/drawing/series.rb +14 -8
  46. data/lib/axlsx/drawing/series_title.rb +6 -6
  47. data/lib/axlsx/drawing/str_data.rb +10 -13
  48. data/lib/axlsx/drawing/str_val.rb +8 -9
  49. data/lib/axlsx/drawing/title.rb +23 -27
  50. data/lib/axlsx/drawing/two_cell_anchor.rb +9 -9
  51. data/lib/axlsx/drawing/val_axis.rb +7 -8
  52. data/lib/axlsx/drawing/view_3D.rb +38 -30
  53. data/lib/axlsx/drawing/vml_drawing.rb +19 -20
  54. data/lib/axlsx/drawing/vml_shape.rb +25 -26
  55. data/lib/axlsx/package.rb +134 -99
  56. data/lib/axlsx/rels/relationship.rb +42 -30
  57. data/lib/axlsx/rels/relationships.rb +9 -10
  58. data/lib/axlsx/stylesheet/border.rb +16 -8
  59. data/lib/axlsx/stylesheet/border_pr.rb +24 -12
  60. data/lib/axlsx/stylesheet/cell_alignment.rb +44 -21
  61. data/lib/axlsx/stylesheet/cell_protection.rb +13 -7
  62. data/lib/axlsx/stylesheet/cell_style.rb +36 -15
  63. data/lib/axlsx/stylesheet/color.rb +23 -14
  64. data/lib/axlsx/stylesheet/dxf.rb +36 -15
  65. data/lib/axlsx/stylesheet/fill.rb +7 -6
  66. data/lib/axlsx/stylesheet/font.rb +71 -22
  67. data/lib/axlsx/stylesheet/gradient_fill.rb +16 -10
  68. data/lib/axlsx/stylesheet/gradient_stop.rb +14 -6
  69. data/lib/axlsx/stylesheet/num_fmt.rb +15 -14
  70. data/lib/axlsx/stylesheet/pattern_fill.rb +20 -8
  71. data/lib/axlsx/stylesheet/styles.rb +167 -118
  72. data/lib/axlsx/stylesheet/table_style.rb +20 -10
  73. data/lib/axlsx/stylesheet/table_style_element.rb +16 -8
  74. data/lib/axlsx/stylesheet/table_styles.rb +16 -11
  75. data/lib/axlsx/stylesheet/theme.rb +163 -0
  76. data/lib/axlsx/stylesheet/xf.rb +78 -25
  77. data/lib/axlsx/util/accessors.rb +13 -11
  78. data/lib/axlsx/util/buffered_zip_output_stream.rb +64 -0
  79. data/lib/axlsx/util/constants.rb +133 -110
  80. data/lib/axlsx/util/mime_type_utils.rb +75 -5
  81. data/lib/axlsx/util/options_parser.rb +4 -3
  82. data/lib/axlsx/util/serialized_attributes.rb +47 -23
  83. data/lib/axlsx/util/simple_typed_list.rb +77 -64
  84. data/lib/axlsx/util/storage.rb +38 -41
  85. data/lib/axlsx/util/uri_utils.rb +70 -0
  86. data/lib/axlsx/util/validators.rb +111 -48
  87. data/lib/axlsx/util/zip_command.rb +10 -12
  88. data/lib/axlsx/version.rb +3 -2
  89. data/lib/axlsx/workbook/defined_name.rb +13 -9
  90. data/lib/axlsx/workbook/defined_names.rb +5 -4
  91. data/lib/axlsx/workbook/shared_strings_table.rb +13 -14
  92. data/lib/axlsx/workbook/workbook.rb +152 -120
  93. data/lib/axlsx/workbook/workbook_view.rb +9 -12
  94. data/lib/axlsx/workbook/workbook_views.rb +5 -5
  95. data/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb +87 -15
  96. data/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb +16 -10
  97. data/lib/axlsx/workbook/worksheet/auto_filter/filters.rb +28 -25
  98. data/lib/axlsx/workbook/worksheet/auto_filter/sort_condition.rb +51 -0
  99. data/lib/axlsx/workbook/worksheet/auto_filter/sort_state.rb +54 -0
  100. data/lib/axlsx/workbook/worksheet/border_creator.rb +34 -29
  101. data/lib/axlsx/workbook/worksheet/break.rb +4 -5
  102. data/lib/axlsx/workbook/worksheet/cell.rb +125 -78
  103. data/lib/axlsx/workbook/worksheet/cell_serializer.rb +33 -29
  104. data/lib/axlsx/workbook/worksheet/cfvo.rb +13 -5
  105. data/lib/axlsx/workbook/worksheet/cfvos.rb +5 -5
  106. data/lib/axlsx/workbook/worksheet/col.rb +32 -19
  107. data/lib/axlsx/workbook/worksheet/col_breaks.rb +9 -8
  108. data/lib/axlsx/workbook/worksheet/color_scale.rb +16 -16
  109. data/lib/axlsx/workbook/worksheet/cols.rb +10 -8
  110. data/lib/axlsx/workbook/worksheet/comment.rb +14 -13
  111. data/lib/axlsx/workbook/worksheet/comments.rb +11 -13
  112. data/lib/axlsx/workbook/worksheet/conditional_formatting.rb +19 -9
  113. data/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb +75 -22
  114. data/lib/axlsx/workbook/worksheet/conditional_formattings.rb +8 -8
  115. data/lib/axlsx/workbook/worksheet/data_bar.rb +29 -30
  116. data/lib/axlsx/workbook/worksheet/data_validation.rb +73 -33
  117. data/lib/axlsx/workbook/worksheet/data_validations.rb +8 -9
  118. data/lib/axlsx/workbook/worksheet/date_time_converter.rb +8 -8
  119. data/lib/axlsx/workbook/worksheet/dimension.rb +10 -7
  120. data/lib/axlsx/workbook/worksheet/header_footer.rb +5 -4
  121. data/lib/axlsx/workbook/worksheet/icon_set.rb +40 -12
  122. data/lib/axlsx/workbook/worksheet/merged_cells.rb +11 -11
  123. data/lib/axlsx/workbook/worksheet/outline_pr.rb +7 -4
  124. data/lib/axlsx/workbook/worksheet/page_margins.rb +41 -13
  125. data/lib/axlsx/workbook/worksheet/page_set_up_pr.rb +7 -6
  126. data/lib/axlsx/workbook/worksheet/page_setup.rb +153 -131
  127. data/lib/axlsx/workbook/worksheet/pane.rb +34 -26
  128. data/lib/axlsx/workbook/worksheet/pivot_table.rb +78 -29
  129. data/lib/axlsx/workbook/worksheet/pivot_table_cache_definition.rb +12 -13
  130. data/lib/axlsx/workbook/worksheet/pivot_tables.rb +6 -5
  131. data/lib/axlsx/workbook/worksheet/print_options.rb +4 -2
  132. data/lib/axlsx/workbook/worksheet/protected_range.rb +6 -5
  133. data/lib/axlsx/workbook/worksheet/protected_ranges.rb +13 -11
  134. data/lib/axlsx/workbook/worksheet/rich_text.rb +6 -6
  135. data/lib/axlsx/workbook/worksheet/rich_text_run.rb +66 -28
  136. data/lib/axlsx/workbook/worksheet/row.rb +34 -23
  137. data/lib/axlsx/workbook/worksheet/row_breaks.rb +9 -8
  138. data/lib/axlsx/workbook/worksheet/selection.rb +22 -16
  139. data/lib/axlsx/workbook/worksheet/sheet_calc_pr.rb +11 -7
  140. data/lib/axlsx/workbook/worksheet/sheet_data.rb +7 -7
  141. data/lib/axlsx/workbook/worksheet/sheet_format_pr.rb +23 -19
  142. data/lib/axlsx/workbook/worksheet/sheet_pr.rb +12 -8
  143. data/lib/axlsx/workbook/worksheet/sheet_protection.rb +22 -21
  144. data/lib/axlsx/workbook/worksheet/sheet_view.rb +71 -55
  145. data/lib/axlsx/workbook/worksheet/table.rb +13 -13
  146. data/lib/axlsx/workbook/worksheet/table_style_info.rb +8 -6
  147. data/lib/axlsx/workbook/worksheet/tables.rb +8 -6
  148. data/lib/axlsx/workbook/worksheet/worksheet.rb +120 -79
  149. data/lib/axlsx/workbook/worksheet/worksheet_comments.rb +13 -11
  150. data/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +11 -4
  151. data/lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb +12 -10
  152. data/lib/axlsx/workbook/worksheet/worksheet_hyperlinks.rb +9 -7
  153. data/lib/axlsx.rb +120 -60
  154. data/lib/caxlsx.rb +3 -2
  155. metadata +42 -75
@@ -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,57 +41,71 @@ 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
70
93
  # attribute name. You may pass in a block for evaluation against non nil
71
- # values. We use an array for element attributes becuase misordering will
72
- # break the xml and 1.8.7 does not support ordered hashes.
94
+ # values. We use an array for element attributes because misordering will
95
+ # break the xml.
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
7
- # Creats a new typed list
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
+
24
+ # Creates 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 == 0
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,37 +89,43 @@ module Axlsx
69
89
  @locked_at = nil
70
90
  self
71
91
  end
72
-
73
- def to_ary
74
- @list
75
- end
76
92
 
77
- alias :to_a :to_ary
78
-
79
- # join operator
80
- # @param [Array] v the array to join
93
+ # Appends the elements of +others+ to self.
94
+ # @param [Array<Array>] others one or more arrays 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 concat(*others)
99
+ others.each do |other|
100
+ other.each do |item|
101
+ self << item
102
+ end
88
103
  end
104
+ self
89
105
  end
90
106
 
91
- # Concat operator
107
+ # Pushes the given object on to the end of this array and returns the index
108
+ # of the item added.
92
109
  # @param [Any] v the data to be added
93
- # @raise [ArgumentError] if the value being added is not one fo the allowed types
110
+ # @raise [ArgumentError] if the value being added is not one of the allowed types
94
111
  # @return [Integer] returns the index of the item added.
95
112
  def <<(v)
96
113
  DataTypeValidator.validate :SimpleTypedList_push, @allowed_types, v
97
- @list << v
98
- @list.size - 1
99
- end
100
-
101
- alias :push :<<
102
-
114
+ super
115
+ size - 1
116
+ end
117
+
118
+ # Pushes the given object(s) on to the end of this array. This expression
119
+ # returns the array itself, so several appends may be chained together.
120
+ # @param [Any] values the data to be added
121
+ # @raise [ArgumentError] if any of the values being joined are not
122
+ # @return [SimpleTypedList]
123
+ def push(*values)
124
+ values.each do |value|
125
+ self << value
126
+ end
127
+ self
128
+ end
103
129
 
104
130
  # delete the item from the list
105
131
  # @param [Any] v The item to be deleted.
@@ -108,16 +134,17 @@ module Axlsx
108
134
  def delete(v)
109
135
  return unless include? v
110
136
  raise ArgumentError, "Item is protected and cannot be deleted" if protected? index(v)
111
- @list.delete v
137
+
138
+ super
112
139
  end
113
140
 
114
141
  # delete the item from the list at the index position provided
115
142
  # @raise [ArgumentError] if the index is protected by locking
116
143
  # @return [Any] The item deleted
117
144
  def delete_at(index)
118
- @list[index]
119
145
  raise ArgumentError, "Item is protected and cannot be deleted" if protected? index
120
- @list.delete_at index
146
+
147
+ super
121
148
  end
122
149
 
123
150
  # positional assignment. Adds the item at the index specified
@@ -125,55 +152,41 @@ module Axlsx
125
152
  # @param [Any] v
126
153
  # @raise [ArgumentError] if the index is protected by locking
127
154
  # @raise [ArgumentError] if the item is not one of the allowed types
155
+ # @return [Any] The item added
128
156
  def []=(index, v)
129
157
  DataTypeValidator.validate :SimpleTypedList_insert, @allowed_types, v
130
158
  raise ArgumentError, "Item is protected and cannot be changed" if protected? index
131
- @list[index] = v
132
- v
159
+
160
+ super
133
161
  end
134
162
 
135
- # inserts an item at the index specfied
163
+ # inserts an item at the index specified
136
164
  # @param [Integer] index
137
165
  # @param [Any] v
138
166
  # @raise [ArgumentError] if the index is protected by locking
139
167
  # @raise [ArgumentError] if the index is not one of the allowed types
168
+ # @return [Any] The item inserted
140
169
  def insert(index, v)
141
170
  DataTypeValidator.validate :SimpleTypedList_insert, @allowed_types, v
142
171
  raise ArgumentError, "Item is protected and cannot be changed" if protected? index
143
- @list.insert(index, v)
172
+
173
+ super
144
174
  v
145
175
  end
146
176
 
147
177
  # determines if the index is protected
148
178
  # @param [Integer] index
149
- def protected? index
179
+ def protected?(index)
150
180
  return false unless locked_at.is_a? Integer
181
+
151
182
  index < locked_at
152
183
  end
153
184
 
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 = '')
169
- 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 << '">')
185
+ def to_xml_string(str = +'')
186
+ el_name = serialize_as.to_s
187
+ str << '<' << el_name << ' count="' << size.to_s << '">'
172
188
  each { |item| item.to_xml_string(str) }
173
- str << ('</' << el_name << '>')
189
+ str << '</' << el_name << '>'
174
190
  end
175
-
176
191
  end
177
-
178
-
179
192
  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 directories 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 directories 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 directories 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
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+ require 'uri'
5
+
6
+ module Axlsx
7
+ # This module defines some utils related to mime type detection
8
+ module UriUtils
9
+ class << self
10
+ # Performs an HTTP GeT request fetching only headers
11
+ def fetch_headers(uri, redirect_limit = 5)
12
+ redirect_count = 0
13
+ use_get = false
14
+
15
+ while redirect_count < redirect_limit
16
+ case (response = fetch_headers_request(uri, use_get: use_get))
17
+ when Net::HTTPSuccess
18
+ return response
19
+ when Net::HTTPMethodNotAllowed, Net::HTTPNotImplemented
20
+ fail_response(response) if use_get
21
+ use_get = true
22
+ next # Retry current URL with GET
23
+ when Net::HTTPRedirection
24
+ uri = follow_redirect(uri, response)
25
+ redirect_count += 1
26
+ else
27
+ fail_response(response)
28
+ end
29
+ end
30
+
31
+ raise ArgumentError, "Too many redirects (exceeded #{redirect_limit})"
32
+ end
33
+
34
+ private
35
+
36
+ def fetch_headers_request(uri, use_get: false)
37
+ Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
38
+ path = build_path(uri)
39
+ if use_get
40
+ http.request_get(path) { |response| break(response) } # Exit early with just headers
41
+ else
42
+ http.head(path)
43
+ end
44
+ end
45
+ end
46
+
47
+ def build_path(uri)
48
+ "#{uri.path.empty? ? '/' : uri.path}#{"?#{uri.query}" if uri.query}"
49
+ end
50
+
51
+ def follow_redirect(original_uri, response)
52
+ location = response['location']
53
+
54
+ if location.nil? || location.empty?
55
+ raise ArgumentError, "Redirect response missing Location header"
56
+ end
57
+
58
+ if location.start_with?('http://', 'https://')
59
+ URI.parse(location)
60
+ else
61
+ URI.join(original_uri, location)
62
+ end
63
+ end
64
+
65
+ def fail_response(response)
66
+ raise ArgumentError, "Failed to fetch resource: #{response.code} #{response.message}"
67
+ end
68
+ end
69
+ end
70
+ end