diff options
| author | Paul Kmiec <[email protected]> | 2023-05-10 21:34:19 -0700 |
|---|---|---|
| committer | Paul Kmiec <[email protected]> | 2023-05-10 21:34:19 -0700 |
| commit | 1d94514b6562131371130449eaccf711aa60dc95 (patch) | |
| tree | 7b8a034297d3d3a3a552e08372520651c3fa040a /lib | |
| parent | 67d8a1a781e53761a483575cc5abc7e9a992d2ca (diff) | |
| download | caxlsx-1d94514b6562131371130449eaccf711aa60dc95.tar.gz caxlsx-1d94514b6562131371130449eaccf711aa60dc95.zip | |
Derive SimpleTypedList from Array
Currently the SimpleTypedList is wrapping `@list` Array and re-implementing
certain methods to add type / value checking and delegating all the
non-destrutive methods to the internal @list.
Most everything that ends up being serialized to the excel file is derived
from SimpleTypedList (for example, a Row is a SimpleTypedList of Cells). This
means that many of the delegated methods are hit over and over again especially
`[]`. This extra delegation really adds up.
We can avoid the delegation by having SimpleTypedList extend Array and still
remove any un-safe methods.
It is possible to re-introduce an unsafe method in a subclass of SimpleTypedList
if needed. Here is one way,
```ruby
class MyList < SimpleTypedList
def clear
@clear_method ||= Array.instance_method(:clear).bind(self)
@clear_method.call
end
end
```
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/axlsx/util/simple_typed_list.rb | 64 |
1 files changed, 31 insertions, 33 deletions
diff --git a/lib/axlsx/util/simple_typed_list.rb b/lib/axlsx/util/simple_typed_list.rb index 60da0372..0798ee22 100644 --- a/lib/axlsx/util/simple_typed_list.rb +++ b/lib/axlsx/util/simple_typed_list.rb @@ -3,12 +3,25 @@ module Axlsx # A SimpleTypedList is a type restrictive collection that allows some of the methods from Array and supports basic xml serialization. # @private - class SimpleTypedList + class SimpleTypedList < Array + DESTRUCTIVE = [ + 'replace', 'insert', 'collect!', 'map!', 'pop', 'delete_if', + 'reverse!', 'shift', 'shuffle!', 'slice!', 'sort!', 'uniq!', + 'unshift', 'zip', 'flatten!', 'fill', 'drop', 'drop_while', + 'clear' + ].freeze + + DESTRUCTIVE.each do |name| + undef_method name + end + # Creats a new typed list # @param [Array, Class] type An array of Class objects or a single Class object # @param [String] serialize_as The tag name to use in serialization # @raise [ArgumentError] if all members of type are not Class objects def initialize(type, serialize_as = nil, start_size = 0) + super(start_size) + if type.is_a? Array type.each { |item| raise ArgumentError, "All members of type must be Class objects" unless item.is_a? Class } @allowed_types = type @@ -18,7 +31,6 @@ module Axlsx @allowed_types = [type] end @serialize_as = serialize_as unless serialize_as.nil? - @list = Array.new(start_size) end # The class constants of allowed types @@ -39,16 +51,16 @@ module Axlsx # Transposes the list (without blowing up like ruby does) # any non populated cell in the matrix will be a nil value def transpose - return @list.clone if @list.size == 0 + return clone if size.zero? - row_count = @list.size - max_column_count = @list.map { |row| row.cells.size }.max + row_count = size + max_column_count = map { |row| row.cells.size }.max result = Array.new(max_column_count) { Array.new(row_count) } # yes, I know it is silly, but that warning is really annoying row_count.times do |row_index| max_column_count.times do |column_index| - datum = if @list[row_index].cells.size >= max_column_count - @list[row_index].cells[column_index] + datum = if self[row_index].cells.size >= max_column_count + self[row_index].cells[column_index] elsif block_given? yield(column_index, row_index) end @@ -61,7 +73,7 @@ module Axlsx # Lock this list at the current size # @return [self] def lock - @locked_at = @list.size + @locked_at = size self end @@ -85,9 +97,9 @@ module Axlsx # @return [SimpleTypedList] def +(v) v.each do |item| - DataTypeValidator.validate :SimpleTypedList_plus, @allowed_types, item - @list << item + self << item end + super end # Concat operator @@ -96,8 +108,8 @@ module Axlsx # @return [Integer] returns the index of the item added. def <<(v) DataTypeValidator.validate :SimpleTypedList_push, @allowed_types, v - @list << v - @list.size - 1 + super + size - 1 end alias :push :<< @@ -110,17 +122,16 @@ module Axlsx return unless include? v raise ArgumentError, "Item is protected and cannot be deleted" if protected? index(v) - @list.delete v + super end # delete the item from the list at the index position provided # @raise [ArgumentError] if the index is protected by locking # @return [Any] The item deleted def delete_at(index) - @list[index] raise ArgumentError, "Item is protected and cannot be deleted" if protected? index - @list.delete_at index + super end # positional assignment. Adds the item at the index specified @@ -128,12 +139,12 @@ module Axlsx # @param [Any] v # @raise [ArgumentError] if the index is protected by locking # @raise [ArgumentError] if the item is not one of the allowed types + # @return [Any] The item added def []=(index, v) DataTypeValidator.validate :SimpleTypedList_insert, @allowed_types, v raise ArgumentError, "Item is protected and cannot be changed" if protected? index - @list[index] = v - v + super end # inserts an item at the index specfied @@ -141,11 +152,12 @@ module Axlsx # @param [Any] v # @raise [ArgumentError] if the index is protected by locking # @raise [ArgumentError] if the index is not one of the allowed types + # @return [Any] The item inserted def insert(index, v) DataTypeValidator.validate :SimpleTypedList_insert, @allowed_types, v raise ArgumentError, "Item is protected and cannot be changed" if protected? index - @list.insert(index, v) + super v end @@ -157,23 +169,9 @@ module Axlsx index < locked_at end - DESTRUCTIVE = ['replace', 'insert', 'collect!', 'map!', 'pop', 'delete_if', - 'reverse!', 'shift', 'shuffle!', 'slice!', 'sort!', 'uniq!', - 'unshift', 'zip', 'flatten!', 'fill', 'drop', 'drop_while', - 'delete_if', 'clear'] - DELEGATES = Array.instance_methods - self.instance_methods - DESTRUCTIVE - - DELEGATES.each do |method| - class_eval %{ - def #{method}(*args, &block) - @list.send(:#{method}, *args, &block) - end - }, __FILE__, __LINE__ - 4 - end - def to_xml_string(str = +'') classname = @allowed_types[0].name.split('::').last - el_name = serialize_as.to_s || (classname[0, 1].downcase + classname[1..-1]) + el_name = serialize_as.to_s || (classname[0, 1].downcase + classname[1..]) str << '<' << el_name << ' count="' << size.to_s << '">' each { |item| item.to_xml_string(str) } str << '</' << el_name << '>' |
